[io] Use Java 17 features (#15485)

* instanceof matching and multiline strings
* use Map/Set/List.of instead of Collections

Signed-off-by: Holger Friedrich <mail@holger-friedrich.de>
This commit is contained in:
Holger Friedrich
2023-08-24 06:44:56 +02:00
committed by GitHub
parent 6e49c6e6ec
commit 56e7daf898
16 changed files with 50 additions and 59 deletions

View File

@@ -237,8 +237,7 @@ public class CloudClient {
})//
.on(Manager.EVENT_CONNECT_ERROR, args -> {
if (args.length > 0) {
if (args[0] instanceof Exception) {
Exception e = (Exception) args[0];
if (args[0] instanceof Exception e) {
logger.debug(
"Error connecting to the openHAB Cloud instance: {} {}. Should reconnect automatically.",
e.getClass().getSimpleName(), e.getMessage());
@@ -256,8 +255,8 @@ public class CloudClient {
.on(Manager.EVENT_PACKET, args -> {
int packetTypeIndex = -1;
String type = "<unexpected packet type>";
if (args.length == 1 && args[0] instanceof Packet<?>) {
packetTypeIndex = ((Packet<?>) args[0]).type;
if (args.length == 1 && args[0] instanceof Packet<?> packet) {
packetTypeIndex = packet.type;
if (packetTypeIndex < Parser.types.length) {
type = Parser.types[packetTypeIndex];
@@ -282,8 +281,7 @@ public class CloudClient {
.on(Socket.EVENT_RECONNECT,
args -> logger.debug("Socket.IO re-connected successfully (attempt {})", args[0]))//
.on(Socket.EVENT_RECONNECT_ERROR, args -> {
if (args[0] instanceof Exception) {
Exception e = (Exception) args[0];
if (args[0] instanceof Exception e) {
logger.debug("Socket.IO re-connect attempt error: {} {}", e.getClass().getSimpleName(),
e.getMessage());
} else {
@@ -308,8 +306,7 @@ public class CloudClient {
.on(Socket.EVENT_ERROR, args -> {
if (CloudClient.this.socket.connected()) {
if (args.length > 0) {
if (args[0] instanceof Exception) {
Exception e = (Exception) args[0];
if (args[0] instanceof Exception e) {
logger.warn("Error during communication: {} {}", e.getClass().getSimpleName(),
e.getMessage());
} else {
@@ -335,8 +332,7 @@ public class CloudClient {
long delay = reconnectBackoff.duration();
// Try reconnecting on connection errors
if (args.length > 0) {
if (args[0] instanceof Exception) {
Exception e = (Exception) args[0];
if (args[0] instanceof Exception e) {
logger.warn(
"Error connecting to the openHAB Cloud instance: {} {}. Reconnecting after {} ms.",
e.getClass().getSimpleName(), e.getMessage(), delay);

View File

@@ -211,8 +211,7 @@ public class CloudService implements ActionService, CloudClientListener, EventSu
exposedItems = new HashSet<>();
Object expCfg = config.get(CFG_EXPOSE);
if (expCfg instanceof String) {
String value = (String) expCfg;
if (expCfg instanceof String value) {
while (value.startsWith("[")) {
value = value.substring(1);
}
@@ -222,8 +221,8 @@ public class CloudService implements ActionService, CloudClientListener, EventSu
for (String itemName : Arrays.asList((value).split(","))) {
exposedItems.add(itemName.trim());
}
} else if (expCfg instanceof Iterable) {
for (Object entry : ((Iterable<?>) expCfg)) {
} else if (expCfg instanceof Iterable iterable) {
for (Object entry : iterable) {
exposedItems.add(entry.toString());
}
}

View File

@@ -62,17 +62,17 @@ public class NotificationModuleHandlerFactory extends BaseModuleHandlerFactory {
@Override
protected @Nullable ModuleHandler internalCreate(Module module, String ruleUID) {
if (module instanceof Action) {
if (module instanceof Action action) {
switch (module.getTypeUID()) {
case SendNotificationActionHandler.TYPE_ID:
case SendNotificationActionHandler.EXTENDED_TYPE_ID:
return new SendNotificationActionHandler((Action) module, cloudService);
return new SendNotificationActionHandler(action, cloudService);
case SendBroadcastNotificationActionHandler.TYPE_ID:
case SendBroadcastNotificationActionHandler.EXTENDED_TYPE_ID:
return new SendBroadcastNotificationActionHandler((Action) module, cloudService);
return new SendBroadcastNotificationActionHandler(action, cloudService);
case SendLogNotificationActionHandler.TYPE_ID:
case SendLogNotificationActionHandler.EXTENDED_TYPE_ID:
return new SendLogNotificationActionHandler((Action) module, cloudService);
return new SendLogNotificationActionHandler(action, cloudService);
default:
break;
}