From 19c912f730a589fa31656d21f36f9d44342b54fe Mon Sep 17 00:00:00 2001 From: Sascha Bieberstein Date: Wed, 20 Nov 2024 20:53:06 +0100 Subject: [PATCH 1/2] Provides a helper for checking if a bunch of strings are all empty --- .../java/sirius/kernel/commons/Strings.java | 16 +++ .../sirius/kernel/commons/StringsTest.kt | 104 +++++++++++------- 2 files changed, 78 insertions(+), 42 deletions(-) diff --git a/src/main/java/sirius/kernel/commons/Strings.java b/src/main/java/sirius/kernel/commons/Strings.java index 0765c6e9..81aeef8f 100644 --- a/src/main/java/sirius/kernel/commons/Strings.java +++ b/src/main/java/sirius/kernel/commons/Strings.java @@ -116,6 +116,22 @@ public static boolean isFilled(@Nullable Object string) { return string.toString() != null && !string.toString().isEmpty(); } + /** + * Checks if the string representations of the given objects are all "" or null. + * + * @param strings the objects which are to be checked + * @return true if all strings are null or "", false if one of them is filled + * @see #isEmpty(Object) + */ + public static boolean areAllEmpty(Object... strings) { + for (Object string : strings) { + if (Strings.isFilled(string)) { + return false; + } + } + return true; + } + /** * Compares the given Strings while treating upper- and lowercase characters as equal. *

diff --git a/src/test/kotlin/sirius/kernel/commons/StringsTest.kt b/src/test/kotlin/sirius/kernel/commons/StringsTest.kt index 8b2adf73..86c7d21b 100644 --- a/src/test/kotlin/sirius/kernel/commons/StringsTest.kt +++ b/src/test/kotlin/sirius/kernel/commons/StringsTest.kt @@ -35,6 +35,17 @@ class StringsTest { assertTrue { Strings.isEmpty("") } } + @Test + fun areAllEmpty() { + assertTrue { Strings.areAllEmpty(null, null) } + assertTrue { Strings.areAllEmpty("", "") } + assertTrue { Strings.areAllEmpty("", null) } + assertTrue { Strings.areAllEmpty(null, "") } + assertFalse { Strings.areAllEmpty("Test", null) } + assertFalse { Strings.areAllEmpty(null, "Test") } + assertFalse { Strings.areAllEmpty("Test", "Test") } + } + @Test fun equalIgnoreCase() { assertTrue { Strings.equalIgnoreCase("A", "a") } @@ -51,7 +62,7 @@ class StringsTest { assertFalse { Strings.areEqual("a", "A") } assertTrue { Strings.areEqual( - "a", "A" + "a", "A" ) { x: Any -> x.toString().lowercase(Locale.getDefault()) } } assertTrue { Strings.areEqual("", null) } @@ -122,12 +133,12 @@ class StringsTest { assertEquals(listOf(), Strings.splitSmart("", 2)) assertEquals(listOf("das ist", "ein", "Test"), Strings.splitSmart("das ist ein Test", 7)) assertEquals( - listOf("lange-w", "örter-w", "erden-a", "uch-get", "rennt"), - Strings.splitSmart("lange-wörter-werden-auch-getrennt", 7) + listOf("lange-w", "örter-w", "erden-a", "uch-get", "rennt"), + Strings.splitSmart("lange-wörter-werden-auch-getrennt", 7) ) assertEquals( - listOf("Ein langer Text kann in eine Zeile"), - Strings.splitSmart("Ein langer Text kann in eine Zeile", 40) + listOf("Ein langer Text kann in eine Zeile"), + Strings.splitSmart("Ein langer Text kann in eine Zeile", 40) ) } @@ -142,9 +153,10 @@ class StringsTest { @Test fun replaceAll() { - assertEquals("A<B&C&&D&;&E", Strings.replaceAll( + assertEquals( + "A<B&C&&D&;&E", Strings.replaceAll( Pattern.compile("&([a-zA-Z0-9]{0,6};?)"), "A<B&C&&D&;&E" - ) { s: String -> (if (s.endsWith(";") && !s.startsWith(";")) "&" else "&") + s }) + ) { s: String -> (if (s.endsWith(";") && !s.startsWith(";")) "&" else "&") + s }) } @Test @@ -175,31 +187,33 @@ class StringsTest { @Test fun cleanup() { assertEquals( - "Hel lo", Strings.cleanup("Hel lo ", UnaryOperator { input: String? -> StringCleanup.trim(input!!) }) + "Hel lo", Strings.cleanup("Hel lo ", UnaryOperator { input: String? -> StringCleanup.trim(input!!) }) ) assertEquals( - "Hel lo ", - Strings.cleanup( - "Hel \t \t \r\n lo ", - UnaryOperator { input: String? -> StringCleanup.reduceWhitespace(input!!) }) + "Hel lo ", + Strings.cleanup( + "Hel \t \t \r\n lo ", + UnaryOperator { input: String? -> StringCleanup.reduceWhitespace(input!!) }) ) assertEquals( - "Hello", - Strings.cleanup( - "Hel \t \t \n lo ", - UnaryOperator { input: String? -> StringCleanup.removeWhitespace(input!!) }) + "Hello", + Strings.cleanup( + "Hel \t \t \n lo ", + UnaryOperator { input: String? -> StringCleanup.removeWhitespace(input!!) }) ) assertEquals( - "Hello", - Strings.cleanup("Héllo", UnaryOperator { term: String? -> StringCleanup.reduceCharacters(term) }) + "Hello", + Strings.cleanup("Héllo", UnaryOperator { term: String? -> StringCleanup.reduceCharacters(term) }) ) assertEquals( - "hello", Strings.cleanup("Héllo", + "hello", Strings.cleanup( + "Héllo", UnaryOperator { term: String? -> StringCleanup.reduceCharacters(term) }, UnaryOperator { input: String? -> StringCleanup.lowercase(input!!) }) ) assertEquals( - "HELLO", Strings.cleanup("Héllo", + "HELLO", Strings.cleanup( + "Héllo", UnaryOperator { term: String? -> StringCleanup.reduceCharacters(term) }, UnaryOperator { input: String? -> StringCleanup.uppercase(input!!) }) ) @@ -216,55 +230,61 @@ class StringsTest { Strings.cleanup("-hello-", UnaryOperator { input: String? -> StringCleanup.capitalize(input!!) }) ) assertEquals( - "Hello", - Strings.cleanup("Hel-lo", UnaryOperator { input: String? -> StringCleanup.removePunctuation(input!!) }) + "Hello", + Strings.cleanup("Hel-lo", UnaryOperator { input: String? -> StringCleanup.removePunctuation(input!!) }) ) assertEquals( - "Hello", - Strings.cleanup( - "\u0008Hello", - UnaryOperator { input: String? -> StringCleanup.removeControlCharacters(input!!) }) + "Hello", + Strings.cleanup( + "\u0008Hello", + UnaryOperator { input: String? -> StringCleanup.removeControlCharacters(input!!) }) ) assertEquals( - "Test", Strings.cleanup("Test", + "Test", Strings.cleanup( + "Test", UnaryOperator { input: String? -> StringCleanup.replaceXml(input) }, UnaryOperator { input: String? -> StringCleanup.trim(input!!) }) ) assertEquals( - "Test", Strings.cleanup("Test
", + "Test", Strings.cleanup( + "Test
", UnaryOperator { input: String? -> StringCleanup.replaceXml(input) }, UnaryOperator { input: String? -> StringCleanup.trim(input!!) }) ) assertEquals( - "Test Blubb", Strings.cleanup("Test
Blubb
", + "Test Blubb", Strings.cleanup( + "Test
Blubb
", UnaryOperator { input: String? -> StringCleanup.replaceXml(input) }, UnaryOperator { input: String? -> StringCleanup.trim(input!!) }) ) assertEquals( - "foo having < 3 m, with >= 3 m", Strings.cleanup("foo having < 3 m, with >= 3 m", + "foo having < 3 m, with >= 3 m", Strings.cleanup( + "foo having < 3 m, with >= 3 m", UnaryOperator { input: String? -> StringCleanup.replaceXml(input) }, UnaryOperator { input: String? -> StringCleanup.trim(input!!) }) ) assertEquals( - "<b>Foo <br /> Bar</b>", - Strings.cleanup("Foo
Bar
", UnaryOperator { input: String? -> - StringCleanup.escapeXml( - input - ) - }) + "<b>Foo <br /> Bar</b>", + Strings.cleanup("Foo
Bar
", UnaryOperator { input: String? -> + StringCleanup.escapeXml( + input + ) + }) ) assertEquals( - "Hello
World", - Strings.cleanup("Hello\nWorld", UnaryOperator { input: String? -> StringCleanup.nlToBr(input) }) + "Hello
World", + Strings.cleanup("Hello\nWorld", UnaryOperator { input: String? -> StringCleanup.nlToBr(input) }) ) assertEquals( - "Testalert('Hello World!')", Strings.cleanup("Test", + "Testalert('Hello World!')", Strings.cleanup( + "Test", UnaryOperator { input: String? -> StringCleanup.removeXml(input!!) }) ) assertEquals( - " äöüÄÖÜß<>\"'&* * * * * * ", - Strings.cleanup(" äöüÄÖÜß<>"'&••‣‣⁃⁃", - UnaryOperator { input: String? -> StringCleanup.decodeHtmlEntities(input!!) }) + " äöüÄÖÜß<>\"'&* * * * * * ", + Strings.cleanup( + " äöüÄÖÜß<>"'&••‣‣⁃⁃", + UnaryOperator { input: String? -> StringCleanup.decodeHtmlEntities(input!!) }) ) } From 7ede244a8e7b4f213e9d3e5bf87086f0d4054a00 Mon Sep 17 00:00:00 2001 From: Sascha Bieberstein Date: Thu, 21 Nov 2024 08:25:02 +0100 Subject: [PATCH 2/2] Hardens helper method signatur This way we dont have to define the not so clear case of `areAllEmpty()`. Also if only one String is present `isEmpty` should be used instead. --- src/main/java/sirius/kernel/commons/Strings.java | 16 ++++++++++------ .../kotlin/sirius/kernel/commons/StringsTest.kt | 2 ++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/sirius/kernel/commons/Strings.java b/src/main/java/sirius/kernel/commons/Strings.java index 81aeef8f..488154d6 100644 --- a/src/main/java/sirius/kernel/commons/Strings.java +++ b/src/main/java/sirius/kernel/commons/Strings.java @@ -25,6 +25,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Provides various helper methods for dealing with Java Strings @@ -119,15 +120,18 @@ public static boolean isFilled(@Nullable Object string) { /** * Checks if the string representations of the given objects are all "" or null. * - * @param strings the objects which are to be checked + * @param first the first object which is to be checked + * @param second the second object which is to be checked + * @param further additional objects to be checked * @return true if all strings are null or "", false if one of them is filled * @see #isEmpty(Object) */ - public static boolean areAllEmpty(Object... strings) { - for (Object string : strings) { - if (Strings.isFilled(string)) { - return false; - } + public static boolean areAllEmpty(Object first, Object second, Object... further) { + if (Strings.isFilled(first) || Strings.isFilled(second)) { + return false; + } + if (further != null) { + return Stream.of(further).allMatch(Strings::isEmpty); } return true; } diff --git a/src/test/kotlin/sirius/kernel/commons/StringsTest.kt b/src/test/kotlin/sirius/kernel/commons/StringsTest.kt index 86c7d21b..5eb3c116 100644 --- a/src/test/kotlin/sirius/kernel/commons/StringsTest.kt +++ b/src/test/kotlin/sirius/kernel/commons/StringsTest.kt @@ -41,9 +41,11 @@ class StringsTest { assertTrue { Strings.areAllEmpty("", "") } assertTrue { Strings.areAllEmpty("", null) } assertTrue { Strings.areAllEmpty(null, "") } + assertTrue { Strings.areAllEmpty(null, "", null, "") } assertFalse { Strings.areAllEmpty("Test", null) } assertFalse { Strings.areAllEmpty(null, "Test") } assertFalse { Strings.areAllEmpty("Test", "Test") } + assertFalse { Strings.areAllEmpty(null, "", null, "", "Test") } } @Test