Rework more commons-lang usages (#10314)
* Reworks many commons-lang usages to use standard Java * Updates all remaining commons.lang imports to commons.lang3 Related to openhab/openhab-addons#7722 Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
@@ -14,7 +14,6 @@ package org.openhab.binding.satel.internal.command;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.satel.internal.protocol.SatelMessage;
|
||||
import org.openhab.binding.satel.internal.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Base class for all commands that return result code in the response.
|
||||
@@ -59,7 +58,7 @@ public abstract class ControlCommand extends SatelCommandBase {
|
||||
}
|
||||
|
||||
protected static byte[] userCodeToBytes(String userCode) {
|
||||
if (StringUtils.isEmpty(userCode)) {
|
||||
if (userCode.isEmpty()) {
|
||||
throw new IllegalArgumentException("User code is empty");
|
||||
}
|
||||
if (userCode.length() > 8) {
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Ethm1Config extends SatelBridgeConfig {
|
||||
|
||||
public static final String HOST = "host";
|
||||
|
||||
private @Nullable String host;
|
||||
private String host = "";
|
||||
private int port;
|
||||
private @Nullable String encryptionKey;
|
||||
|
||||
@@ -33,8 +33,7 @@ public class Ethm1Config extends SatelBridgeConfig {
|
||||
* @return IP or hostname of the bridge
|
||||
*/
|
||||
public String getHost() {
|
||||
final String host = this.host;
|
||||
return host == null ? "" : host;
|
||||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
package org.openhab.binding.satel.internal.config;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* The {@link IntRSConfig} contains configuration values for Satel INT-RS bridge.
|
||||
@@ -25,12 +24,12 @@ public class IntRSConfig extends SatelBridgeConfig {
|
||||
|
||||
public static final String PORT = "port";
|
||||
|
||||
private @Nullable String port;
|
||||
private String port = "";
|
||||
|
||||
/**
|
||||
* @return serial port to which the module is connected
|
||||
*/
|
||||
public @Nullable String getPort() {
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.satel.internal.config.Ethm1Config;
|
||||
import org.openhab.binding.satel.internal.protocol.Ethm1Module;
|
||||
import org.openhab.binding.satel.internal.protocol.SatelModule;
|
||||
import org.openhab.binding.satel.internal.util.StringUtils;
|
||||
import org.openhab.core.config.core.status.ConfigStatusMessage;
|
||||
import org.openhab.core.thing.Bridge;
|
||||
import org.openhab.core.thing.ThingStatus;
|
||||
@@ -54,7 +53,7 @@ public class Ethm1BridgeHandler extends SatelBridgeHandler {
|
||||
logger.debug("Initializing handler");
|
||||
|
||||
Ethm1Config config = getConfigAs(Ethm1Config.class);
|
||||
if (StringUtils.isNotBlank(config.getHost())) {
|
||||
if (!config.getHost().isBlank()) {
|
||||
SatelModule satelModule = new Ethm1Module(config.getHost(), config.getPort(), config.getTimeout(),
|
||||
config.getEncryptionKey(), config.hasExtCommandsSupport());
|
||||
super.initialize(satelModule);
|
||||
@@ -71,7 +70,7 @@ public class Ethm1BridgeHandler extends SatelBridgeHandler {
|
||||
Collection<ConfigStatusMessage> configStatusMessages;
|
||||
|
||||
// Check whether an IP address is provided
|
||||
if (StringUtils.isBlank(host)) {
|
||||
if (host.isBlank()) {
|
||||
configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(HOST)
|
||||
.withMessageKeySuffix("hostEmpty").withArguments(HOST).build());
|
||||
} else {
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.satel.internal.config.IntRSConfig;
|
||||
import org.openhab.binding.satel.internal.protocol.IntRSModule;
|
||||
import org.openhab.binding.satel.internal.protocol.SatelModule;
|
||||
import org.openhab.binding.satel.internal.util.StringUtils;
|
||||
import org.openhab.core.config.core.status.ConfigStatusMessage;
|
||||
import org.openhab.core.io.transport.serial.SerialPortManager;
|
||||
import org.openhab.core.thing.Bridge;
|
||||
@@ -59,7 +58,7 @@ public class IntRSBridgeHandler extends SatelBridgeHandler {
|
||||
|
||||
final IntRSConfig config = getConfigAs(IntRSConfig.class);
|
||||
final String port = config.getPort();
|
||||
if (port != null && StringUtils.isNotBlank(port)) {
|
||||
if (!port.isBlank()) {
|
||||
SatelModule satelModule = new IntRSModule(port, serialPortManager, config.getTimeout(),
|
||||
config.hasExtCommandsSupport());
|
||||
super.initialize(satelModule);
|
||||
@@ -76,7 +75,7 @@ public class IntRSBridgeHandler extends SatelBridgeHandler {
|
||||
Collection<ConfigStatusMessage> configStatusMessages;
|
||||
|
||||
// Check whether a serial port is provided
|
||||
if (StringUtils.isBlank(port)) {
|
||||
if (port.isBlank()) {
|
||||
configStatusMessages = Collections.singletonList(ConfigStatusMessage.Builder.error(PORT)
|
||||
.withMessageKeySuffix("portEmpty").withArguments(PORT).build());
|
||||
} else {
|
||||
|
||||
@@ -26,7 +26,6 @@ import org.openhab.binding.satel.internal.event.ConnectionStatusEvent;
|
||||
import org.openhab.binding.satel.internal.event.SatelEventListener;
|
||||
import org.openhab.binding.satel.internal.protocol.SatelModule;
|
||||
import org.openhab.binding.satel.internal.types.IntegraType;
|
||||
import org.openhab.binding.satel.internal.util.StringUtils;
|
||||
import org.openhab.core.thing.Bridge;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.ThingStatus;
|
||||
@@ -164,7 +163,7 @@ public abstract class SatelBridgeHandler extends ConfigStatusBridgeHandler imple
|
||||
* @return current user code, either from the configuration or set later using {@link #setUserCode(String)}
|
||||
*/
|
||||
public String getUserCode() {
|
||||
if (StringUtils.isNotEmpty(userCodeOverride)) {
|
||||
if (!userCodeOverride.isEmpty()) {
|
||||
return userCodeOverride;
|
||||
} else {
|
||||
return config.getUserCode();
|
||||
|
||||
@@ -25,7 +25,6 @@ import org.openhab.binding.satel.internal.event.ConnectionStatusEvent;
|
||||
import org.openhab.binding.satel.internal.event.IntegraStateEvent;
|
||||
import org.openhab.binding.satel.internal.event.NewStatesEvent;
|
||||
import org.openhab.binding.satel.internal.types.StateType;
|
||||
import org.openhab.binding.satel.internal.util.StringUtils;
|
||||
import org.openhab.core.thing.Channel;
|
||||
import org.openhab.core.thing.ChannelUID;
|
||||
import org.openhab.core.thing.Thing;
|
||||
@@ -59,7 +58,7 @@ public abstract class SatelStateThingHandler extends SatelThingHandler {
|
||||
this.requiresRefresh.set(true);
|
||||
} else {
|
||||
withBridgeHandlerPresent(bridgeHandler -> {
|
||||
if (StringUtils.isEmpty(bridgeHandler.getUserCode())) {
|
||||
if (bridgeHandler.getUserCode().isEmpty()) {
|
||||
logger.info("Cannot control devices without providing valid user code. Command has not been sent.");
|
||||
} else {
|
||||
convertCommand(channelUID, command)
|
||||
|
||||
@@ -23,7 +23,6 @@ import java.net.SocketTimeoutException;
|
||||
import java.util.Random;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.openhab.binding.satel.internal.util.StringUtils;
|
||||
import org.openhab.core.util.HexUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -67,17 +66,17 @@ public class Ethm1Module extends SatelModule {
|
||||
|
||||
@Override
|
||||
protected CommunicationChannel connect() throws ConnectionFailureException {
|
||||
logger.info("Connecting to ETHM-1 module at {}:{}", this.host, this.port);
|
||||
logger.info("Connecting to ETHM-1 module at {}:{}", host, port);
|
||||
|
||||
try {
|
||||
Socket socket = new Socket();
|
||||
socket.connect(new InetSocketAddress(this.host, this.port), this.getTimeout());
|
||||
socket.connect(new InetSocketAddress(host, port), this.getTimeout());
|
||||
logger.info("ETHM-1 module connected successfully");
|
||||
|
||||
if (StringUtils.isBlank(this.encryptionKey)) {
|
||||
if (encryptionKey.isBlank()) {
|
||||
return new TCPCommunicationChannel(socket);
|
||||
} else {
|
||||
return new EncryptedCommunicationChannel(socket, this.encryptionKey);
|
||||
return new EncryptedCommunicationChannel(socket, encryptionKey);
|
||||
}
|
||||
} catch (SocketTimeoutException e) {
|
||||
throw new ConnectionFailureException("Connection timeout", e);
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2021 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.satel.internal.util;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Replacement class for Apache's StringUtils.
|
||||
*
|
||||
* @author Krzysztof Goworek - Initial contribution
|
||||
*
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class StringUtils {
|
||||
|
||||
/**
|
||||
* Checks if a string is empty or null.
|
||||
*
|
||||
* @param str the string to check
|
||||
* @return <code>true</code> if given string is empty or null
|
||||
*/
|
||||
public static boolean isEmpty(@Nullable String str) {
|
||||
return str == null || str.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is not empty and not null.
|
||||
*
|
||||
* @param str the string to check
|
||||
* @return <code>true</code> if given string is not empty and not null
|
||||
*/
|
||||
public static boolean isNotEmpty(@Nullable String str) {
|
||||
return !isEmpty(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is null or empty or all characters are whitespace.
|
||||
*
|
||||
* @param str the string to check
|
||||
* @return <code>true</code> if given string is blank
|
||||
*/
|
||||
public static boolean isBlank(@Nullable String str) {
|
||||
return str == null || str.isBlank();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a string is not null, not empty and contains at least one non-whitespace character.
|
||||
*
|
||||
* @param str the string to check
|
||||
* @return <code>true</code> if given string is not blank
|
||||
*/
|
||||
public static boolean isNotBlank(@Nullable String str) {
|
||||
return !isBlank(str);
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2010-2021 Contributors to the openHAB project
|
||||
*
|
||||
* See the NOTICE file(s) distributed with this work for additional
|
||||
* information.
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Eclipse Public License 2.0 which is available at
|
||||
* http://www.eclipse.org/legal/epl-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.openhab.binding.satel.internal.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author Krzysztof Goworek - Initial contribution
|
||||
*/
|
||||
public class StringUtilsTest {
|
||||
|
||||
@Test
|
||||
public void testIsEmpty() {
|
||||
assertFalse(StringUtils.isEmpty("foobar"));
|
||||
assertFalse(StringUtils.isEmpty(" "));
|
||||
assertTrue(StringUtils.isEmpty(""));
|
||||
assertTrue(StringUtils.isEmpty(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotEmpty() {
|
||||
assertTrue(StringUtils.isNotEmpty("foobar"));
|
||||
assertTrue(StringUtils.isNotEmpty(" "));
|
||||
assertFalse(StringUtils.isNotEmpty(""));
|
||||
assertFalse(StringUtils.isNotEmpty(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsBlank() {
|
||||
assertFalse(StringUtils.isBlank("foobar"));
|
||||
assertTrue(StringUtils.isBlank(" "));
|
||||
assertTrue(StringUtils.isBlank(""));
|
||||
assertTrue(StringUtils.isBlank(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsNotBlank() {
|
||||
assertTrue(StringUtils.isNotBlank("foobar"));
|
||||
assertFalse(StringUtils.isNotBlank(" "));
|
||||
assertFalse(StringUtils.isNotBlank(""));
|
||||
assertFalse(StringUtils.isNotBlank(null));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user