From 0aa874fa933d73a53f597cd6298d6d90df47b4ee Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Sat, 21 May 2016 18:38:18 +0530 Subject: [PATCH] Fixed #18. Added support for `removeEmptyStrings` function --- src/main/java/strman/Strman.java | 13 +++++++++++++ src/test/java/strman/StrmanTest.java | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index f8e9a1e..87b8fa5 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -654,6 +654,19 @@ public abstract class Strman { return joiner.toString() + value; } + /** + * Remove empty Strings from string array + * + * @param strings Array of String to be cleaned + * @return Array of String without empty Strings + */ + public static String[] removeEmptyStrings(String[] strings) { + if (Objects.isNull(strings)) { + throw new IllegalArgumentException("Input array should not be null"); + } + return Arrays.stream(strings).filter(str -> str != null && !str.trim().isEmpty()).toArray(String[]::new); + } + public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 0a39540..fe9a277 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -9,6 +9,7 @@ import static java.util.stream.Collectors.toList; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.hasItems; import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; +import static org.hamcrest.collection.IsArrayWithSize.emptyArray; import static org.junit.Assert.*; import static strman.Strman.*; import static strman.Strman.format; @@ -498,4 +499,9 @@ public class StrmanTest { assertThat(prependArray("bar", new String[]{"foo"}), equalTo("foobar")); } + @Test + public void removeEmptyStrings_shouldRemoveEmptyStrings() throws Exception { + assertThat(removeEmptyStrings(new String[]{"aa", "", " ", "bb", "cc", null}), arrayContaining("aa", "bb", "cc")); + assertThat(removeEmptyStrings(new String[0]), emptyArray()); + } } \ No newline at end of file