]> git.basschouten.com Git - openhab-addons.git/commitdiff
[satel] Removed dependency on 'org.apache.commons.lang' (#9805)
authordruciak <druciak@users.noreply.github.com>
Fri, 15 Jan 2021 13:56:15 +0000 (14:56 +0100)
committerGitHub <noreply@github.com>
Fri, 15 Jan 2021 13:56:15 +0000 (14:56 +0100)
Signed-off-by: Krzysztof Goworek <krzysztof.goworek@gmail.com>
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlCommand.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/ControlObjectCommand.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/command/SetClockCommand.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/Ethm1BridgeHandler.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/IntRSBridgeHandler.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelBridgeHandler.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/handler/SatelStateThingHandler.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/protocol/Ethm1Module.java
bundles/org.openhab.binding.satel/src/main/java/org/openhab/binding/satel/internal/util/StringUtils.java [new file with mode: 0644]
bundles/org.openhab.binding.satel/src/test/java/org/openhab/binding/satel/internal/util/StringUtilsTest.java [new file with mode: 0644]

index 1c3e52ace54cb0fd3ddd30cb20efd837caf2abbe..c6d44986943e9c6a4d638edb41eba621e0bb2ee5 100644 (file)
@@ -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");
index 9395949d5aced0a3f2871748d8d96566e7cd42cf..b0fcd7b457ae92fb1edb1efe56483af69866a34a 100644 (file)
@@ -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;
     }
index 1dd61afe0a5d2d9bb5a2cc0d52920298d69826ed..0fa7f2ff1ae58ccf2b79f04596d616cede3f49ac 100644 (file)
@@ -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) {
index 6601301c7dceeded1fa500f26e1a9cfe230d0305..2d795d6c7bd8d6dfd50088d997101c7d73f87b2d 100644 (file)
@@ -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;
index efa8c6d839c20f0e545eede30e1f78675871b4c5..0718a3234db89c6b97f6ff3dee37c9d0669acacc 100644 (file)
@@ -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;
index d7172a164e4c637576b2a981dffb5a700fd34ad4..f06ba231c0089e9f46527f4f342e45d8c488152b 100644 (file)
@@ -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;
index d14f07263c88b5c0f9cfdc960a460ce0e6c06837..19eedacd0fe525cce0f34623c85c0a14c39643ae 100644 (file)
@@ -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;
index 013dc3a6476cee8cf2736cd8d52677feac8b071f..457f08e865a3aca34c7f35102b69e904f099c75f 100644 (file)
@@ -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 (file)
index 0000000..0661632
--- /dev/null
@@ -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 <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);
+    }
+}
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 (file)
index 0000000..4d35c36
--- /dev/null
@@ -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));
+    }
+}