]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hueemulation] Java 17 features (#15496)
authorHolger Friedrich <holgerfriedrich@users.noreply.github.com>
Fri, 25 Aug 2023 15:48:55 +0000 (17:48 +0200)
committerGitHub <noreply@github.com>
Fri, 25 Aug 2023 15:48:55 +0000 (17:48 +0200)
- 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 <mail@holger-friedrich.de>
16 files changed:
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/RuleUtils.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/automation/HueRuleConditionHandler.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/AbstractHueState.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueAuthorizedConfig.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueDataStore.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueGroupEntry.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueLightEntry.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueRuleEntry.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueSceneEntry.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/dto/HueStateColorBulb.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Rules.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Scenes.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/Schedules.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/StatusResource.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/rest/UserManagement.java
bundles/org.openhab.io.hueemulation/src/main/java/org/openhab/io/hueemulation/internal/upnp/UpnpServer.java

index 73819c2637357b7a3b2590d69e9a2447e0ddf138..78018cf1d05c44104ea3ba4cda0e024bc72f92b0 100644 (file)
@@ -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<Trigger> triggers) {
         Optional<Trigger> 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");
index 6ce2e60eb07a071ec44f09694804a53b3dc95876..de52dc1b4891affa276e4f67400e81b633f4f713 100644 (file)
@@ -70,7 +70,7 @@ public class HueRuleConditionHandler extends BaseModuleHandler<Condition> 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<Condition> 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])) {
index 444c14bd827a12e0eccee23793026283323bb704..f438e49efc46169aad533b70263d19e84029fd81 100644 (file)
@@ -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,
index 0f615d31f9ed647b01371861511b6565b963b2c2..b642e61499e066849a4a01b82ae783aa1715b533 100644 (file)
@@ -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);
         }
     }
 }
index c8370d00c17acd31a227cf65335c6a18d20a4b56..5971afed2de0c6e4f1459b112916347dd46f225e 100644 (file)
@@ -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;
             }
index d43622516c3ac08e7a2b0508e715cd2d733dabaa..34ad8d7ef72acf2dfb2d0bcd51ad8dbb0e969086 100644 (file)
@@ -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);
         }
     }
 }
index 15b1b8b33e93b9d59f1daee2627c3775b109cda4..a84dfdd858c318e1248a09cce7b853cee2414b6c 100644 (file)
@@ -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);
         }
     }
 
index c7a824a4528a9543c261ecd737148af1b07c8f94..891333e06befdbe94d8e06c40e1c44fa22325563 100644 (file)
@@ -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.
index 541cfafbe261a3be1044871cf55c5dabd55ed925..e39e82ebea76debb6c84c30db3d313fb858d0bc6 100644 (file)
@@ -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
     }
index c1ce0b7d0379076603bfa714c49ff7fe27413476..ecf852a9edc70bd40a307cebc203b23b095e23a4 100644 (file)
@@ -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
index 661e3ff8fe1fa97a992e08a6336ebf3cd8458679..a5851dc85e99d0e1aa98c6ef81585730a26f96bb 100644 (file)
@@ -121,7 +121,7 @@ public class Rules implements RegistryChangeListener<Rule> {
             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<Rule> {
             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<Rule> {
             RuleBuilder builder, List<Trigger> oldTriggers, List<Condition> oldConditions, ItemRegistry itemRegistry) {
         // Preserve all triggers, conditions that are not part of hue rules
         Map<String, Trigger> 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<String, Condition> 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<Rule> {
             String apikey) {
         // Preserve all actions that are not "rules.HttpAction"
         List<Action> 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;
index 2081e4a190250f69d1f14459cbb54475971b67db..f02e32483994b17781b1d51fcf0918d773aea13f 100644 (file)
@@ -129,7 +129,7 @@ public class Scenes implements RegistryChangeListener<Rule> {
         List<String> 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);
index 0ba36a441f2b92deb183b18f31e1dd9977aa6c00..7729d6453ef5780de722385da6bcfbb2bc0ff9bc 100644 (file)
@@ -130,7 +130,7 @@ public class Schedules implements RegistryChangeListener<Rule> {
         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<Rule> {
         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<Rule> {
 
         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<Rule> {
 
         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();
index 68ac1eb0aead6cdb84bd8a7d3e7a80ec4c59936c..018eb46dc84040613397e12508d8b061518d0d2f 100644 (file)
@@ -154,7 +154,7 @@ public class StatusResource implements RegistryListener {
                 + user.getValue().name + "</b> <small>" + user.getValue().lastUseDate + "</small>")
                 .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))))
index 29e973c5065ae4e8cc418db6d6fd90a2e74d9067..6aba374a51425cf0997bac392981c98bf490c3ee 100644 (file)
@@ -120,7 +120,7 @@ public class UserManagement extends DefaultAbstractManagedProvider<HueUserAuthWi
             return;
         }
         logger.debug("APIKey {} added", apiKey);
-        String l[] = label.split("#");
+        String[] l = label.split("#");
         HueUserAuthWithSecrets hueUserAuth = new HueUserAuthWithSecrets(l[0], l.length == 2 ? l[1] : "openhab", apiKey,
                 clientKey);
         cs.ds.config.whitelist.put(apiKey, hueUserAuth);
index a973b76b848a8e0a07b3e672c41217c26c86e992..668bb7f80d23da9583a29b1719fe20e3ddc0f4ed 100644 (file)
@@ -349,9 +349,8 @@ public class UpnpServer extends HttpServlet implements Consumer<HueEmulationConf
         }
         configChangeFuture = root.thenApply(this::createConfiguration)
                 .thenApplyAsync(this::performAddressTest, executor).thenApply(this::applyConfiguration)
-                .thenCompose(c -> {
-                    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);
                     }