Skip to content

Commit

Permalink
Optimize scene/scene collection lookups.
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Jan 4, 2022
1 parent 20ac9cd commit 40fff4d
Showing 1 changed file with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -53,8 +56,8 @@ public class AutomationChannelBuilder {
HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);

private List<Channel> channels;
private List<Scene> scenes;
private List<SceneCollection> sceneCollections;
private Map<Integer, Scene> scenes;
private Map<Integer, SceneCollection> sceneCollections;
private List<ScheduledEvent> scheduledEvents;

public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider,
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -98,7 +101,7 @@ public AutomationChannelBuilder withChannels(List<Channel> channels) {
* @return channel builder
*/
public AutomationChannelBuilder withScenes(List<Scene> scenes) {
this.scenes = scenes;
this.scenes = scenes.stream().collect(Collectors.toMap(scene -> scene.id, scene -> scene));
return this;
}

Expand All @@ -109,7 +112,8 @@ public AutomationChannelBuilder withScenes(List<Scene> scenes) {
* @return channel builder
*/
public AutomationChannelBuilder withSceneCollections(List<SceneCollection> sceneCollections) {
this.sceneCollections = sceneCollections;
this.sceneCollections = sceneCollections.stream()
.collect(Collectors.toMap(sceneCollection -> sceneCollection.id, sceneCollection -> sceneCollection));
return this;
}

Expand All @@ -131,7 +135,7 @@ public AutomationChannelBuilder withScheduledEvents(List<ScheduledEvent> schedul
*/
public List<Channel> build() {
scheduledEvents.stream().forEach(scheduledEvent -> {
Channel channel = createChannel(scheduledEvent, scenes, sceneCollections);
Channel channel = createChannel(scheduledEvent);
if (channel != null) {
channels.add(channel);
}
Expand All @@ -140,9 +144,8 @@ public List<Channel> build() {
return channels;
}

private @Nullable Channel createChannel(ScheduledEvent scheduledEvent, List<Scene> scenes,
List<SceneCollection> sceneCollections) {
String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent, scenes, sceneCollections);
private @Nullable Channel createChannel(ScheduledEvent scheduledEvent) {
String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent);
if (referencedName == null) {
return null;
}
Expand All @@ -156,22 +159,19 @@ public List<Channel> build() {
return channel;
}

private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent, List<Scene> scenes,
List<SceneCollection> 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);
Expand Down

0 comments on commit 40fff4d

Please sign in to comment.