Skip to content

Commit ba6cef3

Browse files
authored
[hue] Fix compile warnings (openhab#17293)
Signed-off-by: Leo Siepel <[email protected]>
1 parent 77561d5 commit ba6cef3

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/api/dto/clip2/Resource.java

+22-23
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Objects;
25-
import java.util.Optional;
2625

2726
import org.eclipse.jdt.annotation.NonNullByDefault;
2827
import org.eclipse.jdt.annotation.Nullable;
@@ -662,22 +661,22 @@ public State getRotaryStepsLastUpdatedState(ZoneId zoneId) {
662661
}
663662

664663
/**
665-
* Check if the scene resource contains a 'status.active' element. If such an element is present, returns a Boolean
666-
* Optional whose value depends on the value of that element, or an empty Optional if it is not.
664+
* Check if the scene resource contains a 'status.active' element. Returns a Boolean if such an element is present,
665+
* whose value depends on the value of that element, or null if it is not.
667666
*
668-
* @return true, false, or empty.
667+
* @return true, false, or null.
669668
*/
670-
public Optional<Boolean> getSceneActive() {
669+
public @Nullable Boolean getSceneActive() {
671670
if (ResourceType.SCENE == getType()) {
672671
JsonElement status = this.status;
673672
if (Objects.nonNull(status) && status.isJsonObject()) {
674673
JsonElement active = ((JsonObject) status).get("active");
675674
if (Objects.nonNull(active) && active.isJsonPrimitive()) {
676-
return Optional.of(!"inactive".equalsIgnoreCase(active.getAsString()));
675+
return !"inactive".equalsIgnoreCase(active.getAsString());
677676
}
678677
}
679678
}
680-
return Optional.empty();
679+
return null;
681680
}
682681

683682
/**
@@ -688,42 +687,42 @@ public Optional<Boolean> getSceneActive() {
688687
}
689688

690689
/**
691-
* If the getSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result is
692-
* present and 'true' (i.e. the scene is active) return the scene name. Or finally (the optional result is present
693-
* and 'false') return 'UnDefType.UNDEF'.
690+
* Depending on the returned value from getSceneActive() this method returns 'UnDefType.NULL' for 'null',
691+
* 'UnDefType.UNDEF' for 'false' or when 'true' (i.e. the scene is active) return the scene name.
694692
*
695-
* @return either 'UnDefType.NULL', a StringType containing the (active) scene name, or 'UnDefType.UNDEF'.
693+
* @return either a StringType containing the (active) scene name, 'UnDefType.UNDEF' or 'UnDefType.NULL'.
696694
*/
697695
public State getSceneState() {
698-
return getSceneActive().map(a -> a ? new StringType(getName()) : UnDefType.UNDEF).orElse(UnDefType.NULL);
696+
Boolean sceneActive = getSceneActive();
697+
return sceneActive != null ? sceneActive ? new StringType(getName()) : UnDefType.UNDEF : UnDefType.NULL;
699698
}
700699

701700
/**
702701
* Check if the smart scene resource contains a 'state' element. If such an element is present, returns a Boolean
703-
* Optional whose value depends on the value of that element, or an empty Optional if it is not. Note that in some
704-
* resource types the 'state' element is not a String primitive.
702+
* whose value depends on the value of that element, or null if it is not.
705703
*
706-
* @return true, false, or empty.
704+
* @return true, false, or null.
707705
*/
708-
public Optional<Boolean> getSmartSceneActive() {
706+
public @Nullable Boolean getSmartSceneActive() {
709707
if (ResourceType.SMART_SCENE == getType() && (state instanceof JsonPrimitive statePrimitive)) {
710708
String state = statePrimitive.getAsString();
711709
if (Objects.nonNull(state)) {
712-
return Optional.of(SmartSceneState.ACTIVE == SmartSceneState.of(state));
710+
return SmartSceneState.ACTIVE == SmartSceneState.of(state);
713711
}
714712
}
715-
return Optional.empty();
713+
return null;
716714
}
717715

718716
/**
719-
* If the getSmartSceneActive() optional result is empty return 'UnDefType.NULL'. Otherwise if the optional result
720-
* is present and 'true' (i.e. the scene is active) return the smart scene name. Or finally (the optional result is
721-
* present and 'false') return 'UnDefType.UNDEF'.
717+
* Depending on the returned value from getSmartSceneActive() this method returns 'UnDefType.NULL' for 'null',
718+
* 'UnDefType.UNDEF' for 'false' or when 'true' (i.e. the scene is active) return the scene name.
722719
*
723-
* @return either 'UnDefType.NULL', a StringType containing the (active) scene name, or 'UnDefType.UNDEF'.
720+
* @return either a StringType containing the (active) scene name, 'UnDefType.UNDEF' or 'UnDefType.NULL'.
724721
*/
725722
public State getSmartSceneState() {
726-
return getSmartSceneActive().map(a -> a ? new StringType(getName()) : UnDefType.UNDEF).orElse(UnDefType.NULL);
723+
Boolean smartSceneActive = getSmartSceneActive();
724+
return smartSceneActive != null ? smartSceneActive ? new StringType(getName()) : UnDefType.UNDEF
725+
: UnDefType.NULL;
727726
}
728727

729728
public List<ResourceReference> getServiceReferences() {

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/connection/Clip2Bridge.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ private class Throttler implements AutoCloseable {
485485
long delay;
486486
synchronized (Clip2Bridge.this) {
487487
Instant now = Instant.now();
488-
delay = lastRequestTime
488+
delay = Objects.requireNonNull(lastRequestTime
489489
.map(t -> Math.max(0, Duration.between(now, t).toMillis() + REQUEST_INTERVAL_MILLISECS))
490-
.orElse(0L);
490+
.orElse(0L));
491491
lastRequestTime = Optional.of(now.plusMillis(delay));
492492
}
493493
Thread.sleep(delay);

bundles/org.openhab.binding.hue/src/main/java/org/openhab/binding/hue/internal/handler/Clip2ThingHandler.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -635,12 +635,15 @@ public void initialize() {
635635
* @param resources a collection of Resource objects containing the new state.
636636
*/
637637
public void onResources(Collection<Resource> resources) {
638-
boolean sceneActivated = resources.stream().anyMatch(r -> sceneContributorsCache.containsKey(r.getId())
639-
&& (r.getSceneActive().orElse(false) || r.getSmartSceneActive().orElse(false)));
638+
boolean sceneActivated = resources.stream()
639+
.anyMatch(r -> sceneContributorsCache.containsKey(r.getId())
640+
&& (Objects.requireNonNullElse(r.getSceneActive(), false)
641+
|| Objects.requireNonNullElse(r.getSmartSceneActive(), false)));
640642
for (Resource resource : resources) {
641643
// Skip scene deactivation when we have also received a scene activation.
642644
boolean updateChannels = !sceneActivated || !sceneContributorsCache.containsKey(resource.getId())
643-
|| resource.getSceneActive().orElse(false) || resource.getSmartSceneActive().orElse(false);
645+
|| Objects.requireNonNullElse(resource.getSceneActive(), false)
646+
|| Objects.requireNonNullElse(resource.getSmartSceneActive(), false);
644647
onResource(resource, updateChannels);
645648
}
646649
}
@@ -1233,8 +1236,9 @@ public synchronized boolean updateSceneContributors(List<Resource> allScenes) {
12331236
sceneContributorsCache.putAll(scenes.stream().collect(Collectors.toMap(s -> s.getId(), s -> s)));
12341237
sceneResourceEntries.putAll(scenes.stream().collect(Collectors.toMap(s -> s.getName(), s -> s)));
12351238

1236-
State state = Objects.requireNonNull(scenes.stream().filter(s -> s.getSceneActive().orElse(false))
1237-
.map(s -> s.getSceneState()).findAny().orElse(UnDefType.UNDEF));
1239+
State state = Objects.requireNonNull(
1240+
scenes.stream().filter(s -> Objects.requireNonNullElse(s.getSceneActive(), false))
1241+
.map(s -> s.getSceneState()).findAny().orElse(UnDefType.UNDEF));
12381242

12391243
// create scene channel if it is missing
12401244
if (getThing().getChannel(CHANNEL_2_SCENE) == null) {

bundles/org.openhab.binding.hue/src/test/java/org/openhab/binding/hue/internal/clip2/Clip2DtoTest.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626
import java.util.Map;
27-
import java.util.Optional;
2827
import java.util.Set;
2928

3029
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -522,9 +521,9 @@ void testSmartScene() {
522521
ResourceType type = group.getType();
523522
assertNotNull(type);
524523
assertEquals(ResourceType.ROOM, type);
525-
Optional<Boolean> state = item.getSmartSceneActive();
526-
assertTrue(state.isPresent());
527-
assertFalse(state.get());
524+
Boolean state = item.getSmartSceneActive();
525+
assertNotNull(state);
526+
assertFalse(state);
528527
}
529528

530529
@Test
@@ -613,9 +612,9 @@ void testSseSceneEvent() {
613612
assertNotNull(active);
614613
assertTrue(active.isJsonPrimitive());
615614
assertEquals("inactive", active.getAsString());
616-
Optional<Boolean> isActive = resource.getSceneActive();
617-
assertTrue(isActive.isPresent());
618-
assertEquals(Boolean.FALSE, isActive.get());
615+
Boolean isActive = resource.getSceneActive();
616+
assertNotNull(isActive);
617+
assertEquals(Boolean.FALSE, isActive);
619618
}
620619

621620
@Test

0 commit comments

Comments
 (0)