From 94104fd50dafa0f932f2b4218fb6b9dd7404ff83 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Fri, 18 Apr 2014 18:37:28 +0200 Subject: [PATCH] Added additional trim methods for Strings --- .../java/com/rometools/utils/Strings.java | 32 ++++++++++++++++++- .../java/com/rometools/utils/StringsTest.java | 28 ++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/rometools/utils/Strings.java b/src/main/java/com/rometools/utils/Strings.java index 76f56e4..3ac0697 100644 --- a/src/main/java/com/rometools/utils/Strings.java +++ b/src/main/java/com/rometools/utils/Strings.java @@ -48,7 +48,7 @@ public final class Strings { } /** - * null-safe trimming of a String. + * Removes the whitespace at the beginning and end of a String. * * @param s The String to trim, may be null * @return null when the input String is null, the trimmed String otherwise @@ -61,6 +61,36 @@ public final class Strings { } } + /** + * Removes the whitespace at the beginning and end of a String. When the String only contains whitespace, it returns null. + * + * @param s The String to trim, may be null + * @return null when the input String is null or does only contain whitespace, the trimmed String otherwise + */ + public static String trimToNull(final String s) { + final String trimmed = trim(s); + if (trimmed == null || trimmed.isEmpty()) { + return null; + } else { + return trimmed; + } + } + + /** + * Removes the whitespace at the beginning and end of a String. When the String only contains whitespace, it returns null. + * + * @param s The String to trim, may be null + * @return null when the input String is null or does only contain whitespace, the trimmed String otherwise + */ + public static String trimToEmpty(final String s) { + final String trimmed = trim(s); + if (trimmed == null || trimmed.isEmpty()) { + return ""; + } else { + return trimmed; + } + } + /** * null-safe lower-case conversion of a String. * diff --git a/src/test/java/com/rometools/utils/StringsTest.java b/src/test/java/com/rometools/utils/StringsTest.java index 97e82ac..9983549 100644 --- a/src/test/java/com/rometools/utils/StringsTest.java +++ b/src/test/java/com/rometools/utils/StringsTest.java @@ -80,6 +80,34 @@ public class StringsTest { assertThat(Strings.trim(string), is("a")); } + @Test + public void testTrimToEmpty() { + + final String nullString = null; + final String emptyString = ""; + final String blankString = " "; + final String string = " a "; + + assertThat(Strings.trimToEmpty(nullString), is("")); + assertThat(Strings.trimToEmpty(emptyString), is("")); + assertThat(Strings.trimToEmpty(blankString), is("")); + assertThat(Strings.trimToEmpty(string), is("a")); + } + + @Test + public void testTrimToNull() { + + final String nullString = null; + final String emptyString = ""; + final String blankString = " "; + final String string = " a "; + + assertThat(Strings.trimToNull(nullString), is(nullValue())); + assertThat(Strings.trimToNull(emptyString), is(nullValue())); + assertThat(Strings.trimToNull(blankString), is(nullValue())); + assertThat(Strings.trimToNull(string), is("a")); + } + @Test public void testToLowerCase() {