7
7
import java .util .Iterator ;
8
8
9
9
public class Func {
10
+ public static interface Each <T > {
11
+ public void apply (T t );
12
+ }
13
+
10
14
public static interface Function <T ,R > {
11
15
public R apply (T t );
12
16
}
@@ -17,15 +21,42 @@ public static interface Reducer<T,E> {
17
21
18
22
public static <T ,R > ArrayList <R > map (Iterable <T > xs , Function <T ,R > f ) {
19
23
ArrayList <R > to = new ArrayList <>();
24
+ if (xs == null ) return to ;
25
+
20
26
for (T x : xs ) {
21
27
R ret = f .apply (x );
22
28
to .add (ret );
23
29
}
24
30
return to ;
25
31
}
26
32
33
+ public static <T > void each (Iterable <T > xs , Each <T > f ) {
34
+ if (xs == null ) return ;
35
+ for (T x : xs ) {
36
+ f .apply (x );
37
+ }
38
+ }
39
+
40
+ public static ArrayList <Integer > toList (int ... xs ){
41
+ ArrayList <Integer > to = new ArrayList <>();
42
+ for (int x : xs ) {
43
+ to .add (x );
44
+ }
45
+ return to ;
46
+ }
47
+
48
+ public static <T > ArrayList <T > toList (T ... xs ){
49
+ ArrayList <T > to = new ArrayList <>();
50
+ for (T x : xs ) {
51
+ to .add (x );
52
+ }
53
+ return to ;
54
+ }
55
+
27
56
public static <T > ArrayList <T > toList (Iterable <T > xs ){
28
57
ArrayList <T > to = new ArrayList <>();
58
+ if (xs == null ) return to ;
59
+
29
60
for (T x : xs ) {
30
61
to .add (x );
31
62
}
@@ -34,6 +65,8 @@ public static <T> ArrayList<T> toList(Iterable<T> xs){
34
65
35
66
public static <T > ArrayList <T > filter (Iterable <T > xs , Predicate <T > predicate ){
36
67
ArrayList <T > to = new ArrayList <>();
68
+ if (xs == null ) return to ;
69
+
37
70
for (T x : xs ) {
38
71
if (predicate .apply (x )){
39
72
to .add (x );
@@ -43,6 +76,8 @@ public static <T> ArrayList<T> filter(Iterable<T> xs, Predicate<T> predicate){
43
76
}
44
77
45
78
public static <T > T first (Iterable <T > xs , Predicate <T > predicate ){
79
+ if (xs == null ) return null ;
80
+
46
81
for (T x : xs ) {
47
82
if (predicate .apply (x )){
48
83
return x ;
@@ -51,7 +86,22 @@ public static <T> T first(Iterable<T> xs, Predicate<T> predicate){
51
86
return null ;
52
87
}
53
88
89
+ public static <T > T first (Iterable <T > xs ){
90
+ if (xs == null ) return null ;
91
+
92
+ for (T x : xs ) {
93
+ return x ;
94
+ }
95
+ return null ;
96
+ }
97
+
98
+ public static <T > T first (T [] xs ) {
99
+ return xs == null || xs .length == 0 ? null : xs [0 ];
100
+ }
101
+
54
102
public static <T > T last (Iterable <T > xs , Predicate <T > predicate ){
103
+ if (xs == null ) return null ;
104
+
55
105
for (T x : reverse (xs )) {
56
106
if (predicate .apply (x )){
57
107
return x ;
@@ -60,13 +110,29 @@ public static <T> T last(Iterable<T> xs, Predicate<T> predicate){
60
110
return null ;
61
111
}
62
112
113
+ public static <T > T last (Iterable <T > xs ){
114
+ if (xs == null ) return null ;
115
+
116
+ T last = null ;
117
+ for (T x : xs ) {
118
+ last = x ;
119
+ }
120
+ return last ;
121
+ }
122
+
123
+ public static <T > T last (T [] xs ) {
124
+ return xs == null ? null : xs [xs .length - 1 ];
125
+ }
126
+
63
127
public static <T > boolean contains (Iterable <T > xs , Predicate <T > predicate ){
64
128
return first (xs , predicate ) != null ;
65
129
}
66
130
67
131
public static <T > ArrayList <T > skip (Iterable <T > xs , int skip ){
68
132
int i = 0 ;
69
133
ArrayList <T > to = new ArrayList <>();
134
+ if (xs == null ) return to ;
135
+
70
136
for (T x : xs ) {
71
137
if (i ++ >= skip ){
72
138
to .add (x );
@@ -77,6 +143,8 @@ public static <T> ArrayList<T> skip(Iterable<T> xs, int skip){
77
143
78
144
public static <T > ArrayList <T > skip (Iterable <T > xs , Predicate <T > predicate ){
79
145
ArrayList <T > to = new ArrayList <>();
146
+ if (xs == null ) return to ;
147
+
80
148
for (T x : xs ) {
81
149
if (predicate .apply (x )){
82
150
to .add (x );
@@ -87,6 +155,8 @@ public static <T> ArrayList<T> skip(Iterable<T> xs, Predicate<T> predicate){
87
155
88
156
public static <T > ArrayList <T > take (Iterable <T > xs , Predicate <T > predicate ){
89
157
ArrayList <T > to = new ArrayList <>();
158
+ if (xs == null ) return to ;
159
+
90
160
for (T x : xs ) {
91
161
if (predicate .apply (x )){
92
162
return to ;
@@ -99,6 +169,8 @@ public static <T> ArrayList<T> take(Iterable<T> xs, Predicate<T> predicate){
99
169
public static <T > ArrayList <T > take (Iterable <T > xs , int take ){
100
170
int i = 0 ;
101
171
ArrayList <T > to = new ArrayList <>();
172
+ if (xs == null ) return to ;
173
+
102
174
for (T x : xs ) {
103
175
if (i ++ >= take ){
104
176
return to ;
@@ -109,6 +181,8 @@ public static <T> ArrayList<T> take(Iterable<T> xs, int take){
109
181
}
110
182
111
183
public static <T > boolean any (Iterable <T > xs , Predicate <T > predicate ){
184
+ if (xs == null ) return false ;
185
+
112
186
for (T x : xs ) {
113
187
if (predicate .apply (x )){
114
188
return true ;
@@ -118,6 +192,8 @@ public static <T> boolean any(Iterable<T> xs, Predicate<T> predicate){
118
192
}
119
193
120
194
public static <T > boolean all (Iterable <T > xs , Predicate <T > predicate ){
195
+ if (xs == null ) return false ;
196
+
121
197
for (T x : xs ) {
122
198
if (!predicate .apply (x )){
123
199
return false ;
@@ -128,6 +204,8 @@ public static <T> boolean all(Iterable<T> xs, Predicate<T> predicate){
128
204
129
205
public static <T > ArrayList <T > expand (Iterable <T >... xss ){
130
206
ArrayList <T > to = new ArrayList <>();
207
+ if (xss == null ) return to ;
208
+
131
209
for (Iterable <T > xs : xss ) {
132
210
for (T x : xs ){
133
211
to .add (x );
@@ -137,6 +215,8 @@ public static <T> ArrayList<T> expand(Iterable<T>... xss){
137
215
}
138
216
139
217
public static <T > T elementAt (Iterable <T > xs , int index ){
218
+ if (xs == null ) return null ;
219
+
140
220
int i = 0 ;
141
221
for (T x : xs ){
142
222
if (i ++ == index ){
@@ -147,12 +227,16 @@ public static <T> T elementAt(Iterable<T> xs, int index){
147
227
}
148
228
149
229
public static <T > ArrayList <T > reverse (Iterable <T > xs ){
230
+ if (xs == null ) return new ArrayList <T >();
231
+
150
232
ArrayList <T > clone = toList (xs );
151
233
Collections .reverse (clone );
152
234
return clone ;
153
235
}
154
236
155
237
public static <T ,E > E reduce (Iterable <T > xs , E initialValue , Reducer <T ,E > reducer ){
238
+ if (xs == null ) return initialValue ;
239
+
156
240
E currentValue = initialValue ;
157
241
for (T x : xs ){
158
242
currentValue = reducer .reduce (currentValue , x );
@@ -166,6 +250,8 @@ public static <T,E> E reduceRight(Iterable<T> xs, E initialValue, Reducer<T,E> r
166
250
167
251
public static <T > String join (Iterable <T > xs , String separator ){
168
252
StringBuilder sb = new StringBuilder ();
253
+ if (xs == null ) return sb .toString ();
254
+
169
255
for (T x : xs ){
170
256
if (sb .length () > 0 )
171
257
sb .append (separator );
0 commit comments