Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provides a helper for checking if a bunch of strings are all empty #539

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/sirius/kernel/commons/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>Strings</tt>
Expand Down Expand Up @@ -116,6 +117,25 @@ 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 <tt>null</tt>.
*
* @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 <tt>true</tt> if all strings are <tt>null</tt> or "", <tt>false</tt> if one of them is filled
* @see #isEmpty(Object)
*/
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;
}

/**
* Compares the given <tt>Strings</tt> while treating upper- and lowercase characters as equal.
* <p>
Expand Down
106 changes: 64 additions & 42 deletions src/test/kotlin/sirius/kernel/commons/StringsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ class StringsTest {
assertTrue { Strings.isEmpty("") }
}

@Test
fun areAllEmpty() {
assertTrue { Strings.areAllEmpty(null, null) }
sabieber marked this conversation as resolved.
Show resolved Hide resolved
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
fun equalIgnoreCase() {
assertTrue { Strings.equalIgnoreCase("A", "a") }
Expand All @@ -51,7 +64,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) }
Expand Down Expand Up @@ -122,12 +135,12 @@ class StringsTest {
assertEquals(listOf<Any>(), 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)
)
}

Expand All @@ -142,9 +155,10 @@ class StringsTest {

@Test
fun replaceAll() {
assertEquals("A&lt;B&amp;C&amp;&amp;D&amp;;&amp;E", Strings.replaceAll(
assertEquals(
"A&lt;B&amp;C&amp;&amp;D&amp;;&amp;E", Strings.replaceAll(
Pattern.compile("&([a-zA-Z0-9]{0,6};?)"), "A&lt;B&C&&D&;&E"
) { s: String -> (if (s.endsWith(";") && !s.startsWith(";")) "&" else "&amp;") + s })
) { s: String -> (if (s.endsWith(";") && !s.startsWith(";")) "&" else "&amp;") + s })
}

@Test
Expand Down Expand Up @@ -175,31 +189,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!!) })
)
Expand All @@ -216,55 +232,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("<b>Test</b>",
"Test", Strings.cleanup(
"<b>Test</b>",
UnaryOperator { input: String? -> StringCleanup.replaceXml(input) },
UnaryOperator { input: String? -> StringCleanup.trim(input!!) })
)
assertEquals(
"Test", Strings.cleanup("<b>Test<br><img /></b>",
"Test", Strings.cleanup(
"<b>Test<br><img /></b>",
UnaryOperator { input: String? -> StringCleanup.replaceXml(input) },
UnaryOperator { input: String? -> StringCleanup.trim(input!!) })
)
assertEquals(
"Test Blubb", Strings.cleanup("<b>Test<br><img />Blubb</b>",
"Test Blubb", Strings.cleanup(
"<b>Test<br><img />Blubb</b>",
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(
"&lt;b&gt;Foo &lt;br /&gt; Bar&lt;/b&gt;",
Strings.cleanup("<b>Foo <br /> Bar</b>", UnaryOperator { input: String? ->
StringCleanup.escapeXml(
input
)
})
"&lt;b&gt;Foo &lt;br /&gt; Bar&lt;/b&gt;",
Strings.cleanup("<b>Foo <br /> Bar</b>", UnaryOperator { input: String? ->
StringCleanup.escapeXml(
input
)
})
)
assertEquals(
"Hello <br> World",
Strings.cleanup("Hello\nWorld", UnaryOperator { input: String? -> StringCleanup.nlToBr(input) })
"Hello <br> World",
Strings.cleanup("Hello\nWorld", UnaryOperator { input: String? -> StringCleanup.nlToBr(input) })
)
assertEquals(
"Testalert('Hello World!')", Strings.cleanup("Test<script>alert('Hello World!')</script>",
"Testalert('Hello World!')", Strings.cleanup(
"Test<script>alert('Hello World!')</script>",
UnaryOperator { input: String? -> StringCleanup.removeXml(input!!) })
)
assertEquals(
" äöüÄÖÜß<>\"'&* * * * * * ",
Strings.cleanup("&nbsp;&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&szlig;&lt;&gt;&quot;&apos;&amp;&#8226;&#8226;&#8227;&#8227;&#8259;&#8259;",
UnaryOperator { input: String? -> StringCleanup.decodeHtmlEntities(input!!) })
" äöüÄÖÜß<>\"'&* * * * * * ",
Strings.cleanup(
"&nbsp;&auml;&ouml;&uuml;&Auml;&Ouml;&Uuml;&szlig;&lt;&gt;&quot;&apos;&amp;&#8226;&#8226;&#8227;&#8227;&#8259;&#8259;",
UnaryOperator { input: String? -> StringCleanup.decodeHtmlEntities(input!!) })
)
}

Expand Down