Java 17 features (N-S) (#15565)

- add missing @override
- Java style array syntax
- remove redundant modifiers
- always move String constants to left side in comparisons
- simplify lambda expressions and return statements
- use replace instead of replaceAll w/o regex
- instanceof matching and multiline strings

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-09-13 08:03:31 +02:00
committed by GitHub
parent 641b482551
commit ab58f4ffb4
471 changed files with 1624 additions and 1868 deletions

View File

@@ -37,8 +37,8 @@ public class NetworkActions implements ThingActions {
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof NetworkHandler) {
this.handler = (NetworkHandler) handler;
if (handler instanceof NetworkHandler networkHandler) {
this.handler = networkHandler;
}
}

View File

@@ -181,7 +181,7 @@ public class NetworkDiscoveryService extends AbstractDiscoveryService implements
public static ThingUID createServiceUID(String ip, int tcpPort) {
// uid must not contains dots
return new ThingUID(SERVICE_DEVICE, ip.replace('.', '_') + "_" + String.valueOf(tcpPort));
return new ThingUID(SERVICE_DEVICE, ip.replace('.', '_') + "_" + tcpPort);
}
/**

View File

@@ -17,8 +17,9 @@ import static org.openhab.binding.network.internal.NetworkBindingConstants.*;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -181,7 +182,7 @@ public class NetworkHandler extends BaseThingHandler
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No port configured!");
return;
}
presenceDetection.setServicePorts(Collections.singleton(port));
presenceDetection.setServicePorts(Set.of(port));
} else {
// It does not harm to send an additional UDP packet to a device,
// therefore we assume all ping devices are iOS devices. If this
@@ -239,7 +240,7 @@ public class NetworkHandler extends BaseThingHandler
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singletonList(NetworkActions.class);
return List.of(NetworkActions.class);
}
public void sendWakeOnLanPacketViaIp() {

View File

@@ -208,11 +208,11 @@ public class NetworkUtils {
return IpPingMethodEnum.JAVA_PING;
} else {
os = os.toLowerCase();
if (os.indexOf("win") >= 0) {
if (os.contains("win")) {
method = IpPingMethodEnum.WINDOWS_PING;
} else if (os.indexOf("mac") >= 0) {
} else if (os.contains("mac")) {
method = IpPingMethodEnum.MAC_OS_PING;
} else if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 || os.indexOf("aix") >= 0) {
} else if (os.contains("nix") || os.contains("nux") || os.contains("aix")) {
method = IpPingMethodEnum.IPUTILS_LINUX_PING;
} else {
// We cannot estimate the command line for any other operating system and just return false

View File

@@ -20,8 +20,8 @@ import static org.mockito.Mockito.*;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
@@ -62,7 +62,7 @@ public class PresenceDetectionTest {
@BeforeEach
public void setUp() throws UnknownHostException {
// Mock an interface
when(networkUtils.getInterfaceNames()).thenReturn(Collections.singleton("TESTinterface"));
when(networkUtils.getInterfaceNames()).thenReturn(Set.of("TESTinterface"));
doReturn(ArpPingUtilEnum.IPUTILS_ARPING).when(networkUtils).determineNativeARPpingMethod(anyString());
doReturn(IpPingMethodEnum.WINDOWS_PING).when(networkUtils).determinePingMethod();
@@ -77,7 +77,7 @@ public class PresenceDetectionTest {
subject.setTimeout(300);
subject.setUseDhcpSniffing(false);
subject.setIOSDevice(true);
subject.setServicePorts(Collections.singleton(1010));
subject.setServicePorts(Set.of(1010));
subject.setUseArpPing(true, "arping", ArpPingUtilEnum.IPUTILS_ARPING);
subject.setUseIcmpPing(true);

View File

@@ -17,7 +17,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -80,7 +80,7 @@ public class DiscoveryTest {
// TCP device
when(value.isPingReachable()).thenReturn(false);
when(value.isTCPServiceReachable()).thenReturn(true);
when(value.getReachableTCPports()).thenReturn(Collections.singletonList(1010));
when(value.getReachableTCPports()).thenReturn(List.of(1010));
d.partialDetectionResult(value);
verify(listener).thingDiscovered(any(), result.capture());
DiscoveryResult dresult = result.getValue();