From d36833feef860a068b782a6d4b5ccccdb8bf2e53 Mon Sep 17 00:00:00 2001 From: Holger Friedrich Date: Fri, 25 Aug 2023 17:48:55 +0200 Subject: [PATCH] [hueemulation] Java 17 features (#15496) - 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 Signed-off-by: Holger Friedrich --- .../io/hueemulation/internal/RuleUtils.java | 14 +++++++------- .../automation/HueRuleConditionHandler.java | 4 ++-- .../internal/dto/AbstractHueState.java | 2 +- .../internal/dto/HueAuthorizedConfig.java | 3 +-- .../io/hueemulation/internal/dto/HueDataStore.java | 2 +- .../hueemulation/internal/dto/HueGroupEntry.java | 5 ++--- .../hueemulation/internal/dto/HueLightEntry.java | 3 +-- .../io/hueemulation/internal/dto/HueRuleEntry.java | 2 +- .../hueemulation/internal/dto/HueSceneEntry.java | 2 +- .../internal/dto/HueStateColorBulb.java | 2 +- .../io/hueemulation/internal/rest/Rules.java | 10 +++++----- .../io/hueemulation/internal/rest/Scenes.java | 2 +- .../io/hueemulation/internal/rest/Schedules.java | 8 ++++---- .../hueemulation/internal/rest/StatusResource.java | 2 +- .../hueemulation/internal/rest/UserManagement.java | 2 +- .../io/hueemulation/internal/upnp/UpnpServer.java | 5 ++--- 16 files changed, 32 insertions(+), 36 deletions(-) diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/RuleUtils.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/RuleUtils.java index 73819c263..78018cf1d 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/RuleUtils.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/RuleUtils.java @@ -56,7 +56,7 @@ public class RuleUtils { throw new IllegalStateException("Time pattern incorrect. Must be 'hh:mm[:ss]'. " + baseTime); } - String randomizedTime[] = baseTime.split(":"); + String[] randomizedTime = baseTime.split(":"); if (upperTime != null && !upperTime.isEmpty()) { String[] upperTimeParts = upperTime.split(":"); @@ -89,7 +89,7 @@ public class RuleUtils { @SuppressWarnings({ "unused", "null" }) public static void validateHueHttpAddress(HueDataStore ds, String address) throws IllegalStateException { String[] validation = address.split("/"); - if (validation.length < 6 || !validation[0].isEmpty() || !validation[1].equals("api")) { + if (validation.length < 6 || !validation[0].isEmpty() || !"api".equals(validation[1])) { throw new IllegalStateException("Given address invalid!"); } if ("groups".equals(validation[3]) && "action".equals(validation[5])) { @@ -179,7 +179,7 @@ public class RuleUtils { cronWeekdays.add(String.valueOf(c)); } } - String hourMinSec[] = RuleUtils.computeRandomizedDayTime(time, randomize); + String[] hourMinSec = RuleUtils.computeRandomizedDayTime(time, randomize); // Cron expression: min hour day month weekdays String cronExpression = hourMinSec[1] + " " + hourMinSec[0] + " * * " + String.join(",", cronWeekdays); @@ -318,9 +318,9 @@ public class RuleUtils { public static @Nullable String timeStringFromTrigger(List triggers) { Optional trigger; - trigger = triggers.stream().filter(p -> p.getId().equals("crontrigger")).findFirst(); + trigger = triggers.stream().filter(p -> "crontrigger".equals(p.getId())).findFirst(); if (trigger.isPresent()) { - String cronParts[] = ((String) trigger.get().getConfiguration().get("cronExpression")).split(" "); + String[] cronParts = ((String) trigger.get().getConfiguration().get("cronExpression")).split(" "); if (cronParts.length != 5) { LOGGER.warn("Cron trigger has no valid cron expression: {}", String.join(",", cronParts)); return null; @@ -358,7 +358,7 @@ public class RuleUtils { return String.format("W%d/T%s:%s:00", weekdays, cronParts[1], cronParts[0]); } - trigger = triggers.stream().filter(p -> p.getId().equals("timertrigger")).findFirst(); + trigger = triggers.stream().filter(p -> "timertrigger".equals(p.getId())).findFirst(); if (trigger.isPresent()) { TimerConfig c = trigger.get().getConfiguration().as(TimerConfig.class); if (c.repeat == null) { @@ -370,7 +370,7 @@ public class RuleUtils { c.randomizeTime); } } else { - trigger = triggers.stream().filter(p -> p.getId().equals("absolutetrigger")).findFirst(); + trigger = triggers.stream().filter(p -> "absolutetrigger".equals(p.getId())).findFirst(); if (trigger.isPresent()) { String date = (String) trigger.get().getConfiguration().get("date"); String time = (String) trigger.get().getConfiguration().get("time"); diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/automation/HueRuleConditionHandler.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/automation/HueRuleConditionHandler.java index 6ce2e60eb..de52dc1b4 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/automation/HueRuleConditionHandler.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/automation/HueRuleConditionHandler.java @@ -70,7 +70,7 @@ public class HueRuleConditionHandler extends BaseModuleHandler implem private final DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern(TIME_FORMAT); private final Pattern timePattern = Pattern.compile("W(.*)/T(.*)/T(.*)"); // weekdays range from Monday to Sunday (1-7). The first entry is not used - private final boolean weekDaysAllowed[] = { false, false, false, false, false, false, false, false }; + private final boolean[] weekDaysAllowed = { false, false, false, false, false, false, false, false }; @SuppressWarnings({ "null", "unused" }) public HueRuleConditionHandler(Condition module, HueDataStore ds) { @@ -78,7 +78,7 @@ public class HueRuleConditionHandler extends BaseModuleHandler implem config = module.getConfiguration().as(HueRuleEntry.Condition.class); // pattern: "/sensors/2/state/buttonevent" or "/config/localtime" - String validation[] = config.address.split("/"); + String[] validation = config.address.split("/"); String uid = validation[2]; if ("groups".equals(validation[1]) && "action".equals(validation[3])) { diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/AbstractHueState.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/AbstractHueState.java index 444c14bd8..f438e49ef 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/AbstractHueState.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/AbstractHueState.java @@ -22,7 +22,7 @@ public class AbstractHueState { public boolean reachable = true; public String mode = "homeautomation"; - public static enum AlertEnum { + public enum AlertEnum { none, /** flashes light once */ select, diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueAuthorizedConfig.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueAuthorizedConfig.java index 0f615d31f..b642e6149 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueAuthorizedConfig.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueAuthorizedConfig.java @@ -91,8 +91,7 @@ public class HueAuthorizedConfig extends HueUnauthorizedConfig { public JsonElement serialize(HueAuthorizedConfig src, Type typeOfSrc, JsonSerializationContext context) { src.UTC = LocalDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); src.localtime = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); - JsonElement jsonSubscription = context.serialize(src, HueAuthorizedConfigHelper.class); - return jsonSubscription; + return context.serialize(src, HueAuthorizedConfigHelper.class); } } } diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueDataStore.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueDataStore.java index c8370d00c..5971afed2 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueDataStore.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueDataStore.java @@ -69,7 +69,7 @@ public class HueDataStore { public String nextGroupID() { int nextId = groups.size(); while (true) { - String id = "hueemulation" + String.valueOf(nextId); + String id = "hueemulation" + nextId; if (!groups.containsKey(id)) { return id; } diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueGroupEntry.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueGroupEntry.java index d43622516..34ad8d7ef 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueGroupEntry.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueGroupEntry.java @@ -35,7 +35,7 @@ import com.google.gson.annotations.SerializedName; */ @NonNullByDefault public class HueGroupEntry { - public static enum TypeEnum { + public enum TypeEnum { LightGroup, // 1.4 Luminaire, // 1.4 LightSource, // 1.4 @@ -102,8 +102,7 @@ public class HueGroupEntry { .collect(Collectors.toList()); } - JsonElement jsonSubscription = context.serialize(product, HueGroupHelper.class); - return jsonSubscription; + return context.serialize(product, HueGroupHelper.class); } } } diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueLightEntry.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueLightEntry.java index 15b1b8b33..a84dfdd85 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueLightEntry.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueLightEntry.java @@ -176,8 +176,7 @@ public class HueLightEntry { product.name = label; } - JsonElement jsonSubscription = context.serialize(product, HueDeviceHelper.class); - return jsonSubscription; + return context.serialize(product, HueDeviceHelper.class); } } diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueRuleEntry.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueRuleEntry.java index c7a824a45..891333e06 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueRuleEntry.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueRuleEntry.java @@ -41,7 +41,7 @@ public class HueRuleEntry { public String owner = ""; - public static enum Operator { + public enum Operator { unknown, eq, // equals, Used for bool and int. gt, // greater than, Allowed on int values. diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueSceneEntry.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueSceneEntry.java index 541cfafbe..e39e82ebe 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueSceneEntry.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueSceneEntry.java @@ -24,7 +24,7 @@ import org.eclipse.jdt.annotation.Nullable; */ @NonNullByDefault public class HueSceneEntry { - public static enum TypeEnum { + public enum TypeEnum { LightScene, // 1.28 GroupScene, // 1.28 } diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueStateColorBulb.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueStateColorBulb.java index c1ce0b7d0..ecf852a9e 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueStateColorBulb.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueStateColorBulb.java @@ -38,7 +38,7 @@ public class HueStateColorBulb extends HueStateBulb { /** time for transition in centiseconds. */ public int transitiontime; - public static enum ColorMode { + public enum ColorMode { ct, hs, xy diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Rules.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Rules.java index 661e3ff8f..a5851dc85 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Rules.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Rules.java @@ -121,7 +121,7 @@ public class Rules implements RegistryChangeListener { entry.description = desc; } - rule.getConditions().stream().filter(c -> c.getTypeUID().equals("hue.ruleCondition")).forEach(c -> { + rule.getConditions().stream().filter(c -> "hue.ruleCondition".equals(c.getTypeUID())).forEach(c -> { HueRuleEntry.Condition condition = c.getConfiguration().as(HueRuleEntry.Condition.class); // address with pattern "/sensors/2/state/buttonevent" String[] parts = condition.address.split("/"); @@ -132,7 +132,7 @@ public class Rules implements RegistryChangeListener { entry.conditions.add(condition); }); - rule.getActions().stream().filter(a -> a.getTypeUID().equals("rules.HttpAction")).forEach(a -> { + rule.getActions().stream().filter(a -> "rules.HttpAction".equals(a.getTypeUID())).forEach(a -> { HueCommand command = RuleUtils.httpActionToHueCommand(cs.ds, a, rule.getName()); if (command == null) { return; @@ -205,11 +205,11 @@ public class Rules implements RegistryChangeListener { RuleBuilder builder, List oldTriggers, List oldConditions, ItemRegistry itemRegistry) { // Preserve all triggers, conditions that are not part of hue rules Map triggers = new TreeMap<>(); - triggers.putAll(oldTriggers.stream().filter(a -> !a.getTypeUID().equals("core.ItemStateChangeTrigger")) + triggers.putAll(oldTriggers.stream().filter(a -> !"core.ItemStateChangeTrigger".equals(a.getTypeUID())) .collect(Collectors.toMap(e -> e.getId(), e -> e))); Map conditions = new TreeMap<>(); - conditions.putAll(oldConditions.stream().filter(a -> !a.getTypeUID().equals("hue.ruleCondition")) + conditions.putAll(oldConditions.stream().filter(a -> !"hue.ruleCondition".equals(a.getTypeUID())) .collect(Collectors.toMap(e -> e.getId(), e -> e))); for (HueRuleEntry.Condition condition : hueConditions) { @@ -227,7 +227,7 @@ public class Rules implements RegistryChangeListener { String apikey) { // Preserve all actions that are not "rules.HttpAction" List actions = new ArrayList<>(oldActions); - actions.removeIf(a -> a.getTypeUID().equals("rules.HttpAction")); + actions.removeIf(a -> "rules.HttpAction".equals(a.getTypeUID())); for (HueCommand command : hueActions) { command.address = "/api/" + apikey + command.address; diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Scenes.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Scenes.java index 2081e4a19..f02e32483 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Scenes.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Scenes.java @@ -129,7 +129,7 @@ public class Scenes implements RegistryChangeListener { List items = new ArrayList<>(); for (Action a : scene.getActions()) { - if (!a.getTypeUID().equals("core.ItemCommandAction")) { + if (!"core.ItemCommandAction".equals(a.getTypeUID())) { continue; } ItemCommandActionConfig config = a.getConfiguration().as(ItemCommandActionConfig.class); diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Schedules.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Schedules.java index 0ba36a441..7729d6453 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Schedules.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Schedules.java @@ -130,7 +130,7 @@ public class Schedules implements RegistryChangeListener { HueScheduleEntry entry = new HueScheduleEntry(); entry.name = rule.getName(); entry.description = rule.getDescription(); - entry.autodelete = rule.getActions().stream().anyMatch(p -> p.getId().equals("autodelete")); + entry.autodelete = rule.getActions().stream().anyMatch(p -> "autodelete".equals(p.getId())); entry.status = ruleManager.isEnabled(rule.getUID()) ? "enabled" : "disabled"; String timeStringFromTrigger = RuleUtils.timeStringFromTrigger(rule.getTriggers()); @@ -142,7 +142,7 @@ public class Schedules implements RegistryChangeListener { entry.localtime = timeStringFromTrigger; for (Action a : rule.getActions()) { - if (!a.getTypeUID().equals("rules.HttpAction")) { + if (!"rules.HttpAction".equals(a.getTypeUID())) { continue; } HueCommand command = RuleUtils.httpActionToHueCommand(cs.ds, a, rule.getName()); @@ -208,7 +208,7 @@ public class Schedules implements RegistryChangeListener { if (command != null) { RuleUtils.validateHueHttpAddress(ds, command.address); - actions.removeIf(a -> a.getId().equals("command")); // Remove old command action if any and add new one + actions.removeIf(a -> "command".equals(a.getId())); // Remove old command action if any and add new one actions.add(RuleUtils.createHttpAction(command, "command")); } else if (oldActions.isEmpty()) { // This is a new rule without an action yet throw new IllegalStateException("No command set!"); @@ -216,7 +216,7 @@ public class Schedules implements RegistryChangeListener { if (autodelete != null) { // Remove action to remove rule after execution - actions = actions.stream().filter(e -> !e.getId().equals("autodelete")) + actions = actions.stream().filter(e -> !"autodelete".equals(e.getId())) .collect(Collectors.toCollection(() -> new ArrayList<>())); if (autodelete) { // Add action to remove this rule again after execution final Configuration actionConfig = new Configuration(); diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/StatusResource.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/StatusResource.java index 68ac1eb0a..018eb46dc 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/StatusResource.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/StatusResource.java @@ -154,7 +154,7 @@ public class StatusResource implements RegistryListener { + user.getValue().name + " " + user.getValue().lastUseDate + "") .collect(Collectors.joining("\n")); - String url = "http://" + cs.ds.config.ipaddress + ":" + String.valueOf(localDiscovery.getDefaultport()); + String url = "http://" + cs.ds.config.ipaddress + ":" + localDiscovery.getDefaultport(); String reachable = localDiscovery.selfTests().stream() .map(entry -> TR(TD(entry.address) + TD(toYesNo(entry.reachable)) + TD(toYesNo(entry.isOurs)))) diff --git a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/UserManagement.java b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/UserManagement.java index 29e973c50..6aba374a5 100644 --- a/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/UserManagement.java +++ b/bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/UserManagement.java @@ -120,7 +120,7 @@ public class UserManagement extends DefaultAbstractManagedProvider { - return c.startNow(); - }).whenComplete((HueEmulationConfigWithRuntime config, @Nullable Throwable e) -> { + .thenCompose(c -> c.startNow()) + .whenComplete((HueEmulationConfigWithRuntime config, @Nullable Throwable e) -> { if (e != null) { logger.warn("Upnp server: Address test failed", e); }