diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/ModbusPollerThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/ModbusPollerThingHandler.java index a84858e69..d8832d435 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/ModbusPollerThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/handler/ModbusPollerThingHandler.java @@ -17,7 +17,6 @@ import java.util.Optional; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.modbus.internal.AtomicStampedValue; @@ -179,7 +178,7 @@ public class ModbusPollerThingHandler extends BaseBridgeHandler { private final Logger logger = LoggerFactory.getLogger(ModbusPollerThingHandler.class); private final static List SORTED_READ_FUNCTION_CODES = ModbusBindingConstantsInternal.READ_FUNCTION_CODES - .keySet().stream().sorted().collect(Collectors.toList()); + .keySet().stream().sorted().collect(Collectors.toUnmodifiableList()); private @NonNullByDefault({}) ModbusPollerConfiguration config; private long cacheMillis; @@ -246,7 +245,7 @@ public class ModbusPollerThingHandler extends BaseBridgeHandler { if (!ModbusBindingConstantsInternal.READ_FUNCTION_CODES.containsKey(type)) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, String.format("No function code found for type='%s'. Was expecting one of: %s", type, - StringUtils.join(SORTED_READ_FUNCTION_CODES, ", "))); + String.join(", ", SORTED_READ_FUNCTION_CODES))); return; } functionCode = ModbusBindingConstantsInternal.READ_FUNCTION_CODES.get(type); diff --git a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java index f8d12d2f5..d9f416e6d 100644 --- a/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java +++ b/bundles/org.openhab.binding.modbus/src/main/java/org/openhab/binding/modbus/internal/handler/ModbusDataThingHandler.java @@ -17,11 +17,15 @@ import static org.openhab.binding.modbus.internal.ModbusBindingConstantsInternal import java.math.BigDecimal; import java.time.Duration; import java.time.LocalDateTime; -import java.util.*; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; import java.util.concurrent.TimeUnit; -import org.apache.commons.lang.NotImplementedException; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.modbus.handler.EndpointNotInitializedException; @@ -291,7 +295,7 @@ public class ModbusDataThingHandler extends BaseThingHandler { // Should not happen! This method is not called in case configuration errors and writeType is validated // already in initialization (validateAndParseWriteParameters). // We keep this here for future-proofing the code (new writeType values) - throw new NotImplementedException(String.format( + throw new IllegalStateException(String.format( "writeType does not equal %s or %s and thus configuration is invalid. Should not end up this far with configuration error.", WRITE_TYPE_COIL, WRITE_TYPE_HOLDING)); } @@ -433,8 +437,8 @@ public class ModbusDataThingHandler extends BaseThingHandler { ModbusReadFunctionCode functionCode = this.functionCode; boolean readingDiscreteOrCoil = functionCode == ModbusReadFunctionCode.READ_COILS || functionCode == ModbusReadFunctionCode.READ_INPUT_DISCRETES; - boolean readStartMissing = StringUtils.isBlank(config.getReadStart()); - boolean readValueTypeMissing = StringUtils.isBlank(config.getReadValueType()); + boolean readStartMissing = config.getReadStart() == null || config.getReadStart().isBlank(); + boolean readValueTypeMissing = config.getReadValueType() == null || config.getReadValueType().isBlank(); if (childOfEndpoint && readRequest == null) { if (!readStartMissing || !readValueTypeMissing) { @@ -503,10 +507,10 @@ public class ModbusDataThingHandler extends BaseThingHandler { } private void validateAndParseWriteParameters(ModbusDataConfiguration config) throws ModbusConfigurationException { - boolean writeTypeMissing = StringUtils.isBlank(config.getWriteType()); - boolean writeStartMissing = StringUtils.isBlank(config.getWriteStart()); - boolean writeValueTypeMissing = StringUtils.isBlank(config.getWriteValueType()); - boolean writeTransformationMissing = StringUtils.isBlank(config.getWriteTransform()); + boolean writeTypeMissing = config.getWriteType() == null || config.getWriteType().isBlank(); + boolean writeStartMissing = config.getWriteStart() == null || config.getWriteStart().isBlank(); + boolean writeValueTypeMissing = config.getWriteValueType() == null || config.getWriteValueType().isBlank(); + boolean writeTransformationMissing = config.getWriteTransform() == null || config.getWriteTransform().isBlank(); writeTransformation = new Transformation(config.getWriteTransform()); boolean writingCoil = WRITE_TYPE_COIL.equals(config.getWriteType()); writeParametersHavingTransformationOnly = (writeTypeMissing && writeStartMissing && writeValueTypeMissing @@ -865,8 +869,8 @@ public class ModbusDataThingHandler extends BaseThingHandler { localReadTransformation.isIdentityTransform() ? "" : localReadTransformation); states.put(channelUID, transformedState); } else { - String types = StringUtils.join(acceptedDataTypes.stream().map(cls -> cls.getSimpleName()).toArray(), - ", "); + String types = String.join(", ", + acceptedDataTypes.stream().map(cls -> cls.getSimpleName()).toArray(String[]::new)); logger.warn( "Channel {} will not be updated since transformation was unsuccessful. Channel is expecting the following data types [{}]. Input data: number value {} (value type '{}' taken into account) and bool value {}. Transformation: {}", channelId, types, numericState, readValueType, boolValue, diff --git a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java index ac2004311..a45d73458 100644 --- a/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java +++ b/itests/org.openhab.binding.modbus.tests/src/main/java/org/openhab/binding/modbus/tests/ModbusDataHandlerTest.java @@ -31,7 +31,6 @@ import java.util.concurrent.ScheduledFuture; import java.util.function.Consumer; import java.util.function.Function; -import org.apache.commons.lang.StringUtils; import org.hamcrest.Matcher; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -60,7 +59,6 @@ import org.openhab.core.io.transport.modbus.endpoint.ModbusSlaveEndpoint; import org.openhab.core.io.transport.modbus.endpoint.ModbusTCPSlaveEndpoint; import org.openhab.core.items.GenericItem; import org.openhab.core.items.Item; -import org.openhab.core.library.items.DateTimeItem; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; import org.openhab.core.library.types.OpenClosedType; @@ -191,6 +189,7 @@ public class ModbusDataHandlerTest extends AbstractModbusOSGiTest { Map toBeLinked = new HashMap<>(); for (Entry entry : CHANNEL_TO_ACCEPTED_TYPE.entrySet()) { String channelId = entry.getKey(); + // accepted item type String channelAcceptedType = entry.getValue(); ChannelUID channelUID = new ChannelUID(thingUID, channelId); builder = builder.withChannel(ChannelBuilder.create(channelUID, channelAcceptedType).build()); @@ -199,11 +198,7 @@ public class ModbusDataHandlerTest extends AbstractModbusOSGiTest { // Create item of correct type and link it to channel String itemName = getItemName(channelUID); final GenericItem item; - if (channelId.startsWith("last") || channelId.equals("datetime")) { - item = new DateTimeItem(itemName); - } else { - item = coreItemFactory.createItem(StringUtils.capitalize(channelId), itemName); - } + item = coreItemFactory.createItem(channelAcceptedType, itemName); assertThat(String.format("Could not determine correct item type for %s", channelId), item, is(notNullValue())); assertNotNull(item);