From ec5df4d65e6b6c098c2f9b35329ed1a10c5e4c06 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Tue, 20 Aug 2024 00:01:10 +0200 Subject: [PATCH] [hue] Fix compile warnings (#17293) Signed-off-by: Leo Siepel Signed-off-by: Patrik Gfeller --- .../hue/internal/api/dto/clip2/Resource.java | 45 +++++++++---------- .../hue/internal/connection/Clip2Bridge.java | 4 +- .../internal/handler/Clip2ThingHandler.java | 14 +++--- .../hue/internal/clip2/Clip2DtoTest.java | 13 +++--- 4 files changed, 39 insertions(+), 37 deletions(-) diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java index 8adb111a2d4b0..c05594aa8df43 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java @@ -22,7 +22,6 @@ 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 State getRotaryStepsLastUpdatedState(ZoneId zoneId) { } /** - * 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 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 Optional getSceneActive() { } /** - * 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 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 getServiceReferences() { diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/connection/Clip2Bridge.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/connection/Clip2Bridge.java index 13230ac9510f6..59cd410a22b47 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/connection/Clip2Bridge.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/connection/Clip2Bridge.java @@ -485,9 +485,9 @@ private class Throttler implements AutoCloseable { 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); diff --git a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java index 1a69b30c4d931..7944e5085f2ab 100644 --- a/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java +++ b/bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java @@ -635,12 +635,15 @@ public void initialize() { * @param resources a collection of Resource objects containing the new state. */ public void onResources(Collection 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 synchronized boolean updateSceneContributors(List allScenes) { 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) { diff --git a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/clip2/Clip2DtoTest.java b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/clip2/Clip2DtoTest.java index fc7be62844895..78b33e3d9ba3f 100644 --- a/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/clip2/Clip2DtoTest.java +++ b/bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/clip2/Clip2DtoTest.java @@ -24,7 +24,6 @@ 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 @@ void testSmartScene() { ResourceType type = group.getType(); assertNotNull(type); assertEquals(ResourceType.ROOM, type); - Optional state = item.getSmartSceneActive(); - assertTrue(state.isPresent()); - assertFalse(state.get()); + Boolean state = item.getSmartSceneActive(); + assertNotNull(state); + assertFalse(state); } @Test @@ -613,9 +612,9 @@ void testSseSceneEvent() { assertNotNull(active); assertTrue(active.isJsonPrimitive()); assertEquals("inactive", active.getAsString()); - Optional isActive = resource.getSceneActive(); - assertTrue(isActive.isPresent()); - assertEquals(Boolean.FALSE, isActive.get()); + Boolean isActive = resource.getSceneActive(); + assertNotNull(isActive); + assertEquals(Boolean.FALSE, isActive); } @Test