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