diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java b/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java index bce1318dd4a..f9f03c43fa9 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/StringUtil.java @@ -370,6 +370,85 @@ public class StringUtil } } + /* ------------------------------------------------------------ */ + /** + * Test if a string is null or only has whitespace characters in it. + *
+ * Note: uses codepoint version of {@link Character#isWhitespace(int)} to support Unicode better. + * + *
+ * isBlank(null) == true + * isBlank("") == true + * isBlank("\r\n") == true + * isBlank("\t") == true + * isBlank(" ") == true + * isBlank("a") == false + * isBlank(".") == false + * isBlank(";\n") == false + *+ * + * @param str + * the string to test. + * @return true if string is null or only whitespace characters, false if non-whitespace characters encountered. + */ + public static boolean isBlank(String str) + { + if (str == null) + { + return true; + } + int len = str.length(); + for (int i = 0; i < len; i++) + { + if (!Character.isWhitespace(str.codePointAt(i))) + { + // found a non-whitespace, we can stop searching now + return false; + } + } + // only whitespace + return true; + } + + /* ------------------------------------------------------------ */ + /** + * Test if a string is not null and contains at least 1 non-whitespace characters in it. + *
+ * Note: uses codepoint version of {@link Character#isWhitespace(int)} to support Unicode better. + * + *
+ * isNotBlank(null) == false + * isNotBlank("") == false + * isNotBlank("\r\n") == false + * isNotBlank("\t") == false + * isNotBlank(" ") == false + * isNotBlank("a") == true + * isNotBlank(".") == true + * isNotBlank(";\n") == true + *+ * + * @param str + * the string to test. + * @return true if string is not null and has at least 1 non-whitespace character, false if null or all-whitespace characters. + */ + public static boolean isNotBlank(String str) + { + if (str == null) + { + return false; + } + int len = str.length(); + for (int i = 0; i < len; i++) + { + if (!Character.isWhitespace(str.codePointAt(i))) + { + // found a non-whitespace, we can stop searching now + return true; + } + } + // only whitespace + return false; + } /* ------------------------------------------------------------ */ public static boolean isUTF8(String charset) diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java index 8167eae07ad..912b1c86e68 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/StringUtilTest.java @@ -197,4 +197,34 @@ public class StringUtilTest } System.err.println(calc); } + + @Test + public void testIsBlank() { + Assert.assertTrue(StringUtil.isBlank(null)); + Assert.assertTrue(StringUtil.isBlank("")); + Assert.assertTrue(StringUtil.isBlank("\r\n")); + Assert.assertTrue(StringUtil.isBlank("\t")); + Assert.assertTrue(StringUtil.isBlank(" ")); + + Assert.assertFalse(StringUtil.isBlank("a")); + Assert.assertFalse(StringUtil.isBlank(" a")); + Assert.assertFalse(StringUtil.isBlank("a ")); + Assert.assertFalse(StringUtil.isBlank(".")); + Assert.assertFalse(StringUtil.isBlank(";\n")); + } + + @Test + public void testIsNotBlank() { + Assert.assertFalse(StringUtil.isNotBlank(null)); + Assert.assertFalse(StringUtil.isNotBlank("")); + Assert.assertFalse(StringUtil.isNotBlank("\r\n")); + Assert.assertFalse(StringUtil.isNotBlank("\t")); + Assert.assertFalse(StringUtil.isNotBlank(" ")); + + Assert.assertTrue(StringUtil.isNotBlank("a")); + Assert.assertTrue(StringUtil.isNotBlank(" a")); + Assert.assertTrue(StringUtil.isNotBlank("a ")); + Assert.assertTrue(StringUtil.isNotBlank(".")); + Assert.assertTrue(StringUtil.isNotBlank(";\n")); + } }