forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.java
More file actions
113 lines (102 loc) · 3.17 KB
/
Copy pathStrings.java
File metadata and controls
113 lines (102 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package fj.function;
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.
*
* @version %build.number%
*/
public final class Strings {
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
public Boolean f(final String a) {
return a != null && a.length() > 0;
}
};
/**
* 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()}.
*/
public static final F<String, Boolean> isEmpty = new F<String, Boolean>() {
public Boolean f(final String s) {
return s.length() == 0;
}
};
/**
* A curried version of {@link String#length()}.
*/
public static final F<String, Integer> length = new F<String, Integer>() {
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<String, F<String, Boolean>> contains = curry(new F2<String, String, Boolean>() {
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<String, F<String, Boolean>> matches = curry(new F2<String, String, Boolean>() {
public Boolean f(final String s1, final String s2) {
return s2.matches(s1);
}
});
}