Skip to content

Commit d04e679

Browse files
deesejashekhargulati
authored andcommitted
Adding isBlank method (shekhargulati#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
1 parent cf1d373 commit d04e679

4 files changed

Lines changed: 44 additions & 2 deletions

File tree

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ hs_err_pid*
7171

7272
!gradle/wrapper/gradle-wrapper.jar
7373

74+
/bin/
75+
76+
.classpath
77+
.project
78+
.settings

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,19 @@ join(new String[]{"hello","world","123"}, ";")
618618
// result => "hello;world;123")
619619
```
620620

621+
## isBlank
622+
623+
isEmpty returns true if a string is blank or null and false otherwise.
624+
625+
```java
626+
isBlank("")
627+
// result => true)
628+
isBlank(null)
629+
// result => true)
630+
isBlank("test")
631+
// result => false)
632+
```
633+
621634
## Inspiration
622635

623636
This library is inspired by [dleitee/strman](https://github.com/dleitee/strman).

src/main/java/strman/Strman.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,16 @@ public static Map<Character, Long> charsCount(String input) {
13051305
.mapToObj(c -> (char) c)
13061306
.collect(groupingBy(identity(), counting()));
13071307
}
1308+
1309+
/**
1310+
* Checks if string is empty. This is a null safe check and will return true when string is null.
1311+
*
1312+
* @param input The input string
1313+
* @return true if string is null or empty
1314+
*/
1315+
public static boolean isBlank(String input) {
1316+
return input == null || input.isEmpty();
1317+
}
13081318

13091319
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
13101320
if (predicate.test(value)) {
@@ -1325,5 +1335,6 @@ private static long countSubstr(String value, String subStr, boolean allowOverla
13251335
}
13261336
return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count);
13271337
}
1338+
13281339
}
13291340

src/test/java/strman/StrmanTest.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
import static org.hamcrest.collection.IsArrayWithSize.emptyArray;
3838
import static org.junit.Assert.*;
3939
import static strman.Strman.*;
40-
import static strman.Strman.endsWith;
41-
import static strman.Strman.format;
4240

4341
public class StrmanTest {
4442

@@ -1027,4 +1025,19 @@ public void charsCount_shouldReturnCharsCountWhenInputIsAComplexString(){
10271025

10281026
assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput));
10291027
}
1028+
1029+
@Test
1030+
public void isBlank_shouldReturnTrueIfNull() {
1031+
assertTrue(isBlank(null));
1032+
}
1033+
1034+
@Test
1035+
public void isBlank_shouldReturnTrueIfEmpty() {
1036+
assertTrue(isBlank(""));
1037+
}
1038+
1039+
@Test
1040+
public void isBlank_shouldReturnFalseIfNotEmpty() {
1041+
assertFalse(isBlank("ac"));
1042+
}
10301043
}

0 commit comments

Comments
 (0)