Java 17 features (A-G) (#15516)

- 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-05 22:30:16 +02:00
committed by GitHub
parent a0dc5c05f2
commit cf10b3e9c7
486 changed files with 2053 additions and 1955 deletions

View File

@@ -64,8 +64,7 @@ public class BondHomeHandlerFactory extends BaseThingHandlerFactory {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();
if (THING_TYPE_BOND_BRIDGE.equals(thingTypeUID)) {
final BondBridgeHandler handler = new BondBridgeHandler((Bridge) thing, httpClientFactory);
return handler;
return new BondBridgeHandler((Bridge) thing, httpClientFactory);
} else if (SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
return new BondDeviceHandler(thing);
}

View File

@@ -219,7 +219,7 @@ public class BondHttpApi {
bridgeHandler.getBridgeToken());
ContentResponse response;
response = request.send();
String encoding = response.getEncoding() != null ? response.getEncoding().replaceAll("\"", "").trim()
String encoding = response.getEncoding() != null ? response.getEncoding().replace("\"", "").trim()
: StandardCharsets.UTF_8.name();
try {
httpResponse = new String(response.getContent(), encoding);

View File

@@ -58,9 +58,7 @@ public class BondDiscoveryService extends AbstractDiscoveryService implements Th
@Override
public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof BondBridgeHandler) {
@Nullable
BondBridgeHandler localHandler = (BondBridgeHandler) handler;
if (handler instanceof BondBridgeHandler localHandler) {
bridgeHandler = localHandler;
localHandler.setDiscoveryService(this);
api = localHandler.getBridgeAPI();

View File

@@ -170,8 +170,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
@Override
public void childHandlerInitialized(ThingHandler childHandler, Thing childThing) {
super.childHandlerInitialized(childHandler, childThing);
if (childHandler instanceof BondDeviceHandler) {
BondDeviceHandler handler = (BondDeviceHandler) childHandler;
if (childHandler instanceof BondDeviceHandler handler) {
synchronized (handlers) {
// Start the BPUP update service after the first child device is added
startUDPListenerJob();
@@ -184,8 +183,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
@Override
public void childHandlerDisposed(ThingHandler childHandler, Thing childThing) {
if (childHandler instanceof BondDeviceHandler) {
BondDeviceHandler handler = (BondDeviceHandler) childHandler;
if (childHandler instanceof BondDeviceHandler handler) {
synchronized (handlers) {
handlers.remove(handler);
if (handlers.isEmpty()) {
@@ -210,7 +208,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
String deviceId = null;
String topicType = null;
if (topic != null) {
String parts[] = topic.split("/");
String[] parts = topic.split("/");
deviceId = parts[1];
topicType = parts[2];
}
@@ -332,7 +330,7 @@ public class BondBridgeHandler extends BaseBridgeHandler {
@Override
public Collection<Class<? extends ThingHandlerService>> getServices() {
return Collections.singleton(BondDiscoveryService.class);
return Set.of(BondDiscoveryService.class);
}
public void setDiscoveryService(BondDiscoveryService discoveryService) {

View File

@@ -156,9 +156,9 @@ public class BondDeviceHandler extends BaseThingHandler {
case CHANNEL_FAN_SPEED:
logger.trace("Fan speed command");
if (command instanceof PercentType) {
if (command instanceof PercentType percentCommand) {
if (devInfo.actions.contains(BondDeviceAction.SET_FP_FAN)) {
value = ((PercentType) command).intValue();
value = percentCommand.intValue();
if (value == 0) {
action = BondDeviceAction.TURN_FP_FAN_OFF;
value = null;
@@ -169,7 +169,7 @@ public class BondDeviceHandler extends BaseThingHandler {
BondDeviceProperties devProperties = this.deviceProperties;
if (devProperties != null) {
int maxSpeed = devProperties.maxSpeed;
value = (int) Math.ceil(((PercentType) command).intValue() * maxSpeed / 100);
value = (int) Math.ceil(percentCommand.intValue() * maxSpeed / 100);
} else {
value = 1;
}
@@ -182,11 +182,10 @@ public class BondDeviceHandler extends BaseThingHandler {
}
logger.trace("Fan speed command with speed set as {}", value);
api.executeDeviceAction(deviceId, action, value);
} else if (command instanceof IncreaseDecreaseType) {
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
logger.trace("Fan increase/decrease speed command");
api.executeDeviceAction(deviceId,
((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE
? BondDeviceAction.INCREASE_SPEED
(increaseDecreaseCommand == IncreaseDecreaseType.INCREASE ? BondDeviceAction.INCREASE_SPEED
: BondDeviceAction.DECREASE_SPEED),
null);
} else if (command instanceof OnOffType) {
@@ -206,8 +205,8 @@ public class BondDeviceHandler extends BaseThingHandler {
break;
case CHANNEL_RAW_FAN_SPEED:
if (command instanceof DecimalType) {
value = ((DecimalType) command).intValue();
if (command instanceof DecimalType decimalCommand) {
value = decimalCommand.intValue();
BondDeviceProperties devProperties = this.deviceProperties;
if (devProperties != null) {
if (value < 1) {
@@ -246,7 +245,7 @@ public class BondDeviceHandler extends BaseThingHandler {
logger.trace("Fan direction command {}", command.toString());
if (command instanceof StringType) {
api.executeDeviceAction(deviceId, BondDeviceAction.SET_DIRECTION,
command.toString().equals("winter") ? -1 : 1);
"winter".equals(command.toString()) ? -1 : 1);
}
break;
@@ -258,9 +257,8 @@ public class BondDeviceHandler extends BaseThingHandler {
break;
case CHANNEL_LIGHT_BRIGHTNESS:
if (command instanceof PercentType) {
PercentType pctCommand = (PercentType) command;
value = pctCommand.intValue();
if (command instanceof PercentType percentCommand) {
value = percentCommand.intValue();
if (value == 0) {
action = BondDeviceAction.TURN_LIGHT_OFF;
value = null;
@@ -269,10 +267,10 @@ public class BondDeviceHandler extends BaseThingHandler {
}
logger.trace("Fan light brightness command with value of {}", value);
api.executeDeviceAction(deviceId, action, value);
} else if (command instanceof IncreaseDecreaseType) {
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
logger.trace("Fan light brightness increase/decrease command {}", command);
api.executeDeviceAction(deviceId,
((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE
(increaseDecreaseCommand == IncreaseDecreaseType.INCREASE
? BondDeviceAction.INCREASE_BRIGHTNESS
: BondDeviceAction.DECREASE_BRIGHTNESS),
null);
@@ -301,9 +299,8 @@ public class BondDeviceHandler extends BaseThingHandler {
case CHANNEL_UP_LIGHT_BRIGHTNESS:
enableUpLight();
if (command instanceof PercentType) {
PercentType pctCommand = (PercentType) command;
value = pctCommand.intValue();
if (command instanceof PercentType percentCommand) {
value = percentCommand.intValue();
if (value == 0) {
action = BondDeviceAction.TURN_LIGHT_OFF;
value = null;
@@ -312,10 +309,10 @@ public class BondDeviceHandler extends BaseThingHandler {
}
logger.trace("Fan up light brightness command with value of {}", value);
api.executeDeviceAction(deviceId, action, value);
} else if (command instanceof IncreaseDecreaseType) {
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
logger.trace("Fan uplight brightness increase/decrease command {}", command);
api.executeDeviceAction(deviceId,
((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE
(increaseDecreaseCommand == IncreaseDecreaseType.INCREASE
? BondDeviceAction.INCREASE_UP_LIGHT_BRIGHTNESS
: BondDeviceAction.DECREASE_UP_LIGHT_BRIGHTNESS),
null);
@@ -344,9 +341,8 @@ public class BondDeviceHandler extends BaseThingHandler {
case CHANNEL_DOWN_LIGHT_BRIGHTNESS:
enableDownLight();
if (command instanceof PercentType) {
PercentType pctCommand = (PercentType) command;
value = pctCommand.intValue();
if (command instanceof PercentType percentCommand) {
value = percentCommand.intValue();
if (value == 0) {
action = BondDeviceAction.TURN_LIGHT_OFF;
value = null;
@@ -355,10 +351,10 @@ public class BondDeviceHandler extends BaseThingHandler {
}
logger.trace("Fan down light brightness command with value of {}", value);
api.executeDeviceAction(deviceId, action, value);
} else if (command instanceof IncreaseDecreaseType) {
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
logger.trace("Fan down light brightness increase/decrease command");
api.executeDeviceAction(deviceId,
((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE
(increaseDecreaseCommand == IncreaseDecreaseType.INCREASE
? BondDeviceAction.INCREASE_DOWN_LIGHT_BRIGHTNESS
: BondDeviceAction.DECREASE_DOWN_LIGHT_BRIGHTNESS),
null);
@@ -373,9 +369,8 @@ public class BondDeviceHandler extends BaseThingHandler {
break;
case CHANNEL_FLAME:
if (command instanceof PercentType) {
PercentType pctCommand = (PercentType) command;
value = pctCommand.intValue();
if (command instanceof PercentType percentCommand) {
value = percentCommand.intValue();
if (value == 0) {
action = BondDeviceAction.TURN_OFF;
value = null;
@@ -384,11 +379,10 @@ public class BondDeviceHandler extends BaseThingHandler {
}
logger.trace("Fireplace flame command with value of {}", value);
api.executeDeviceAction(deviceId, action, value);
} else if (command instanceof IncreaseDecreaseType) {
} else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
logger.trace("Fireplace flame increase/decrease command");
api.executeDeviceAction(deviceId,
((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE
? BondDeviceAction.INCREASE_FLAME
(increaseDecreaseCommand == IncreaseDecreaseType.INCREASE ? BondDeviceAction.INCREASE_FLAME
: BondDeviceAction.DECREASE_FLAME),
null);
} else if (command instanceof OnOffType) {
@@ -540,7 +534,7 @@ public class BondDeviceHandler extends BaseThingHandler {
updateProperty(thingProperties, PROPERTIES_TEMPLATE_NAME, devInfo.template);
thingProperties.put(PROPERTIES_MAX_SPEED, String.valueOf(devProperties.maxSpeed));
thingProperties.put(PROPERTIES_TRUST_STATE, String.valueOf(devProperties.trustState));
thingProperties.put(PROPERTIES_ADDRESS, String.valueOf(devProperties.addr));
thingProperties.put(PROPERTIES_ADDRESS, devProperties.addr);
thingProperties.put(PROPERTIES_RF_FREQUENCY, String.valueOf(devProperties.freq));
logger.trace("Saving properties for {} ({})", config.deviceId, this.getThing().getLabel());
updateProperties(thingProperties);