fj.function.Strings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj.function;
import fj.F;
import fj.F2;
import fj.data.List;
import fj.data.Stream;
import static fj.Function.curry;
import static fj.function.Booleans.not;
import static fj.function.Characters.isWhitespace;
/**
* Curried string functions.
*
* @version %build.number%
*/
public final class Strings {
private Strings() {
throw new UnsupportedOperationException();
}
public static final String lineSeparator = System.getProperty("line.separator");
/**
* This function checks if a given String contains any non-whitespace character
* (according to {@link Character#isWhitespace(char)}) and if it's also not
* null
and not empty ("").
*
* @see #isNullOrBlank
* @see Character#isWhitespace(char)
* @see Characters#isWhitespace
*/
public static final F isNotNullOrBlank = new F() {
@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 null
.
*
* @see #isNotNullOrBlank
* @see Character#isWhitespace(char)
* @see Characters#isWhitespace
*/
public static final F isNullOrBlank = new F() {
@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 null
nor empty.
*
* @see #isNullOrEmpty
*/
public static final F isNotNullOrEmpty = new F() {
@Override
public Boolean f(final String a) {
return a != null && a.length() > 0;
}
};
/**
* This function checks if a given String is null
or empty ({@link String#isEmpty()}).
*
* @see #isNotNullOrEmpty
*/
public static final F isNullOrEmpty = new F() {
@Override
public Boolean f(final String a) {
return a == null || a.length() == 0;
}
};
/**
* A curried version of {@link String#isEmpty()}.
*/
public static final F isEmpty = new F() {
public Boolean f(final String s) {
return s.length() == 0;
}
};
/**
* A curried version of {@link String#length()}.
*/
public static final F length = new F() {
public Integer f(final String s) {
return s.length();
}
};
/**
* A curried version of {@link String#contains(CharSequence)}.
* The function returns true if the second argument contains the first.
*/
public static final F> contains = curry(new F2() {
public Boolean f(final String s1, final String s2) {
return s2.contains(s1);
}
});
/**
* A curried version of {@link String#matches(String)}.
* The function returns true if the second argument matches the first.
*/
public static final F> matches = curry(new F2() {
public Boolean f(final String s1, final String s2) {
return s2.matches(s1);
}
});
public static List lines(String s) {
return List.list(s.split("\\r?\\n"));
}
public static F> lines() {
return s -> lines(s);
}
public static String unlines(List list) {
StringBuilder sb = new StringBuilder();
list.intersperse(lineSeparator).forEach(s -> sb.append(s));
return sb.toString();
}
public static F, String> unlines() {
return l -> unlines(l);
}
}