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;
}
/**
- * 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;
}
/**
}
/**
- * 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() {
* @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);
}
}
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) {
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;
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
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