]> git.basschouten.com Git - openhab-addons.git/commitdiff
[hue] Fix compile warnings (#17293)
authorlsiepel <leosiepel@gmail.com>
Mon, 19 Aug 2024 22:01:10 +0000 (00:01 +0200)
committerGitHub <noreply@github.com>
Mon, 19 Aug 2024 22:01:10 +0000 (00:01 +0200)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/connection/Clip2Bridge.java
bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java
bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/clip2/Clip2DtoTest.java

index 8adb111a2d4b0a512dd29d3365057fa5bf1582e1..c05594aa8df43712b9e4a6d10b4484494c01461c 100644 (file)
@@ -22,7 +22,6 @@ import java.time.ZonedDateTime;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Optional;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
@@ -662,22 +661,22 @@ public class Resource {
     }
 
     /**
-     * Check if the scene resource contains a 'status.active' element. If such an element is present, returns a Boolean
-     * Optional whose value depends on the value of that element, or an empty Optional if it is not.
+     * Check if the scene resource contains a 'status.active' element. Returns a Boolean if such an element is present,
+     * whose value depends on the value of that element, or null if it is not.
      *
-     * @return true, false, or empty.
+     * @return true, false, or null.
      */
-    public Optional<Boolean> getSceneActive() {
+    public @Nullable Boolean getSceneActive() {
         if (ResourceType.SCENE == getType()) {
             JsonElement status = this.status;
             if (Objects.nonNull(status) && status.isJsonObject()) {
                 JsonElement active = ((JsonObject) status).get("active");
                 if (Objects.nonNull(active) && active.isJsonPrimitive()) {
-                    return Optional.of(!"inactive".equalsIgnoreCase(active.getAsString()));
+                    return !"inactive".equalsIgnoreCase(active.getAsString());
                 }
             }
         }
-        return Optional.empty();
+        return null;
     }
 
     /**
@@ -688,42 +687,42 @@ public class Resource {
     }
 
     /**
-     * If the getSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result is
-     * present and 'true' (i.e. the scene is active) return the scene name. Or finally (the optional result is present
-     * and 'false') return 'UnDefType.UNDEF'.
+     * Depending on the returned value from getSceneActive() this method returns 'UnDefType.NULL' for 'null',
+     * 'UnDefType.UNDEF' for 'false' or when 'true' (i.e. the scene is active) return the scene name.
      *
-     * @return either 'UnDefType.NULL', a StringType containing the (active) scene name, or 'UnDefType.UNDEF'.
+     * @return either a StringType containing the (active) scene name, 'UnDefType.UNDEF' or 'UnDefType.NULL'.
      */
     public State getSceneState() {
-        return getSceneActive().map(a -> a ? new StringType(getName()) : UnDefType.UNDEF).orElse(UnDefType.NULL);
+        Boolean sceneActive = getSceneActive();
+        return sceneActive != null ? sceneActive ? new StringType(getName()) : UnDefType.UNDEF : UnDefType.NULL;
     }
 
     /**
      * Check if the smart scene resource contains a 'state' element. If such an element is present, returns a Boolean
-     * Optional whose value depends on the value of that element, or an empty Optional if it is not. Note that in some
-     * resource types the 'state' element is not a String primitive.
+     * whose value depends on the value of that element, or null if it is not.
      *
-     * @return true, false, or empty.
+     * @return true, false, or null.
      */
-    public Optional<Boolean> getSmartSceneActive() {
+    public @Nullable Boolean getSmartSceneActive() {
         if (ResourceType.SMART_SCENE == getType() && (state instanceof JsonPrimitive statePrimitive)) {
             String state = statePrimitive.getAsString();
             if (Objects.nonNull(state)) {
-                return Optional.of(SmartSceneState.ACTIVE == SmartSceneState.of(state));
+                return SmartSceneState.ACTIVE == SmartSceneState.of(state);
             }
         }
-        return Optional.empty();
+        return null;
     }
 
     /**
-     * If the getSmartSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result
-     * is present and 'true' (i.e. the scene is active) return the smart scene name. Or finally (the optional result is
-     * present and 'false') return 'UnDefType.UNDEF'.
+     * Depending on the returned value from getSmartSceneActive() this method returns 'UnDefType.NULL' for 'null',
+     * 'UnDefType.UNDEF' for 'false' or when 'true' (i.e. the scene is active) return the scene name.
      *
-     * @return either 'UnDefType.NULL', a StringType containing the (active) scene name, or 'UnDefType.UNDEF'.
+     * @return either a StringType containing the (active) scene name, 'UnDefType.UNDEF' or 'UnDefType.NULL'.
      */
     public State getSmartSceneState() {
-        return getSmartSceneActive().map(a -> a ? new StringType(getName()) : UnDefType.UNDEF).orElse(UnDefType.NULL);
+        Boolean smartSceneActive = getSmartSceneActive();
+        return smartSceneActive != null ? smartSceneActive ? new StringType(getName()) : UnDefType.UNDEF
+                : UnDefType.NULL;
     }
 
     public List<ResourceReference> getServiceReferences() {
index 13230ac9510f65f9536f50699af25406019a602b..59cd410a22b470323a434bfc0cb6c5850444465b 100644 (file)
@@ -485,9 +485,9 @@ public class Clip2Bridge implements Closeable {
             long delay;
             synchronized (Clip2Bridge.this) {
                 Instant now = Instant.now();
-                delay = lastRequestTime
+                delay = Objects.requireNonNull(lastRequestTime
                         .map(t -> Math.max(0, Duration.between(now, t).toMillis() + REQUEST_INTERVAL_MILLISECS))
-                        .orElse(0L);
+                        .orElse(0L));
                 lastRequestTime = Optional.of(now.plusMillis(delay));
             }
             Thread.sleep(delay);
index 1a69b30c4d931f2a8793e358447936b2e8962783..7944e5085f2abab1f315a154806606cf9a9194cd 100644 (file)
@@ -635,12 +635,15 @@ public class Clip2ThingHandler extends BaseThingHandler {
      * @param resources a collection of Resource objects containing the new state.
      */
     public void onResources(Collection<Resource> resources) {
-        boolean sceneActivated = resources.stream().anyMatch(r -> sceneContributorsCache.containsKey(r.getId())
-                && (r.getSceneActive().orElse(false) || r.getSmartSceneActive().orElse(false)));
+        boolean sceneActivated = resources.stream()
+                .anyMatch(r -> sceneContributorsCache.containsKey(r.getId())
+                        && (Objects.requireNonNullElse(r.getSceneActive(), false)
+                                || Objects.requireNonNullElse(r.getSmartSceneActive(), false)));
         for (Resource resource : resources) {
             // Skip scene deactivation when we have also received a scene activation.
             boolean updateChannels = !sceneActivated || !sceneContributorsCache.containsKey(resource.getId())
-                    || resource.getSceneActive().orElse(false) || resource.getSmartSceneActive().orElse(false);
+                    || Objects.requireNonNullElse(resource.getSceneActive(), false)
+                    || Objects.requireNonNullElse(resource.getSmartSceneActive(), false);
             onResource(resource, updateChannels);
         }
     }
@@ -1233,8 +1236,9 @@ public class Clip2ThingHandler extends BaseThingHandler {
                 sceneContributorsCache.putAll(scenes.stream().collect(Collectors.toMap(s -> s.getId(), s -> s)));
                 sceneResourceEntries.putAll(scenes.stream().collect(Collectors.toMap(s -> s.getName(), s -> s)));
 
-                State state = Objects.requireNonNull(scenes.stream().filter(s -> s.getSceneActive().orElse(false))
-                        .map(s -> s.getSceneState()).findAny().orElse(UnDefType.UNDEF));
+                State state = Objects.requireNonNull(
+                        scenes.stream().filter(s -> Objects.requireNonNullElse(s.getSceneActive(), false))
+                                .map(s -> s.getSceneState()).findAny().orElse(UnDefType.UNDEF));
 
                 // create scene channel if it is missing
                 if (getThing().getChannel(CHANNEL_2_SCENE) == null) {
index fc7be62844895678c82c2824971ec7be6ed81d6a..78b33e3d9ba3fa0b940dbb15969a21bf7f76d733 100644 (file)
@@ -24,7 +24,6 @@ import java.time.ZoneId;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -522,9 +521,9 @@ class Clip2DtoTest {
         ResourceType type = group.getType();
         assertNotNull(type);
         assertEquals(ResourceType.ROOM, type);
-        Optional<Boolean> state = item.getSmartSceneActive();
-        assertTrue(state.isPresent());
-        assertFalse(state.get());
+        Boolean state = item.getSmartSceneActive();
+        assertNotNull(state);
+        assertFalse(state);
     }
 
     @Test
@@ -613,9 +612,9 @@ class Clip2DtoTest {
         assertNotNull(active);
         assertTrue(active.isJsonPrimitive());
         assertEquals("inactive", active.getAsString());
-        Optional<Boolean> isActive = resource.getSceneActive();
-        assertTrue(isActive.isPresent());
-        assertEquals(Boolean.FALSE, isActive.get());
+        Boolean isActive = resource.getSceneActive();
+        assertNotNull(isActive);
+        assertEquals(Boolean.FALSE, isActive);
     }
 
     @Test