Adjustments for nullness annotated TypeParser (#10068)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2021-02-07 11:43:17 +01:00 committed by GitHub
parent 8a2ac82b07
commit 1210fec9f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 33 deletions

View File

@ -267,6 +267,7 @@ public class HomieThingHandlerTests {
// Assign old value // Assign old value
Value value = property.getChannelState().getCache(); Value value = property.getChannelState().getCache();
Command command = TypeParser.parseCommand(value.getSupportedCommandTypes(), "OLDVALUE"); Command command = TypeParser.parseCommand(value.getSupportedCommandTypes(), "OLDVALUE");
if (command != null) {
property.getChannelState().getCache().update(command); property.getChannelState().getCache().update(command);
// Try to update with new value // Try to update with new value
updateValue = new StringType("SOMETHINGNEW"); updateValue = new StringType("SOMETHINGNEW");
@ -275,6 +276,7 @@ public class HomieThingHandlerTests {
assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE")); assertThat(property.getChannelState().getCache().getChannelState().toString(), is("OLDVALUE"));
verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean()); verify(connection, times(1)).publish(any(), any(), anyInt(), anyBoolean());
} }
}
public Object createSubscriberAnswer(InvocationOnMock invocation) { public Object createSubscriberAnswer(InvocationOnMock invocation) {
final AbstractMqttAttributeClass attributes = (AbstractMqttAttributeClass) invocation.getMock(); final AbstractMqttAttributeClass attributes = (AbstractMqttAttributeClass) invocation.getMock();

View File

@ -22,6 +22,8 @@ import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils; 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.NetworkOceanicBindingConfiguration;
import org.openhab.binding.oceanic.internal.Throttler; import org.openhab.binding.oceanic.internal.Throttler;
import org.openhab.core.thing.Thing; import org.openhab.core.thing.Thing;
@ -36,6 +38,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Karel Goderis - Initial contribution * @author Karel Goderis - Initial contribution
*/ */
@NonNullByDefault
public class NetworkOceanicThingHandler extends OceanicThingHandler { public class NetworkOceanicThingHandler extends OceanicThingHandler {
private static final int REQUEST_TIMEOUT = 3000; 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 final Logger logger = LoggerFactory.getLogger(NetworkOceanicThingHandler.class);
private Socket socket; private @Nullable Socket socket;
private InputStream inputStream; private @Nullable InputStream inputStream;
private OutputStream outputStream; private @Nullable OutputStream outputStream;
protected ScheduledFuture<?> reconnectJob; protected @Nullable ScheduledFuture<?> reconnectJob;
public NetworkOceanicThingHandler(Thing thing) { public NetworkOceanicThingHandler(Thing thing) {
super(thing); super(thing);
@ -99,7 +102,7 @@ public class NetworkOceanicThingHandler extends OceanicThingHandler {
} }
@Override @Override
protected String requestResponse(String commandAsString) { protected @Nullable String requestResponse(String commandAsString) {
synchronized (this) { synchronized (this) {
if (getThing().getStatus() == ThingStatus.ONLINE) { if (getThing().getStatus() == ThingStatus.ONLINE) {
NetworkOceanicBindingConfiguration config = getConfigAs(NetworkOceanicBindingConfiguration.class); NetworkOceanicBindingConfiguration config = getConfigAs(NetworkOceanicBindingConfiguration.class);

View File

@ -13,13 +13,13 @@
package org.openhab.binding.oceanic.internal.handler; package org.openhab.binding.oceanic.internal.handler;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; 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.binding.oceanic.internal.OceanicBindingConstants.OceanicChannelSelector;
import org.openhab.core.thing.Channel; import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID; 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.Command;
import org.openhab.core.types.RefreshType; import org.openhab.core.types.RefreshType;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.core.types.Type;
import org.openhab.core.types.TypeParser; import org.openhab.core.types.TypeParser;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -41,6 +40,7 @@ import org.slf4j.LoggerFactory;
* *
* @author Karel Goderis - Initial contribution * @author Karel Goderis - Initial contribution
*/ */
@NonNullByDefault
public abstract class OceanicThingHandler extends BaseThingHandler { public abstract class OceanicThingHandler extends BaseThingHandler {
public static final String INTERVAL = "interval"; public static final String INTERVAL = "interval";
@ -48,10 +48,10 @@ public abstract class OceanicThingHandler extends BaseThingHandler {
private final Logger logger = LoggerFactory.getLogger(OceanicThingHandler.class); private final Logger logger = LoggerFactory.getLogger(OceanicThingHandler.class);
protected int bufferSize; protected int bufferSize;
protected ScheduledFuture<?> pollingJob; protected @Nullable ScheduledFuture<?> pollingJob;
protected static String lastLineReceived = ""; protected static String lastLineReceived = "";
public OceanicThingHandler(@NonNull Thing thing) { public OceanicThingHandler(Thing thing) {
super(thing); super(thing);
} }
@ -78,8 +78,10 @@ public abstract class OceanicThingHandler extends BaseThingHandler {
updateProperties(properties); updateProperties(properties);
} else { } else {
State value = createStateForType(selector, response); State value = createStateForType(selector, response);
if (value != null) {
updateState(theChannelUID, value); updateState(theChannelUID, value);
} }
}
} else { } else {
logger.warn("Received an empty answer for '{}'", selector.name()); logger.warn("Received an empty answer for '{}'", selector.name());
} }
@ -138,7 +140,7 @@ public abstract class OceanicThingHandler extends BaseThingHandler {
break; break;
} }
String response = requestResponse(commandAsString); String response = requestResponse(commandAsString);
if (response.equals("ERR")) { if ("ERR".equals(response)) {
logger.error("An error occurred while setting '{}' to {}", selector.toString(), logger.error("An error occurred while setting '{}' to {}", selector.toString(),
commandAsString); commandAsString);
} }
@ -155,15 +157,10 @@ public abstract class OceanicThingHandler extends BaseThingHandler {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private State createStateForType(OceanicChannelSelector selector, String value) { private @Nullable State createStateForType(OceanicChannelSelector selector, String value) {
Class<? extends Type> typeClass = selector.getTypeClass(); return TypeParser.parseState(List.of((Class<? extends State>) selector.getTypeClass()),
List<Class<? extends State>> stateTypeList = new ArrayList<>(); selector.convertValue(value));
stateTypeList.add((Class<? extends State>) typeClass);
State state = TypeParser.parseState(stateTypeList, selector.convertValue(value));
return state;
} }
protected abstract String requestResponse(String commandAsString); protected abstract @Nullable String requestResponse(String commandAsString);
} }

View File

@ -16,7 +16,6 @@ import java.math.BigDecimal;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.time.Duration; import java.time.Duration;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -204,7 +203,9 @@ public class SmartMeterHandler extends BaseThingHandler {
if (!channel.getProperties().containsKey(SmartMeterBindingConstants.CHANNEL_PROPERTY_OBIS)) { if (!channel.getProperties().containsKey(SmartMeterBindingConstants.CHANNEL_PROPERTY_OBIS)) {
addObisPropertyToChannel(obis, channel); addObisPropertyToChannel(obis, channel);
} }
if (state != null) {
updateState(channel.getUID(), state); updateState(channel.getUID(), state);
}
updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE); updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
} else { } else {
@ -256,21 +257,24 @@ public class SmartMeterHandler extends BaseThingHandler {
MeterValue<?> value = this.smlDevice.getMeterValue(obis); MeterValue<?> value = this.smlDevice.getMeterValue(obis);
if (value != null) { if (value != null) {
State state = getStateForObisValue(value, channel); State state = getStateForObisValue(value, channel);
if (state != null) {
updateState(channel.getUID(), state); updateState(channel.getUID(), state);
} }
} }
} }
} }
} }
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <Q extends Quantity<Q>> State getStateForObisValue(MeterValue<?> value, @Nullable Channel channel) { private @Nullable <Q extends Quantity<Q>> State getStateForObisValue(MeterValue<?> value,
@Nullable Channel channel) {
Unit<?> unit = value.getUnit(); Unit<?> unit = value.getUnit();
String valueString = value.getValue(); String valueString = value.getValue();
if (unit != null) { if (unit != null) {
valueString += " " + value.getUnit(); 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) { if (channel != null && state instanceof QuantityType) {
state = applyConformity(channel, (QuantityType<Q>) state); state = applyConformity(channel, (QuantityType<Q>) state);
Number conversionRatio = (Number) channel.getConfiguration() Number conversionRatio = (Number) channel.getConfiguration()