[satel] Removed dependency on 'org.apache.commons.lang' (#9805)

Signed-off-by: Krzysztof Goworek <krzysztof.goworek@gmail.com>
pull/9833/head
druciak 2021-01-15 14:56:15 +01:00 committed by GitHub
parent d95fc925e9
commit acc5eb911c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 148 additions and 10 deletions

View 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");

View 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;
}

View 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) {

View 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;

View 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;

View 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;

View 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;

View 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;

View File

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

View File

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