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

@@ -84,8 +84,8 @@ public class OpenWebNetCENActions implements ThingActions {
// legacy delegate methods
public static void virtualPress(@Nullable ThingActions actions, @Nullable String press, int button) {
if (actions instanceof OpenWebNetCENActions) {
((OpenWebNetCENActions) actions).virtualPress(press, button);
if (actions instanceof OpenWebNetCENActions openWebNetCENActions) {
openWebNetCENActions.virtualPress(press, button);
} else {
throw new IllegalArgumentException("Instance is not an OpenWebNetCENActions class.");
}

View File

@@ -13,7 +13,6 @@
package org.openhab.binding.openwebnet.internal.discovery;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@@ -145,7 +144,7 @@ public class BusGatewayUpnpDiscovery implements UpnpDiscoveryParticipant {
@Override
public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
return Collections.singleton(OpenWebNetBindingConstants.THING_TYPE_BUS_GATEWAY);
return Set.of(OpenWebNetBindingConstants.THING_TYPE_BUS_GATEWAY);
}
@Override

View File

@@ -270,14 +270,14 @@ public class OpenWebNetDeviceDiscoveryService extends AbstractDiscoveryService
whereConfig, properties.get(OpenWebNetBindingConstants.CONFIG_PROPERTY_STANDALONE));
}
if (w instanceof WhereZigBee && WhereZigBee.UNIT_02.equals(((WhereZigBee) w).getUnit())) {
if (w instanceof WhereZigBee whereZigBee && WhereZigBee.UNIT_02.equals(whereZigBee.getUnit())) {
logger.debug("UNIT=02 found (WHERE={}) -> will remove previous result if exists", w);
thingRemoved(thingUID); // remove previously discovered thing
// re-create thingUID with new type
thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH_2UNITS;
thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_ON_OFF_SWITCH_2UNITS;
thingUID = new ThingUID(thingTypeUID, bridgeUID, tId);
whereConfig = ((WhereZigBee) w).valueWithUnit(WhereZigBee.UNIT_ALL); // replace unit '02' with '00'
whereConfig = whereZigBee.valueWithUnit(WhereZigBee.UNIT_ALL); // replace unit '02' with '00'
logger.debug("UNIT=02, switching type from {} to {}",
OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH,
OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH_2UNITS);
@@ -302,9 +302,9 @@ public class OpenWebNetDeviceDiscoveryService extends AbstractDiscoveryService
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof OpenWebNetBridgeHandler) {
if (handler instanceof OpenWebNetBridgeHandler openWebNetBridgeHandler) {
logger.debug("attaching {} to handler {} ", this, handler);
bridgeHandler = (OpenWebNetBridgeHandler) handler;
bridgeHandler = openWebNetBridgeHandler;
bridgeHandler.deviceDiscoveryService = this;
bridgeUID = bridgeHandler.getThing().getUID();
}

View File

@@ -12,9 +12,9 @@
*/
package org.openhab.binding.openwebnet.internal.discovery;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@@ -78,8 +78,7 @@ public class UsbGatewayDiscoveryService extends AbstractDiscoveryService impleme
*/
@Activate
public UsbGatewayDiscoveryService(final @Reference SerialPortManager spm) {
super(Collections.singleton(OpenWebNetBindingConstants.THING_TYPE_ZB_GATEWAY), DISCOVERY_TIMEOUT_SECONDS,
false);
super(Set.of(OpenWebNetBindingConstants.THING_TYPE_ZB_GATEWAY), DISCOVERY_TIMEOUT_SECONDS, false);
// Obtain the serial port manager service using an OSGi reference
serialPortManager = spm;
}

View File

@@ -107,12 +107,12 @@ public class OpenWebNetAutomationHandler extends OpenWebNetThingHandler {
if (shutterRunConfig == null) {
shutterRunConfig = AUTO_CALIBRATION;
logger.debug("shutterRun null --> default to AUTO");
} else if (shutterRunConfig instanceof String) {
if (AUTO_CALIBRATION.equalsIgnoreCase(((String) shutterRunConfig))) {
} else if (shutterRunConfig instanceof String stringValue) {
if (AUTO_CALIBRATION.equalsIgnoreCase(stringValue)) {
logger.debug("shutterRun set to AUTO via configuration");
shutterRun = SHUTTER_RUN_UNDEFINED; // reset shutterRun
} else { // try to parse int>=1000
int shutterRunInt = Integer.parseInt((String) shutterRunConfig);
int shutterRunInt = Integer.parseInt(stringValue);
if (shutterRunInt < 1000) {
throw new NumberFormatException();
}
@@ -210,8 +210,8 @@ public class OpenWebNetAutomationHandler extends OpenWebNetThingHandler {
} else {
send(Automation.requestMoveDown(w.value()));
}
} else if (command instanceof PercentType) {
handlePercentCommand((PercentType) command, w.value());
} else if (command instanceof PercentType percentCommand) {
handlePercentCommand(percentCommand, w.value());
}
}
} else {

View File

@@ -268,7 +268,7 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(OpenWebNetDeviceDiscoveryService.class);
return Set.of(OpenWebNetDeviceDiscoveryService.class);
}
/**
@@ -505,8 +505,7 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement
return; // we ignore ACKS/NACKS
}
// GATEWAY MANAGEMENT
if (msg instanceof GatewayMgmt) {
GatewayMgmt gwMsg = (GatewayMgmt) msg;
if (msg instanceof GatewayMgmt gwMsg) {
if (dateTimeSynch && GatewayMgmt.DimGatewayMgmt.DATETIME.equals(gwMsg.getDim())) {
checkDateTimeDiff(gwMsg);
}
@@ -573,9 +572,9 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement
logger.warn("received onConnected() but gateway is null");
return;
}
if (gw instanceof USBGateway) {
if (gw instanceof USBGateway usbGateway) {
logger.info("---- CONNECTED to Zigbee USB gateway bridge '{}' (serialPort: {})", thing.getUID(),
((USBGateway) gw).getSerialPortName());
usbGateway.getSerialPortName());
} else {
logger.info("---- CONNECTED to BUS gateway bridge '{}' ({}:{})", thing.getUID(),
((BUSGateway) gw).getHost(), ((BUSGateway) gw).getPort());
@@ -736,8 +735,8 @@ public class OpenWebNetBridgeHandler extends ConfigStatusBridgeHandler implement
*/
public String normalizeWhere(Where where) {
String str = where.value();
if (where instanceof WhereZigBee) {
str = ((WhereZigBee) where).valueWithUnit(WhereZigBee.UNIT_ALL); // 76543210X#9 --> 765432100#9
if (where instanceof WhereZigBee whereZigBee) {
str = whereZigBee.valueWithUnit(WhereZigBee.UNIT_ALL); // 76543210X#9 --> 765432100#9
} else {
if (str.indexOf("#4#") == -1) { // skip APL#4#bus case
if (str.indexOf('#') == 0) { // Thermo central unit (#0) or zone via central unit (#Z, Z=[1-99]) --> Z,

View File

@@ -169,8 +169,8 @@ public class OpenWebNetLightingHandler extends OpenWebNetThingHandler {
*/
private void handleBrightnessCommand(Command command) {
logger.debug("handleBrightnessCommand() command={}", command);
if (command instanceof PercentType) {
dimLightTo(((PercentType) command).intValue(), command);
if (command instanceof PercentType percentCommand) {
dimLightTo(percentCommand.intValue(), command);
} else if (command instanceof IncreaseDecreaseType) {
if (IncreaseDecreaseType.INCREASE.equals(command)) {
dimLightTo(brightness + 10, command);

View File

@@ -16,7 +16,6 @@ import static org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.Scanner;
import java.util.Set;
@@ -162,7 +161,7 @@ public class OpenWebNetScenarioHandler extends OpenWebNetThingHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(OpenWebNetCENActions.class);
return Set.of(OpenWebNetCENActions.class);
}
@Override