From acc5eb911c3cabc6dabd9db487efd1d81d08279d Mon Sep 17 00:00:00 2001 From: druciak Date: Fri, 15 Jan 2021 14:56:15 +0100 Subject: [PATCH] [satel] Removed dependency on 'org.apache.commons.lang' (#9805) Signed-off-by: Krzysztof Goworek --- .../internal/command/ControlCommand.java | 21 +++++- .../command/ControlObjectCommand.java | 3 +- .../internal/command/SetClockCommand.java | 3 +- .../internal/handler/Ethm1BridgeHandler.java | 2 +- .../internal/handler/IntRSBridgeHandler.java | 2 +- .../internal/handler/SatelBridgeHandler.java | 2 +- .../handler/SatelStateThingHandler.java | 2 +- .../satel/internal/protocol/Ethm1Module.java | 2 +- .../satel/internal/util/StringUtils.java | 66 +++++++++++++++++++ .../satel/internal/util/StringUtilsTest.java | 55 ++++++++++++++++ 10 files changed, 148 insertions(+), 10 deletions(-) create mode 100644 bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/util/StringUtils.java create mode 100644 bundles/org.openhab.binding.satel/src/test/java/org/openhab/binding/satel/internal/util/StringUtilsTest.java diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlCommand.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlCommand.java index 1c3e52ace..c6d449869 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlCommand.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlCommand.java @@ -12,9 +12,9 @@ */ package org.openhab.binding.satel.internal.command; -import org.apache.commons.lang.StringUtils; 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. @@ -34,11 +34,30 @@ public abstract class ControlCommand extends SatelCommandBase { super(commandCode, payload); } + /** + * Creates new command class instance. + * + * @param commandCode command code + * @param payload command bytes + * @param userCode user code + */ + public ControlCommand(byte commandCode, byte[] payload, String userCode) { + super(commandCode, appendUserCode(payload, userCode)); + } + @Override protected boolean isResponseValid(SatelMessage response) { return true; } + protected static byte[] appendUserCode(byte[] payload, String userCode) { + byte[] userCodeBytes = userCodeToBytes(userCode); + byte[] result = new byte[userCodeBytes.length + payload.length]; + System.arraycopy(userCodeBytes, 0, result, 0, userCodeBytes.length); + System.arraycopy(payload, 0, result, userCodeBytes.length, payload.length); + return result; + } + protected static byte[] userCodeToBytes(String userCode) { if (StringUtils.isEmpty(userCode)) { throw new IllegalArgumentException("User code is empty"); diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlObjectCommand.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlObjectCommand.java index 9395949d5..b0fcd7b45 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlObjectCommand.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlObjectCommand.java @@ -16,7 +16,6 @@ import java.util.BitSet; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -import org.apache.commons.lang.ArrayUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.openhab.binding.satel.internal.event.EventDispatcher; import org.openhab.binding.satel.internal.event.NewStatesEvent; @@ -47,7 +46,7 @@ public class ControlObjectCommand extends ControlCommand { */ public ControlObjectCommand(ControlType controlType, byte[] objects, String userCode, ScheduledExecutorService scheduler) { - super(controlType.getControlCommand(), ArrayUtils.addAll(userCodeToBytes(userCode), objects)); + super(controlType.getControlCommand(), objects, userCode); this.controlType = controlType; this.scheduler = scheduler; } diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/SetClockCommand.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/SetClockCommand.java index 1dd61afe0..0fa7f2ff1 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/SetClockCommand.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/SetClockCommand.java @@ -15,7 +15,6 @@ package org.openhab.binding.satel.internal.command; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import org.apache.commons.lang.ArrayUtils; import org.eclipse.jdt.annotation.NonNullByDefault; /** @@ -37,7 +36,7 @@ public class SetClockCommand extends ControlCommand { * @param userCode code of the user on behalf the control is made */ public SetClockCommand(LocalDateTime dateTime, String userCode) { - super(COMMAND_CODE, ArrayUtils.addAll(userCodeToBytes(userCode), getDateTimeBytes(dateTime))); + super(COMMAND_CODE, getDateTimeBytes(dateTime), userCode); } private static byte[] getDateTimeBytes(LocalDateTime dateTime) { diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/Ethm1BridgeHandler.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/Ethm1BridgeHandler.java index 6601301c7..2d795d6c7 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/Ethm1BridgeHandler.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/Ethm1BridgeHandler.java @@ -19,11 +19,11 @@ import java.util.Collection; import java.util.Collections; import java.util.Set; -import org.apache.commons.lang.StringUtils; 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; diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/IntRSBridgeHandler.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/IntRSBridgeHandler.java index efa8c6d83..0718a3234 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/IntRSBridgeHandler.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/IntRSBridgeHandler.java @@ -19,11 +19,11 @@ import java.util.Collection; import java.util.Collections; import java.util.Set; -import org.apache.commons.lang.StringUtils; 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; diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelBridgeHandler.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelBridgeHandler.java index d7172a164..f06ba231c 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelBridgeHandler.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelBridgeHandler.java @@ -17,7 +17,6 @@ import java.time.ZoneId; 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.satel.internal.command.NewStatesCommand; @@ -27,6 +26,7 @@ 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; diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelStateThingHandler.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelStateThingHandler.java index d14f07263..19eedacd0 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelStateThingHandler.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelStateThingHandler.java @@ -17,7 +17,6 @@ import java.util.LinkedList; import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; -import org.apache.commons.lang.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.satel.internal.command.IntegraStateCommand; @@ -26,6 +25,7 @@ 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; diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/Ethm1Module.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/Ethm1Module.java index 013dc3a64..457f08e86 100644 --- a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/Ethm1Module.java +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/Ethm1Module.java @@ -22,8 +22,8 @@ import java.net.Socket; import java.net.SocketTimeoutException; import java.util.Random; -import org.apache.commons.lang.StringUtils; 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; diff --git a/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/util/StringUtils.java b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/util/StringUtils.java new file mode 100644 index 000000000..066163286 --- /dev/null +++ b/bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/util/StringUtils.java @@ -0,0 +1,66 @@ +/** + * 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 true 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 true 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 true 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 true if given string is not blank + */ + public static boolean isNotBlank(@Nullable String str) { + return !isBlank(str); + } +} diff --git a/bundles/org.openhab.binding.satel/src/test/java/org/openhab/binding/satel/internal/util/StringUtilsTest.java b/bundles/org.openhab.binding.satel/src/test/java/org/openhab/binding/satel/internal/util/StringUtilsTest.java new file mode 100644 index 000000000..4d35c367b --- /dev/null +++ b/bundles/org.openhab.binding.satel/src/test/java/org/openhab/binding/satel/internal/util/StringUtilsTest.java @@ -0,0 +1,55 @@ +/** + * 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)); + } +}