Skip to content

Commit

Permalink
Fix null annotation issues.
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Jan 15, 2022
1 parent a763e97 commit e7a70f5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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 @@ -125,10 +124,10 @@ public AutomationChannelBuilder withScheduledEvents(List<ScheduledEvent> schedul
* @return the {@link Channel} list
*/
public List<Channel> build() {
List<ScheduledEvent> scheduledEvents = this.scheduledEvents;
if (scheduledEvents == null || (scenes == null && sceneCollections == null)) {
return getChannelList(0);
}
List<ScheduledEvent> scheduledEvents = (@NonNull List<ScheduledEvent>) this.scheduledEvents;
List<Channel> channels = getChannelList(scheduledEvents.size());
scheduledEvents.stream().forEach(scheduledEvent -> {
Channel channel = createChannel(scheduledEvent);
Expand Down Expand Up @@ -157,26 +156,27 @@ public List<Channel> build() {

private @Nullable String getReferencedSceneOrSceneCollectionName(ScheduledEvent scheduledEvent) {
if (scheduledEvent.sceneId > 0) {
Map<Integer, Scene> scenes = this.scenes;
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);
Scene 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) {
Map<Integer, SceneCollection> sceneCollections = this.sceneCollections;
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);
SceneCollection 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,6 @@
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;
Expand Down Expand Up @@ -48,6 +47,7 @@ protected BaseChannelBuilder(HDPowerViewTranslationProvider translationProvider,
}

protected List<Channel> getChannelList(int initialCapacity) {
return channels != null ? (@NonNull List<Channel>) channels : new ArrayList<>(initialCapacity);
List<Channel> channels = this.channels;
return channels != null ? channels : new ArrayList<>(initialCapacity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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;
Expand Down Expand Up @@ -83,10 +82,10 @@ public SceneChannelBuilder withScenes(List<Scene> scenes) {
* @return the {@link Channel} list
*/
public List<Channel> build() {
List<Scene> scenes = this.scenes;
if (scenes == null) {
return getChannelList(0);
}
List<Scene> scenes = (@NonNull List<Scene>) this.scenes;
List<Channel> channels = getChannelList(scenes.size());
scenes.stream().sorted().forEach(scene -> channels.add(createChannel(scene)));
return channels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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;
Expand Down Expand Up @@ -84,10 +83,10 @@ public SceneGroupChannelBuilder withSceneCollections(List<SceneCollection> scene
* @return the {@link Channel} list
*/
public List<Channel> build() {
List<SceneCollection> sceneCollections = this.sceneCollections;
if (sceneCollections == null) {
return getChannelList(0);
}
List<SceneCollection> sceneCollections = (@NonNull List<SceneCollection>) this.sceneCollections;
List<Channel> channels = getChannelList(sceneCollections.size());
sceneCollections.stream().sorted().forEach(sceneCollection -> channels.add(createChannel(sceneCollection)));
return channels;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Base64;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
Expand All @@ -39,21 +39,21 @@
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class AutomationChannelBuilderTest {

private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, AutomationChannelBuilderTest.class.getSimpleName()),
HDPowerViewBindingConstants.CHANNELTYPE_AUTOMATION_ENABLED);

private HDPowerViewTranslationProvider translationProvider;
private AutomationChannelBuilder builder;
private List<Scene> scenes;
private List<SceneCollection> sceneCollections;
private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
private AutomationChannelBuilder builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
private List<Scene> scenes = new ArrayList<>();
private List<SceneCollection> sceneCollections = new ArrayList<>();

@BeforeEach
private void setUp() {
translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(),
new LocaleProviderForTests());
builder = AutomationChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);

Scene scene = new Scene();
Expand All @@ -74,9 +74,7 @@ public void sceneSunriseWeekends() {
scheduledEvent.daySunday = true;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, At sunrise, Weekends", channels.get(0).getLabel());
Expand All @@ -92,9 +90,7 @@ public void sceneSunsetWeekdays() {
scheduledEvent.dayFriday = true;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, At sunset, Weekdays", channels.get(0).getLabel());
Expand All @@ -114,9 +110,7 @@ public void sceneTimeAllDays() {
scheduledEvent.minute = 30;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, 06:30, All days", channels.get(0).getLabel());
Expand All @@ -130,9 +124,7 @@ public void sceneMinutesBeforeSunriseMondayTuesday() {
scheduledEvent.minute = -15;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, 15m before sunrise, Mon, Tue", channels.get(0).getLabel());
Expand All @@ -145,9 +137,7 @@ public void sceneHoursMinutesAfterSunriseMonday() {
scheduledEvent.minute = 61;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, 1hr 1m after sunrise, Mon", channels.get(0).getLabel());
Expand All @@ -162,9 +152,7 @@ public void sceneMinutesBeforeSunsetWednesdayThursdayFriday() {
scheduledEvent.minute = -59;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, 59m before sunset, Wed, Thu, Fri", channels.get(0).getLabel());
Expand All @@ -179,9 +167,7 @@ public void sceneHourAfterSunsetFridaySaturdaySunday() {
scheduledEvent.minute = 60;

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals("TestScene, 1hr after sunset, Fri, Sat, Sun", channels.get(0).getLabel());
Expand All @@ -193,10 +179,8 @@ public void sceneCollection() {
ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);

List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections)
.withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents)
.build();

assertEquals(1, channels.size());
assertEquals("TestSceneCollection, At sunrise, ", channels.get(0).getLabel());
Expand All @@ -207,18 +191,15 @@ public void suppliedListIsUsed() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));
List<Channel> existingChannels = new ArrayList<>(0);

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents)
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents)
.withChannels(existingChannels).build();

assertEquals(existingChannels, channels);
}

@Test
public void emptyListWhenNoScheduledEvents() {
@NonNull
List<@NonNull Channel> channels = builder.build();
List<Channel> channels = builder.build();

assertEquals(0, channels.size());
}
Expand All @@ -227,9 +208,7 @@ public void emptyListWhenNoScheduledEvents() {
public void emptyListWhenNoScenesOrSceneCollections() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScheduledEvents(scheduledEvents).build();

assertEquals(0, channels.size());
}
Expand All @@ -239,9 +218,7 @@ public void emptyListWhenNoSceneForScheduledEvent() {
ScheduledEvent scheduledEvent = createScheduledEventWithSceneCollection(
ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(0, channels.size());
}
Expand All @@ -251,9 +228,8 @@ public void emptyListWhenNoSceneCollectionForScheduledEvent() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withSceneCollections(sceneCollections)
.withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withSceneCollections(sceneCollections).withScheduledEvents(scheduledEvents)
.build();

assertEquals(0, channels.size());
}
Expand All @@ -263,9 +239,7 @@ public void groupAndIdAreCorrect() {
ScheduledEvent scheduledEvent = createScheduledEventWithScene(ScheduledEvents.SCHEDULED_EVENT_TYPE_SUNRISE);
scheduledEvent.id = 42;
List<ScheduledEvent> scheduledEvents = new ArrayList<>(List.of(scheduledEvent));

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();
List<Channel> channels = builder.withScenes(scenes).withScheduledEvents(scheduledEvents).build();

assertEquals(1, channels.size());
assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Base64;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants;
Expand All @@ -36,28 +36,26 @@
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class SceneChannelBuilderTest {

private static final ChannelGroupUID CHANNEL_GROUP_UID = new ChannelGroupUID(
new ThingUID(HDPowerViewBindingConstants.BINDING_ID, SceneChannelBuilderTest.class.getSimpleName()),
HDPowerViewBindingConstants.CHANNELTYPE_SCENE_ACTIVATE);

private HDPowerViewTranslationProvider translationProvider;
private SceneChannelBuilder builder;
private static final HDPowerViewTranslationProvider translationProvider = new HDPowerViewTranslationProvider(
mock(Bundle.class), new TranslationProviderForTests(), new LocaleProviderForTests());
private SceneChannelBuilder builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);

@BeforeEach
private void setUp() {
translationProvider = new HDPowerViewTranslationProvider(mock(Bundle.class), new TranslationProviderForTests(),
new LocaleProviderForTests());
builder = SceneChannelBuilder.create(translationProvider, CHANNEL_GROUP_UID);
}

@Test
public void labelIsCorrect() {
List<Scene> scenes = createScenes();

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).build();
List<Channel> channels = builder.withScenes(scenes).build();

assertEquals(1, channels.size());
assertEquals("TestScene", channels.get(0).getLabel());
Expand All @@ -66,9 +64,7 @@ public void labelIsCorrect() {
@Test
public void descriptionIsCorrect() {
List<Scene> scenes = createScenes();

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).build();
List<Channel> channels = builder.withScenes(scenes).build();

assertEquals(1, channels.size());
assertEquals("Activates the scene 'TestScene'", channels.get(0).getDescription());
Expand All @@ -77,9 +73,7 @@ public void descriptionIsCorrect() {
@Test
public void groupAndIdAreCorrect() {
List<Scene> scenes = createScenes();

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).build();
List<Channel> channels = builder.withScenes(scenes).build();

assertEquals(1, channels.size());
assertEquals(CHANNEL_GROUP_UID.getId(), channels.get(0).getUID().getGroupId());
Expand All @@ -90,17 +84,14 @@ public void groupAndIdAreCorrect() {
public void suppliedListIsUsed() {
List<Scene> scenes = createScenes();
List<Channel> existingChannels = new ArrayList<>(0);

@NonNull
List<@NonNull Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();
List<Channel> channels = builder.withScenes(scenes).withChannels(existingChannels).build();

assertEquals(existingChannels, channels);
}

@Test
public void emptyListWhenNoScenes() {
@NonNull
List<@NonNull Channel> channels = builder.build();
List<Channel> channels = builder.build();

assertEquals(0, channels.size());
}
Expand Down
Loading

0 comments on commit e7a70f5

Please sign in to comment.