Skip to content

Commit

Permalink
[hue] Fix compile warnings (openhab#17293)
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Siepel <[email protected]>
Signed-off-by: Patrik Gfeller <[email protected]>
  • Loading branch information
lsiepel authored and pgfeller committed Sep 29, 2024
1 parent ede0e8a commit ec5df4d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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;
}

/**
Expand All @@ -688,42 +687,42 @@ public Optional<Boolean> 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<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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,15 @@ public void initialize() {
* @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);
}
}
Expand Down Expand Up @@ -1233,8 +1236,9 @@ public synchronized boolean updateSceneContributors(List<Resource> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -522,9 +521,9 @@ void testSmartScene() {
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
Expand Down Expand Up @@ -613,9 +612,9 @@ void testSseSceneEvent() {
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
Expand Down

0 comments on commit ec5df4d

Please sign in to comment.