Skip to content

Commit

Permalink
Avoid double list allocations.
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Jan 7, 2022
1 parent e961587 commit 9c6a63a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
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.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
Expand Down Expand Up @@ -55,19 +55,19 @@ public class AutomationChannelBuilder {
private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);

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

public AutomationChannelBuilder(HDPowerViewTranslationProvider translationProvider,
ChannelGroupUID channelGroupUid) {
this.translationProvider = translationProvider;
this.channelGroupUid = channelGroupUid;
this.channels = new ArrayList<>(0);
this.scheduledEvents = new ArrayList<>(0);
this.scenes = new HashMap<>(0);
this.sceneCollections = new HashMap<>(0);
}

/**
Expand Down Expand Up @@ -134,6 +134,11 @@ public AutomationChannelBuilder withScheduledEvents(List<ScheduledEvent> schedul
* @return the {@link Channel} list
*/
public List<Channel> build() {
if (scheduledEvents == null || (scenes == null && sceneCollections == null)) {
return this.getChannelList(0);
}
List<ScheduledEvent> scheduledEvents = (@NonNull List<ScheduledEvent>) this.scheduledEvents;
List<Channel> channels = this.getChannelList(scheduledEvents.size());
scheduledEvents.stream().forEach(scheduledEvent -> {
Channel channel = createChannel(scheduledEvent);
if (channel != null) {
Expand All @@ -144,6 +149,10 @@ public List<Channel> build() {
return channels;
}

private List<Channel> getChannelList(int initialCapacity) {
return this.channels != null ? (@NonNull List<Channel>) this.channels : new ArrayList<>(initialCapacity);
}

private @Nullable Channel createChannel(ScheduledEvent scheduledEvent) {
String referencedName = getReferencedSceneOrSceneCollectionName(scheduledEvent);
if (referencedName == null) {
Expand All @@ -161,14 +170,26 @@ public List<Channel> build() {

private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent) {
if (scheduledEvent.sceneId > 0) {
Scene scene = scenes.get(scheduledEvent.sceneId);
if (scenes == null) {
logger.warn("Scheduled event '{}' references scene '{}', but no scenes are loaded", scheduledEvent.id,
scheduledEvent.sceneId);
return null;
}
Scene scene = ((@NonNull Map<Integer, Scene>) scenes).get(scheduledEvent.sceneId);
if (scene != null) {
return scene.getName();
}
logger.warn("Scene '{}' was not found for scheduled event '{}'", scheduledEvent.sceneId, scheduledEvent.id);
return null;
} else if (scheduledEvent.sceneCollectionId > 0) {
SceneCollection sceneCollection = sceneCollections.get(scheduledEvent.sceneCollectionId);
if (sceneCollections == null) {
logger.warn(
"Scheduled event '{}' references scene collection '{}', but no scene collections are loaded",
scheduledEvent.id, scheduledEvent.sceneCollectionId);
return null;
}
SceneCollection sceneCollection = ((@NonNull Map<Integer, SceneCollection>) sceneCollections)
.get(scheduledEvent.sceneCollectionId);
if (sceneCollection != null) {
return sceneCollection.getName();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
import org.openhab.binding.hdpowerview.internal.api.responses.Scenes.Scene;
Expand All @@ -40,14 +42,14 @@ public class SceneChannelBuilder {
private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);

@Nullable
private List<Channel> channels;
@Nullable
private List<Scene> scenes;

public SceneChannelBuilder(HDPowerViewTranslationProvider translationProvider, ChannelGroupUID channelGroupUid) {
this.translationProvider = translationProvider;
this.channelGroupUid = channelGroupUid;
this.channels = new ArrayList<>(0);
this.scenes = new ArrayList<>(0);
}

/**
Expand Down Expand Up @@ -91,10 +93,19 @@ public SceneChannelBuilder withScenes(List<Scene> scenes) {
* @return the {@link Channel} list
*/
public List<Channel> build() {
if (scenes == null) {
return this.getChannelList(0);
}
List<Scene> scenes = (@NonNull List<Scene>) this.scenes;
List<Channel> channels = this.getChannelList(scenes.size());
scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene)));
return channels;
}

private List<Channel> getChannelList(int initialCapacity) {
return this.channels != null ? (@NonNull List<Channel>) this.channels : new ArrayList<>(initialCapacity);
}

private Channel createChannel(Scene scene) {
ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(scene.id));
String description = translationProvider.getText("dynamic-channel.scene-activate.description", scene.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
import org.openhab.binding.hdpowerview.internal.HDPowerViewTranslationProvider;
import org.openhab.binding.hdpowerview.internal.api.responses.SceneCollections.SceneCollection;
Expand All @@ -40,15 +42,15 @@ public class SceneGroupChannelBuilder {
private final ChannelTypeUID channelTypeUid = new ChannelTypeUID(HDPowerViewBindingConstants.BINDING_ID,
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_GROUP_ACTIVATE);

@Nullable
private List<Channel> channels;
@Nullable
private List<SceneCollection> sceneCollections;

public SceneGroupChannelBuilder(HDPowerViewTranslationProvider translationProvider,
ChannelGroupUID channelGroupUid) {
this.translationProvider = translationProvider;
this.channelGroupUid = channelGroupUid;
this.channels = new ArrayList<>(0);
this.sceneCollections = new ArrayList<>(0);
}

/**
Expand Down Expand Up @@ -92,10 +94,19 @@ public SceneGroupChannelBuilder withSceneCollections(List<SceneCollection> scene
* @return the {@link Channel} list
*/
public List<Channel> build() {
if (sceneCollections == null) {
return this.getChannelList(0);
}
List<SceneCollection> sceneCollections = (@NonNull List<SceneCollection>) this.sceneCollections;
List<Channel> channels = this.getChannelList(sceneCollections.size());
sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection)));
return channels;
}

private List<Channel> getChannelList(int initialCapacity) {
return this.channels != null ? (@NonNull List<Channel>) this.channels : new ArrayList<>(initialCapacity);
}

private Channel createChannel(SceneCollection sceneCollection) {
ChannelUID channelUid = new ChannelUID(channelGroupUid, Integer.toString(sceneCollection.id));
String description = translationProvider.getText("dynamic-channel.scene-group-activate.description",
Expand Down

0 comments on commit 9c6a63a

Please sign in to comment.