Skip to content

Add isNullOrEmpty, isNullOrBlank and isNotNullOrBlank to Strings. #10

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

Merged
merged 1 commit into from
Jun 2, 2014
Merged
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
48 changes: 48 additions & 0 deletions core/src/main/java/fj/function/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import fj.F;
import fj.F2;
import fj.data.Stream;
import static fj.Function.curry;
import static fj.function.Booleans.not;
import static fj.function.Characters.isWhitespace;

/**
* Curried string functions.
Expand All @@ -14,8 +17,41 @@ private Strings() {
throw new UnsupportedOperationException();
}

/**
* This function checks if a given String contains any non-whitespace character
* (according to {@link Character#isWhitespace(char)}) and if it's also not
* <code>null</code> and not empty ("").
*
* @see #isNullOrBlank
* @see Character#isWhitespace(char)
* @see Characters#isWhitespace
*/
public static final F<String, Boolean> isNotNullOrBlank = new F<String, Boolean>() {
@Override
public Boolean f(final String a) {
return isNotNullOrEmpty.f(a) && Stream.fromString(a).find(not(isWhitespace)).isSome();
}
};

/**
* This function checks if a given String is whitespace (according to {@link Character#isWhitespace(char)}),
* empty ("") or <code>null</code>.
*
* @see #isNotNullOrBlank
* @see Character#isWhitespace(char)
* @see Characters#isWhitespace
*/
public static final F<String, Boolean> isNullOrBlank = new F<String, Boolean>() {
@Override
public Boolean f(final String a) {
return isNullOrEmpty.f(a) || Stream.fromString(a).find(not(isWhitespace)).isNone();
}
};

/**
* This function checks if a given String is neither <code>null</code> nor empty.
*
* @see #isNullOrEmpty
*/
public static final F<String, Boolean> isNotNullOrEmpty = new F<String, Boolean>() {
@Override
Expand All @@ -24,6 +60,18 @@ public Boolean f(final String a) {
}
};

/**
* This function checks if a given String is <code>null</code> or empty ({@link String#isEmpty()}).
*
* @see #isNotNullOrEmpty
*/
public static final F<String, Boolean> isNullOrEmpty = new F<String, Boolean>() {
@Override
public Boolean f(final String a) {
return a == null || a.length() == 0;
}
};

/**
* A curried version of {@link String#isEmpty()}.
*/
Expand Down