From d04e679b07e58bb83f8293d8c6ebad6ada0147a6 Mon Sep 17 00:00:00 2001 From: Andy Deese Date: Fri, 19 May 2017 21:27:21 -0400 Subject: [PATCH] Adding isBlank method (#89) * added isBlank method, along with junit test and readme update * removing local files * adding gitignore with Eclipse specific files ignored * removed unused imports from Strman.java and split tests out to match current test convention --- .gitignore | 5 +++++ README.md | 13 +++++++++++++ src/main/java/strman/Strman.java | 11 +++++++++++ src/test/java/strman/StrmanTest.java | 17 +++++++++++++++-- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 48267e9..6c9b373 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,8 @@ hs_err_pid* !gradle/wrapper/gradle-wrapper.jar +/bin/ + +.classpath +.project +.settings diff --git a/README.md b/README.md index a671ca8..9bca612 100644 --- a/README.md +++ b/README.md @@ -618,6 +618,19 @@ join(new String[]{"hello","world","123"}, ";") // result => "hello;world;123") ``` +## isBlank + +isEmpty returns true if a string is blank or null and false otherwise. + +```java +isBlank("") +// result => true) +isBlank(null) +// result => true) +isBlank("test") +// result => false) +``` + ## Inspiration This library is inspired by [dleitee/strman](https://github.com/dleitee/strman). diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 08ddd2b..8f2eaa1 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -1305,6 +1305,16 @@ public abstract class Strman { .mapToObj(c -> (char) c) .collect(groupingBy(identity(), counting())); } + + /** + * Checks if string is empty. This is a null safe check and will return true when string is null. + * + * @param input The input string + * @return true if string is null or empty + */ + public static boolean isBlank(String input) { + return input == null || input.isEmpty(); + } private static void validate(String value, Predicate predicate, final Supplier supplier) { if (predicate.test(value)) { @@ -1325,5 +1335,6 @@ public abstract class Strman { } return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count); } + } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 84f18f6..328b962 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -37,8 +37,6 @@ 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.endsWith; -import static strman.Strman.format; public class StrmanTest { @@ -1027,4 +1025,19 @@ public class StrmanTest { assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput)); } + + @Test + public void isBlank_shouldReturnTrueIfNull() { + assertTrue(isBlank(null)); + } + + @Test + public void isBlank_shouldReturnTrueIfEmpty() { + assertTrue(isBlank("")); + } + + @Test + public void isBlank_shouldReturnFalseIfNotEmpty() { + assertFalse(isBlank("ac")); + } } \ No newline at end of file