diff --git a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java index dd1f6c8f17fc1..fbf75e0069423 100644 --- a/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java +++ b/bundles/org.openhab.binding.hdpowerview/src/main/java/org/openhab/binding/hdpowerview/internal/builders/AutomationChannelBuilder.java @@ -17,8 +17,11 @@ import java.time.format.TextStyle; import java.util.ArrayList; import java.util.EnumSet; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.StringJoiner; +import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; @@ -53,8 +56,8 @@ public class AutomationChannelBuilder { HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED); private List channels; - private List scenes; - private List sceneCollections; + private Map scenes; + private Map sceneCollections; private List scheduledEvents; public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider, @@ -63,8 +66,8 @@ public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvid this.channelGroupUid = channelGroupUid; this.channels = new ArrayList<>(0); this.scheduledEvents = new ArrayList<>(0); - this.scenes = new ArrayList<>(0); - this.sceneCollections = new ArrayList<>(0); + this.scenes = new HashMap<>(0); + this.sceneCollections = new HashMap<>(0); } /** @@ -98,7 +101,7 @@ public AutomationChannelBuilder withChannels(List channels) { * @return channel builder */ public AutomationChannelBuilder withScenes(List scenes) { - this.scenes = scenes; + this.scenes = scenes.stream().collect(Collectors.toMap(scene -> scene.id, scene -> scene)); return this; } @@ -109,7 +112,8 @@ public AutomationChannelBuilder withScenes(List scenes) { * @return channel builder */ public AutomationChannelBuilder withSceneCollections(List sceneCollections) { - this.sceneCollections = sceneCollections; + this.sceneCollections = sceneCollections.stream() + .collect(Collectors.toMap(sceneCollection -> sceneCollection.id, sceneCollection -> sceneCollection)); return this; } @@ -131,7 +135,7 @@ public AutomationChannelBuilder withScheduledEvents(List schedul */ public List build() { scheduledEvents.stream().forEach(scheduledEvent -> { - Channel channel = createChannel(scheduledEvent, scenes, sceneCollections); + Channel channel = createChannel(scheduledEvent); if (channel != null) { channels.add(channel); } @@ -140,9 +144,8 @@ public List build() { return channels; } - private @Nullable Channel createChannel(ScheduledEvent scheduledEvent, List scenes, - List sceneCollections) { - String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent, scenes, sceneCollections); + private @Nullable Channel createChannel(ScheduledEvent scheduledEvent) { + String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent); if (referencedName == null) { return null; } @@ -156,22 +159,19 @@ public List build() { return channel; } - private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent, List scenes, - List sceneCollections) { + private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent) { if (scheduledEvent.sceneId > 0) { - for (Scene scene : scenes) { - if (scene.id == scheduledEvent.sceneId) { - return scene.getName(); - } + Scene scene = scenes.get(scheduledEvent.sceneId); + if (scene != null) { + return scene.getName(); } logger.error("Scene '{}' was not found for scheduled event '{}'", scheduledEvent.sceneId, scheduledEvent.id); return null; } else if (scheduledEvent.sceneCollectionId > 0) { - for (SceneCollection sceneCollection : sceneCollections) { - if (sceneCollection.id == scheduledEvent.sceneCollectionId) { - return sceneCollection.getName(); - } + SceneCollection sceneCollection = sceneCollections.get(scheduledEvent.sceneCollectionId); + if (sceneCollection != null) { + return sceneCollection.getName(); } logger.error("Scene collection '{}' was not found for scheduled event '{}'", scheduledEvent.sceneCollectionId, scheduledEvent.id);