From 76bdbdc316620869899ea792b95a2007964e4a17 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sat, 18 Feb 2023 14:15:14 +0100 Subject: [PATCH 1/2] Remove org.apache.common Signed-off-by: lsiepel --- .../internal/PlugwiseMessageProcessor.java | 3 +-- .../binding/plugwise/internal/PlugwiseUtils.java | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) 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 b2bafe0386c6b..e46c9927263d1 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 @@ -22,7 +22,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.plugwise.internal.protocol.AcknowledgementMessage; @@ -95,7 +94,7 @@ public PlugwiseMessageProcessor(PlugwiseCommunicationContext context) { */ private void parseAndQueue(ByteBuffer readBuffer) { String response = new String(readBuffer.array(), 0, readBuffer.limit()); - response = StringUtils.chomp(response); + response = response.replaceAll("\r", "").replaceAll("\n", ""); 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 35b34aa1ada08..4d09193a5231f 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 @@ -20,8 +20,6 @@ import java.time.format.DateTimeFormatter; import java.util.Map; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.text.WordUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.plugwise.internal.protocol.InformationResponseMessage; @@ -102,7 +100,18 @@ public static void stopBackgroundThread(@Nullable Thread thread) { } public static String upperUnderscoreToLowerCamel(String text) { - String upperCamel = StringUtils.remove(WordUtils.capitalizeFully(text, new char[] { '_' }), "_"); + final String delimiter = "_"; + String upperCamel = ""; + for (String str : text.split(delimiter)) { + String properlyCapitalized = ""; + if (str.length() > 0) { + properlyCapitalized = str.substring(0, 1).toUpperCase(); + } + if (str.length() > 1) { + properlyCapitalized = str.substring(1).toLowerCase(); + } + upperCamel = upperCamel + properlyCapitalized; + } return upperCamel.substring(0, 1).toLowerCase() + upperCamel.substring(1); } From 7c44fa515e43f2c50738ca320d1da523bdb9e1ef Mon Sep 17 00:00:00 2001 From: lsiepel Date: Tue, 21 Feb 2023 20:26:42 +0100 Subject: [PATCH 2/2] Fix comment + stringbuilder + add test Signed-off-by: lsiepel --- .../plugwise/internal/PlugwiseUtils.java | 14 +++---- .../plugwise/internal/PlugwiseUtilsTest.java | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 bundles/org.openhab.binding.plugwise/src/test/java/org/openhab/binding/plugwise/internal/PlugwiseUtilsTest.java 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 4d09193a5231f..fb86148f9e5d4 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 @@ -101,18 +101,18 @@ public static void stopBackgroundThread(@Nullable Thread thread) { public static String upperUnderscoreToLowerCamel(String text) { final String delimiter = "_"; - String upperCamel = ""; + StringBuilder upperCamelBuilder = new StringBuilder(text.length()); for (String str : text.split(delimiter)) { - String properlyCapitalized = ""; - if (str.length() > 0) { - properlyCapitalized = str.substring(0, 1).toUpperCase(); + if (upperCamelBuilder.isEmpty() && str.length() > 0) { + upperCamelBuilder.append(str.substring(0, 1).toLowerCase()); + } else if (str.length() > 0) { + upperCamelBuilder.append(str.substring(0, 1).toUpperCase()); } if (str.length() > 1) { - properlyCapitalized = str.substring(1).toLowerCase(); + upperCamelBuilder.append(str.substring(1).toLowerCase()); } - upperCamel = upperCamel + properlyCapitalized; } - return upperCamel.substring(0, 1).toLowerCase() + upperCamel.substring(1); + return upperCamelBuilder.toString(); } public static boolean updateProperties(Map properties, InformationResponseMessage message) { 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 new file mode 100644 index 0000000000000..a72f84037acdb --- /dev/null +++ b/bundles/org.openhab.binding.plugwise/src/test/java/org/openhab/binding/plugwise/internal/PlugwiseUtilsTest.java @@ -0,0 +1,38 @@ +/** + * 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("_")); + } +}