[modbus] Replace apache commons lang usage with plain java (#10002)

* [modbus] fix item creation in tests

Signed-off-by: Sami Salonen <ssalonen@gmail.com>
This commit is contained in:
Sami Salonen 2021-02-01 13:07:52 +02:00 committed by GitHub
parent 0f8f72dd2e
commit d5e83e395b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 22 deletions

View File

@ -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<String> 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);

View File

@ -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() ? "<identity>" : 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,

View File

@ -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<String, ChannelUID> toBeLinked = new HashMap<>();
for (Entry<String, String> 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);