From 1210fec9f9d7d1e22489a0b620d7958ec82f0a95 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Sun, 7 Feb 2021 11:43:17 +0100 Subject: [PATCH] Adjustments for nullness annotated TypeParser (#10068) Signed-off-by: Christoph Weitkamp --- .../handler/HomieThingHandlerTests.java | 16 +++++----- .../handler/NetworkOceanicThingHandler.java | 13 +++++---- .../internal/handler/OceanicThingHandler.java | 29 +++++++++---------- .../internal/SmartMeterHandler.java | 14 +++++---- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandlerTests.java b/bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandlerTests.java index cc270a63e..708c12bd3 100644 --- a/bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandlerTests.java +++ b/bundles/org.openhab.binding.mqtt.homie/src/test/java/org/openhab/binding/mqtt/homie/internal/handler/HomieThingHandlerTests.java @@ -267,13 +267,15 @@ public class HomieThingHandlerTests { // Assign old value Value value = property.getChannelState().getCache(); Command command = TypeParser.parseCommand(value.getSupportedCommandTypes(), "OLDVALUE"); - property.getChannelState().getCache().update(command); - // Try to update with new value - updateValue = new StringType("SOMETHINGNEW"); - thingHandler.handleCommand(property.channelUID, updateValue); - // Expect old value and no MQTT publish - assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE")); - verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean()); + if (command != null) { + property.getChannelState().getCache().update(command); + // Try to update with new value + updateValue = new StringType("SOMETHINGNEW"); + thingHandler.handleCommand(property.channelUID, updateValue); + // Expect old value and no MQTT publish + assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE")); + verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean()); + } } public Object createSubscriberAnswer(InvocationOnMock invocation) { diff --git a/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/NetworkOceanicThingHandler.java b/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/NetworkOceanicThingHandler.java index 4ef563431..f968b1618 100644 --- a/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/NetworkOceanicThingHandler.java +++ b/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/NetworkOceanicThingHandler.java @@ -22,6 +22,8 @@ import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; import org.apache.commons.lang.StringUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.oceanic.internal.NetworkOceanicBindingConfiguration; import org.openhab.binding.oceanic.internal.Throttler; import org.openhab.core.thing.Thing; @@ -36,6 +38,7 @@ import org.slf4j.LoggerFactory; * * @author Karel Goderis - Initial contribution */ +@NonNullByDefault public class NetworkOceanicThingHandler extends OceanicThingHandler { private static final int REQUEST_TIMEOUT = 3000; @@ -43,10 +46,10 @@ public class NetworkOceanicThingHandler extends OceanicThingHandler { private final Logger logger = LoggerFactory.getLogger(NetworkOceanicThingHandler.class); - private Socket socket; - private InputStream inputStream; - private OutputStream outputStream; - protected ScheduledFuture reconnectJob; + private @Nullable Socket socket; + private @Nullable InputStream inputStream; + private @Nullable OutputStream outputStream; + protected @Nullable ScheduledFuture reconnectJob; public NetworkOceanicThingHandler(Thing thing) { super(thing); @@ -99,7 +102,7 @@ public class NetworkOceanicThingHandler extends OceanicThingHandler { } @Override - protected String requestResponse(String commandAsString) { + protected @Nullable String requestResponse(String commandAsString) { synchronized (this) { if (getThing().getStatus() == ThingStatus.ONLINE) { NetworkOceanicBindingConfiguration config = getConfigAs(NetworkOceanicBindingConfiguration.class); diff --git a/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/OceanicThingHandler.java b/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/OceanicThingHandler.java index ff02b26a4..af944440e 100644 --- a/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/OceanicThingHandler.java +++ b/bundles/org.openhab.binding.oceanic/src/main/java/org/openhab/binding/oceanic/internal/handler/OceanicThingHandler.java @@ -13,13 +13,13 @@ package org.openhab.binding.oceanic.internal.handler; import java.math.BigDecimal; -import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.oceanic.internal.OceanicBindingConstants.OceanicChannelSelector; import org.openhab.core.thing.Channel; import org.openhab.core.thing.ChannelUID; @@ -30,7 +30,6 @@ import org.openhab.core.thing.binding.BaseThingHandler; import org.openhab.core.types.Command; import org.openhab.core.types.RefreshType; import org.openhab.core.types.State; -import org.openhab.core.types.Type; import org.openhab.core.types.TypeParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,6 +40,7 @@ import org.slf4j.LoggerFactory; * * @author Karel Goderis - Initial contribution */ +@NonNullByDefault public abstract class OceanicThingHandler extends BaseThingHandler { public static final String INTERVAL = "interval"; @@ -48,10 +48,10 @@ public abstract class OceanicThingHandler extends BaseThingHandler { private final Logger logger = LoggerFactory.getLogger(OceanicThingHandler.class); protected int bufferSize; - protected ScheduledFuture pollingJob; + protected @Nullable ScheduledFuture pollingJob; protected static String lastLineReceived = ""; - public OceanicThingHandler(@NonNull Thing thing) { + public OceanicThingHandler(Thing thing) { super(thing); } @@ -78,7 +78,9 @@ public abstract class OceanicThingHandler extends BaseThingHandler { updateProperties(properties); } else { State value = createStateForType(selector, response); - updateState(theChannelUID, value); + if (value != null) { + updateState(theChannelUID, value); + } } } else { logger.warn("Received an empty answer for '{}'", selector.name()); @@ -138,7 +140,7 @@ public abstract class OceanicThingHandler extends BaseThingHandler { break; } String response = requestResponse(commandAsString); - if (response.equals("ERR")) { + if ("ERR".equals(response)) { logger.error("An error occurred while setting '{}' to {}", selector.toString(), commandAsString); } @@ -155,15 +157,10 @@ public abstract class OceanicThingHandler extends BaseThingHandler { } @SuppressWarnings("unchecked") - private State createStateForType(OceanicChannelSelector selector, String value) { - Class typeClass = selector.getTypeClass(); - List> stateTypeList = new ArrayList<>(); - - stateTypeList.add((Class) typeClass); - State state = TypeParser.parseState(stateTypeList, selector.convertValue(value)); - - return state; + private @Nullable State createStateForType(OceanicChannelSelector selector, String value) { + return TypeParser.parseState(List.of((Class) selector.getTypeClass()), + selector.convertValue(value)); } - protected abstract String requestResponse(String commandAsString); + protected abstract @Nullable String requestResponse(String commandAsString); } diff --git a/bundles/org.openhab.binding.smartmeter/src/main/java/org/openhab/binding/smartmeter/internal/SmartMeterHandler.java b/bundles/org.openhab.binding.smartmeter/src/main/java/org/openhab/binding/smartmeter/internal/SmartMeterHandler.java index d5e403949..5fdfb36eb 100644 --- a/bundles/org.openhab.binding.smartmeter/src/main/java/org/openhab/binding/smartmeter/internal/SmartMeterHandler.java +++ b/bundles/org.openhab.binding.smartmeter/src/main/java/org/openhab/binding/smartmeter/internal/SmartMeterHandler.java @@ -16,7 +16,6 @@ import java.math.BigDecimal; import java.text.MessageFormat; import java.time.Duration; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -204,7 +203,9 @@ public class SmartMeterHandler extends BaseThingHandler { if (!channel.getProperties().containsKey(SmartMeterBindingConstants.CHANNEL_PROPERTY_OBIS)) { addObisPropertyToChannel(obis, channel); } - updateState(channel.getUID(), state); + if (state != null) { + updateState(channel.getUID(), state); + } updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE); } else { @@ -256,7 +257,9 @@ public class SmartMeterHandler extends BaseThingHandler { MeterValue value = this.smlDevice.getMeterValue(obis); if (value != null) { State state = getStateForObisValue(value, channel); - updateState(channel.getUID(), state); + if (state != null) { + updateState(channel.getUID(), state); + } } } } @@ -264,13 +267,14 @@ public class SmartMeterHandler extends BaseThingHandler { } @SuppressWarnings("unchecked") - private > State getStateForObisValue(MeterValue value, @Nullable Channel channel) { + private @Nullable > State getStateForObisValue(MeterValue value, + @Nullable Channel channel) { Unit unit = value.getUnit(); String valueString = value.getValue(); if (unit != null) { valueString += " " + value.getUnit(); } - State state = TypeParser.parseState(Arrays.asList(QuantityType.class, StringType.class), valueString); + State state = TypeParser.parseState(List.of(QuantityType.class, StringType.class), valueString); if (channel != null && state instanceof QuantityType) { state = applyConformity(channel, (QuantityType) state); Number conversionRatio = (Number) channel.getConfiguration()