diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java index 27fb280dc19..c348a6930f6 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseMessageProcessor.java @@ -30,6 +30,7 @@ import org.openhab.binding.plugwise.internal.protocol.field.MessageType; import org.openhab.core.io.transport.serial.SerialPort; import org.openhab.core.io.transport.serial.SerialPortEvent; import org.openhab.core.io.transport.serial.SerialPortEventListener; +import org.openhab.core.util.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,7 +94,7 @@ public class PlugwiseMessageProcessor implements SerialPortEventListener { */ private void parseAndQueue(ByteBuffer readBuffer) { String response = new String(readBuffer.array(), 0, readBuffer.limit()); - response = response.replace("\r", "").replace("\n", ""); + response = StringUtils.chomp(response); Matcher matcher = RESPONSE_PATTERN.matcher(response); diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java index ceb722888a1..dfbdb872a02 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/PlugwiseUtils.java @@ -99,22 +99,6 @@ public final class PlugwiseUtils { } } - public static String upperUnderscoreToLowerCamel(String text) { - final String delimiter = "_"; - StringBuilder upperCamelBuilder = new StringBuilder(text.length()); - for (String str : text.split(delimiter)) { - if (upperCamelBuilder.isEmpty() && !str.isEmpty()) { - upperCamelBuilder.append(str.substring(0, 1).toLowerCase()); - } else if (!str.isEmpty()) { - upperCamelBuilder.append(str.substring(0, 1).toUpperCase()); - } - if (str.length() > 1) { - upperCamelBuilder.append(str.substring(1).toLowerCase()); - } - } - return upperCamelBuilder.toString(); - } - public static boolean updateProperties(Map properties, InformationResponseMessage message) { boolean update = false; diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseRelayConfig.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseRelayConfig.java index 9477ad0a177..d6fde718a9d 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseRelayConfig.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseRelayConfig.java @@ -12,13 +12,15 @@ */ package org.openhab.binding.plugwise.internal.config; -import static org.openhab.binding.plugwise.internal.PlugwiseUtils.*; import static org.openhab.binding.plugwise.internal.config.PlugwiseRelayConfig.PowerStateChanging.COMMAND_SWITCHING; import java.time.Duration; +import java.util.Objects; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.plugwise.internal.PlugwiseUtils; import org.openhab.binding.plugwise.internal.protocol.field.MACAddress; +import org.openhab.core.util.StringUtils; /** * The {@link PlugwiseRelayConfig} class represents the configuration for a Plugwise relay device (Circle, Circle+, @@ -36,7 +38,8 @@ public class PlugwiseRelayConfig { } private String macAddress = ""; - private String powerStateChanging = upperUnderscoreToLowerCamel(COMMAND_SWITCHING.name()); + private String powerStateChanging = Objects + .requireNonNull(StringUtils.capitalizeByUnderscore(COMMAND_SWITCHING.name())); private boolean suppliesPower = false; private int measurementInterval = 60; // minutes private boolean temporarilyNotInNetwork = false; @@ -47,7 +50,7 @@ public class PlugwiseRelayConfig { } public PowerStateChanging getPowerStateChanging() { - return PowerStateChanging.valueOf(lowerCamelToUpperUnderscore(powerStateChanging)); + return PowerStateChanging.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(powerStateChanging)); } public boolean isSuppliesPower() { diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseScanConfig.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseScanConfig.java index fa06bdc616e..d9e2783204b 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseScanConfig.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseScanConfig.java @@ -12,14 +12,16 @@ */ package org.openhab.binding.plugwise.internal.config; -import static org.openhab.binding.plugwise.internal.PlugwiseUtils.*; import static org.openhab.binding.plugwise.internal.protocol.field.Sensitivity.MEDIUM; import java.time.Duration; +import java.util.Objects; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.plugwise.internal.PlugwiseUtils; import org.openhab.binding.plugwise.internal.protocol.field.MACAddress; import org.openhab.binding.plugwise.internal.protocol.field.Sensitivity; +import org.openhab.core.util.StringUtils; /** * The {@link PlugwiseScanConfig} class represents the configuration for a Plugwise Scan. @@ -30,7 +32,7 @@ import org.openhab.binding.plugwise.internal.protocol.field.Sensitivity; public class PlugwiseScanConfig { private String macAddress = ""; - private String sensitivity = upperUnderscoreToLowerCamel(MEDIUM.name()); + private String sensitivity = Objects.requireNonNull(StringUtils.capitalizeByUnderscore(MEDIUM.name())); private int switchOffDelay = 5; // minutes private boolean daylightOverride = false; private int wakeupInterval = 1440; // minutes (1 day) @@ -43,7 +45,7 @@ public class PlugwiseScanConfig { } public Sensitivity getSensitivity() { - return Sensitivity.valueOf(lowerCamelToUpperUnderscore(sensitivity)); + return Sensitivity.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(sensitivity)); } public Duration getSwitchOffDelay() { diff --git a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseSenseConfig.java b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseSenseConfig.java index 342ac8f4692..acde2175ba6 100644 --- a/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseSenseConfig.java +++ b/bundles/org.openhab.binding.plugwise/src/main/java/org/openhab/binding/plugwise/internal/config/PlugwiseSenseConfig.java @@ -12,18 +12,20 @@ */ package org.openhab.binding.plugwise.internal.config; -import static org.openhab.binding.plugwise.internal.PlugwiseUtils.*; import static org.openhab.binding.plugwise.internal.protocol.field.BoundaryAction.OFF_BELOW_ON_ABOVE; import static org.openhab.binding.plugwise.internal.protocol.field.BoundaryType.NONE; import java.time.Duration; +import java.util.Objects; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.openhab.binding.plugwise.internal.PlugwiseUtils; import org.openhab.binding.plugwise.internal.protocol.field.BoundaryAction; import org.openhab.binding.plugwise.internal.protocol.field.BoundaryType; import org.openhab.binding.plugwise.internal.protocol.field.Humidity; import org.openhab.binding.plugwise.internal.protocol.field.MACAddress; import org.openhab.binding.plugwise.internal.protocol.field.Temperature; +import org.openhab.core.util.StringUtils; /** * The {@link PlugwiseScanConfig} class represents the configuration for a Plugwise Sense. @@ -35,8 +37,9 @@ public class PlugwiseSenseConfig { private String macAddress = ""; private int measurementInterval = 15; // minutes - private String boundaryType = upperUnderscoreToLowerCamel(NONE.name()); - private String boundaryAction = upperUnderscoreToLowerCamel(OFF_BELOW_ON_ABOVE.name()); + private String boundaryType = Objects.requireNonNull(StringUtils.capitalizeByUnderscore(NONE.name())); + private String boundaryAction = Objects + .requireNonNull(StringUtils.capitalizeByUnderscore(OFF_BELOW_ON_ABOVE.name())); private int temperatureBoundaryMin = 15; // degrees Celsius private int temperatureBoundaryMax = 25; // degrees Celsius private int humidityBoundaryMin = 45; // relative humidity (RH) @@ -54,11 +57,11 @@ public class PlugwiseSenseConfig { } public BoundaryType getBoundaryType() { - return BoundaryType.valueOf(lowerCamelToUpperUnderscore(boundaryType)); + return BoundaryType.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(boundaryType)); } public BoundaryAction getBoundaryAction() { - return BoundaryAction.valueOf(lowerCamelToUpperUnderscore(boundaryAction)); + return BoundaryAction.valueOf(PlugwiseUtils.lowerCamelToUpperUnderscore(boundaryAction)); } public Temperature getTemperatureBoundaryMin() { diff --git a/bundles/org.openhab.binding.plugwise/src/test/java/org/openhab/binding/plugwise/internal/PlugwiseUtilsTest.java b/bundles/org.openhab.binding.plugwise/src/test/java/org/openhab/binding/plugwise/internal/PlugwiseUtilsTest.java deleted file mode 100644 index a72f84037ac..00000000000 --- a/bundles/org.openhab.binding.plugwise/src/test/java/org/openhab/binding/plugwise/internal/PlugwiseUtilsTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) 2010-2023 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.plugwise.internal; - -import static org.junit.jupiter.api.Assertions.*; - -import org.eclipse.jdt.annotation.NonNullByDefault; -import org.junit.jupiter.api.Test; - -/** - * The {@link PlugwiseUtilsTest} class tests some static string utility methods - * - * @author Leo Siepel - Initial contribution - */ -@NonNullByDefault -public class PlugwiseUtilsTest { - - @Test - public void upperUnderscoreToLowerCamelBaseTest() { - assertEquals("nodelimiterpresent", PlugwiseUtils.upperUnderscoreToLowerCamel("NoDelimiterPresent")); - assertEquals("delimiterIsPresent", PlugwiseUtils.upperUnderscoreToLowerCamel("DeliMiter_iS_PreSent")); - assertEquals("doubleDelimitersDouble", PlugwiseUtils.upperUnderscoreToLowerCamel("DOUBLE__DELIMITERS__DOUBLE")); - assertEquals("helloWorld", PlugwiseUtils.upperUnderscoreToLowerCamel("HELLO_WORLD")); - assertEquals("", PlugwiseUtils.upperUnderscoreToLowerCamel("")); - assertEquals("", PlugwiseUtils.upperUnderscoreToLowerCamel("_")); - } -}