From 14f8490d1cf67bcfce9d6b12e8997fb433317a03 Mon Sep 17 00:00:00 2001 From: Kai Kreuzer Date: Sun, 15 Oct 2023 22:01:07 +0200 Subject: [PATCH] Address warnings in StringUtils class (#3845) Signed-off-by: Kai Kreuzer --- .../org/openhab/core/util/StringUtils.java | 53 ++++++++----------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/util/StringUtils.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/util/StringUtils.java index 98e4a692867..8cbcf485bf6 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/util/StringUtils.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/util/StringUtils.java @@ -24,7 +24,6 @@ * * @author Leo Siepel - Initial contribution */ - @NonNullByDefault public class StringUtils { @@ -65,7 +64,7 @@ public class StringUtils { /** * Simple method to escape XML special characters in String. * There are five XML special characters which needs to be escaped: - * + * *
      * & - &
      * < - <
@@ -80,14 +79,10 @@ public class StringUtils {
     public static @Nullable String escapeXml(@Nullable String str) {
         if (str == null || str.isEmpty()) {
             return str;
+        } else {
+            return str.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """)
+                    .replace("'", "'");
         }
-
-        str = str.replace("&", "&");
-        str = str.replace("<", "<");
-        str = str.replace(">", ">");
-        str = str.replace("\"", """);
-        str = str.replace("'", "'");
-        return str;
     }
 
     /**
@@ -122,7 +117,7 @@ public class StringUtils {
       * "openHAB_is_cool"   => "Openhab_Is_Cool"
       * "foobar_Example" => "Foobar_Example"
      * 
- * + * * @param str the String to capitalize, may be null * @return the capitalized String, may be null */ @@ -155,7 +150,7 @@ public class StringUtils { * "openHAB is cool" => "OpenHAB Is Cool" * "foobar Example" => "Foobar Example" * - * + * * @param str the String to capitalize, may be null * @return the capitalized String, may be null */ @@ -180,29 +175,26 @@ public class StringUtils { /** * Pads the string from the left - * + * *
       * padLeft("9", 4, "0")        => "0009"
       * padLeft("3112", 12, "*")    => "********3112"
       * padLeft("openHAB", 4, "*")  => "openHAB"
      * 
- * + * * @param str the String to pad, may be null * @param minSize the minimum String size to return * @param padString the String to add when padding * @return the padded String */ public static String padLeft(@Nullable String str, int minSize, String padString) { - if (str == null) { - str = ""; - } - - return String.format("%" + minSize + "s", str).replace(" ", padString); + String paddedString = str == null ? "" : str; + return String.format("%" + minSize + "s", paddedString).replace(" ", padString); } /** * Creates a random string - * + * * @param length the length of the String to return * @param charset the characters to use to create the String * @return the random String @@ -220,7 +212,7 @@ public static String getRandomString(int length, String charset) { /** * Creates a random string with [A-Zaz] characters - * + * * @param length the length of the String to return * @return the random String */ @@ -230,7 +222,7 @@ public static String getRandomAlphabetic(int length) { /** * Creates a random string with only hex characters - * + * * @param length the length of the String to return * @return the random String */ @@ -240,7 +232,7 @@ public static String getRandomHex(int length) { /** * Creates a random string with [A-Za-z0-9] characters - * + * * @param length the length of the String to return * @return the random String */ @@ -250,7 +242,7 @@ public static String getRandomAlphanumeric(int length) { /** * Splits the string by character type into an array - * + * *
      * "ab de fg"   => "ab", " ", "de", " ", "fg";
      * "ab   de fg" => "ab", "   ", "de", " ", "fg";
@@ -259,7 +251,7 @@ public static String getRandomAlphanumeric(int length) {
      * "fooBar"     =>  "foo", "Bar"
      * "OHRules"    =>  "OH", "Rules"
      * 
- * + * * @param str the input String to split, may be null * @return the splitted String */ @@ -296,7 +288,7 @@ public static String[] splitByCharacterType(@Nullable String str) { /** * Simple method to un escape XML special characters in String. * There are five XML Special characters which needs to be escaped: - * + * *
      * & => &
      * < => <
@@ -304,19 +296,16 @@ public static String[] splitByCharacterType(@Nullable String str) {
      * " => "
      * ' => '
      * 
- * + * * @param input the input xml as String to unescape, may be null * @return the unescaped xml as String, may be null */ public static @Nullable String unEscapeXml(@Nullable String str) { if (str == null || str.isEmpty()) { return str; + } else { + return str.replace("&", "&").replace("<", "<").replace(">", ">").replace(""", "\"") + .replace("'", "'"); } - str = str.replace("&", "&"); - str = str.replace("<", "<"); - str = str.replace(">", ">"); - str = str.replace(""", "\""); - str = str.replace("'", "'"); - return str; } }