16
16
*/
17
17
package org .geektimes .commons .function ;
18
18
19
+ import org .geektimes .commons .lang .util .ArrayUtils ;
20
+
19
21
import java .util .LinkedList ;
20
22
import java .util .List ;
21
23
import java .util .Queue ;
30
32
import static java .util .Collections .unmodifiableList ;
31
33
import static java .util .Collections .unmodifiableSet ;
32
34
import static java .util .stream .Collectors .toList ;
35
+ import static java .util .stream .Collectors .toSet ;
33
36
import static org .geektimes .commons .collection .util .CollectionUtils .*;
34
- import static org .geektimes .commons .function .Predicates .*;
37
+ import static org .geektimes .commons .function .Predicates .and ;
38
+ import static org .geektimes .commons .function .Predicates .or ;
35
39
36
40
/**
37
41
* The utilities class for {@link Stream}
@@ -45,121 +49,86 @@ static <T> Stream<T> stream(Iterable<T> iterable) {
45
49
}
46
50
47
51
static <T , I extends Iterable <T >> Stream <T > filterStream (I values , Predicate <? super T > predicate ) {
48
- return filterStream (values , predicate , EMPTY_ARRAY );
52
+ return stream (values ). filter ( predicate );
49
53
}
50
54
51
55
static <T , I extends Iterable <T >> Stream <T > filterStream (I values , Predicate <? super T >... predicates ) {
52
- return filterStream (values , alwaysTrue (), predicates );
53
- }
54
-
55
- static <T , I extends Iterable <T >> Stream <T > filterStream (I values , Predicate <? super T > predicate ,
56
- Predicate <? super T >... otherPredicates ) {
57
- return stream (values ).filter (and (predicate , otherPredicates ));
56
+ return ArrayUtils .isEmpty (predicates ) ? stream (values ) : filterStream (values , and (predicates ));
58
57
}
59
58
60
59
static <E , L extends List <E >> List <E > filter (L values , Predicate <? super E > predicate ) {
61
- return filter (values , predicate , EMPTY_ARRAY );
62
- }
63
-
64
- static <E , L extends List <E >> List <E > filter (L values , Predicate <? super E >... predicates ) {
65
- return filter (values , alwaysTrue (), predicates );
66
- }
67
-
68
- static <E , L extends List <E >> List <E > filter (L values , Predicate <? super E > predicate , Predicate <? super E >... otherPredicates ) {
69
60
final L result ;
70
61
if (predicate == null ) {
71
62
result = values ;
72
63
} else {
73
- result = (L ) filterStream (values , predicate , otherPredicates ).collect (toList ());
64
+ result = (L ) filterStream (values , predicate ).collect (toList ());
74
65
}
75
66
return unmodifiableList (result );
76
67
}
77
68
78
- static <E , S extends Set <E >> Set <E > filter (S values , Predicate <? super E > predicate ) {
79
- return filter (values , predicate , EMPTY_ARRAY );
80
- }
81
-
82
- static <E , S extends Set <E >> Set <E > filter (S values , Predicate <? super E >... predicates ) {
83
- return filter (values , alwaysTrue (), predicates );
69
+ static <E , L extends List <E >> List <E > filter (L values , Predicate <? super E >... predicates ) {
70
+ return filter (values , and (predicates ));
84
71
}
85
72
86
- static <E , S extends Set <E >> Set <E > filter (S values , Predicate <? super E > predicate ,
87
- Predicate <? super E >... otherPredicates ) {
73
+ static <E , S extends Set <E >> Set <E > filter (S values , Predicate <? super E > predicate ) {
88
74
final S result ;
89
75
if (predicate == null ) {
90
76
result = values ;
91
77
} else {
92
- result = (S ) filterStream (values , predicate , otherPredicates ).collect (Collectors . toSet ());
78
+ result = (S ) filterStream (values , predicate ).collect (toSet ());
93
79
}
94
80
return unmodifiableSet (result );
95
81
}
96
82
97
- static <E , Q extends Queue <E >> Queue <E > filter (Q values , Predicate <? super E > predicate ) {
98
- return filter (values , predicate , EMPTY_ARRAY );
99
- }
100
-
101
- static <E , Q extends Queue <E >> Queue <E > filter (Q values , Predicate <? super E >... predicates ) {
102
- return filter (values , alwaysTrue (), predicates );
83
+ static <E , S extends Set <E >> Set <E > filter (S values , Predicate <? super E >... predicates ) {
84
+ return filter (values , and (predicates ));
103
85
}
104
86
105
- static <E , Q extends Queue <E >> Queue <E > filter (Q values , Predicate <? super E > predicate ,
106
- Predicate <? super E >... otherPredicates ) {
87
+ static <E , Q extends Queue <E >> Queue <E > filter (Q values , Predicate <? super E > predicate ) {
107
88
final Q result ;
108
89
if (predicate == null ) {
109
90
result = values ;
110
91
} else {
111
- result = (Q ) filterStream (values , predicate , otherPredicates )
112
- .collect (LinkedList ::new , List ::add , List ::addAll );
92
+ result = (Q ) filterStream (values , predicate ).collect (LinkedList ::new , List ::add , List ::addAll );
113
93
}
114
94
return unmodifiableQueue (result );
115
95
}
116
96
117
- static <T , S extends Iterable <T >> S filter (S values , Predicate <? super T > predicate ) {
118
- return (S ) filter (values , predicate , EMPTY_ARRAY );
119
- }
120
-
121
- static <T , S extends Iterable <T >> S filter (S values , Predicate <? super T >... predicates ) {
122
- return filter (values , alwaysTrue (), predicates );
97
+ static <E , Q extends Queue <E >> Queue <E > filter (Q values , Predicate <? super E >... predicates ) {
98
+ return filter (values , and (predicates ));
123
99
}
124
100
125
- static <T , S extends Iterable <T >> S filter (S values , Predicate <? super T > predicate ,
126
- Predicate <? super T >... otherPredicates ) {
101
+ static <T , S extends Iterable <T >> S filter (S values , Predicate <? super T > predicate ) {
127
102
if (isSet (values )) {
128
- return (S ) filter ((Set ) values , predicate , otherPredicates );
103
+ return (S ) filter ((Set ) values , predicate );
129
104
} else if (isList (values )) {
130
- return (S ) filter ((List ) values , predicate , otherPredicates );
105
+ return (S ) filter ((List ) values , predicate );
131
106
} else if (isQueue (values )) {
132
- return (S ) filter ((Queue ) values , predicate , otherPredicates );
107
+ return (S ) filter ((Queue ) values , predicate );
133
108
}
134
109
String message = format ("The 'values' type can't be supported!" , values .getClass ().getName ());
135
110
throw new UnsupportedOperationException (message );
136
111
}
137
112
138
- static <T , S extends Iterable <T >> S filterAny (S values , Predicate <? super T >... predicates ) {
139
- return filterAny (values , alwaysTrue (), predicates );
113
+ static <T , S extends Iterable <T >> S filter (S values , Predicate <? super T >... predicates ) {
114
+ return filter (values , and ( predicates ) );
140
115
}
141
116
142
- static <T , S extends Iterable <T >> S filterAny (S values , Predicate <? super T > predicate ,
143
- Predicate <? super T >... otherPredicates ) {
144
- return filter (values , or (predicate , otherPredicates ));
117
+ static <T , S extends Iterable <T >> S filterAny (S values , Predicate <? super T >... predicates ) {
118
+ return filter (values , or (predicates ));
145
119
}
146
120
147
121
static <T > T filterFirst (Iterable <T > values , Predicate <? super T > predicate ) {
148
- return (T ) filterFirst (values , predicate , EMPTY_ARRAY );
149
- }
150
-
151
- static <T > T filterFirst (Iterable <T > values , Predicate <? super T >... predicates ) {
152
- return filterFirst (values , alwaysTrue (), predicates );
153
- }
154
-
155
- static <T > T filterFirst (Iterable <T > values , Predicate <? super T > predicate ,
156
- Predicate <? super T >... otherPredicates ) {
157
122
return stream (values )
158
- .filter (and ( predicate , otherPredicates ) )
123
+ .filter (predicate )
159
124
.findFirst ()
160
125
.orElse (null );
161
126
}
162
127
128
+ static <T > T filterFirst (Iterable <T > values , Predicate <? super T >... predicates ) {
129
+ return filterFirst (values , and (predicates ));
130
+ }
131
+
163
132
static <T , R > List <R > map (List <T > values , Function <T , R > mapper ) {
164
133
return stream (values )
165
134
.map (mapper )
@@ -169,7 +138,7 @@ static <T, R> List<R> map(List<T> values, Function<T, R> mapper) {
169
138
static <T , R > Set <R > map (Set <T > values , Function <T , R > mapper ) {
170
139
return stream (values )
171
140
.map (mapper )
172
- .collect (Collectors . toSet ());
141
+ .collect (toSet ());
173
142
}
174
143
}
175
144
0 commit comments