2
2
3
3
import java .util .Arrays ;
4
4
import java .util .function .Predicate ;
5
- import java .util .stream .Stream ;
6
5
7
6
import static com .nitorcreations .predicates .NPredicates .*;
8
7
import static com .nitorcreations .streams .NStreams .asStream ;
8
+ import static java .util .Arrays .asList ;
9
9
10
- public class NCollectionPredicates {
10
+ public final class NCollectionPredicates {
11
11
private NCollectionPredicates () { /** prevent instantiation */ }
12
12
13
13
@@ -47,8 +47,20 @@ public static <T, S extends Iterable<T>> Predicate<S> contains(T element) {
47
47
* @param <S> type of the iterable
48
48
* @return predicate
49
49
*/
50
+ @ SafeVarargs
50
51
public static <T , S extends Iterable <T >> Predicate <S > containsAll (T ... elements ) {
51
- final Predicate <S > allmatch = Stream .of (elements )
52
+ return containsAll (asList (elements ));
53
+ }
54
+
55
+ /**
56
+ * Checks that the iterable is non-null and contains all of the target elements (comparison by {@code #equals})
57
+ * @param elements
58
+ * @param <T> type of an element
59
+ * @param <S> type of the iterable
60
+ * @return predicate
61
+ */
62
+ public static <T , S extends Iterable <T >> Predicate <S > containsAll (Iterable <T > elements ) {
63
+ final Predicate <S > allmatch = asStream (elements )
52
64
.map (NCollectionPredicates ::<T , S >contains )
53
65
.reduce (notEmpty (), (p1 , p2 ) -> p1 .and (p2 ));
54
66
return NPredicates .<S >notNull ().and (allmatch );
@@ -60,16 +72,28 @@ public static <T, S extends Iterable<T>> Predicate<S> containsAll(T... elements)
60
72
* @param <S> type of the iterable
61
73
* @return predicate
62
74
*/
75
+ @ SafeVarargs
63
76
public static <T , S extends Iterable <T >> Predicate <S > containsAny (T ... elements ) {
64
- final Predicate <S > anyMatches = Arrays .stream (elements )
77
+ return containsAny (asList (elements ));
78
+ }
79
+
80
+ /**
81
+ * Checks that the iterable is non-null and contains any of the target elements (comparison by {@code #equals})
82
+ * @param elements
83
+ * @param <T> type of an element
84
+ * @param <S> type of the iterable
85
+ * @return predicate
86
+ */
87
+ public static <T , S extends Iterable <T >> Predicate <S > containsAny (Iterable <T > elements ) {
88
+ final Predicate <S > anyMatches = asStream (elements )
65
89
.map (NCollectionPredicates ::<T , S >contains )
66
90
.reduce (never (), (p1 , p2 ) -> p1 .or (p2 ));
67
91
return NPredicates .<S >notNull ().and (anyMatches );
68
92
}
69
93
70
-
71
94
/**
72
95
* Checks that the iterable is non-null and does not contain the target element (comparison by {@code #equals})
96
+ * @param element the element
73
97
* @param <T> type of an element
74
98
* @param <S> type of the iterable
75
99
* @return predicate
@@ -80,11 +104,24 @@ public static <T, S extends Iterable<T>> Predicate<S> doesNotContain(T element)
80
104
81
105
/**
82
106
* Checks that the iterable is non-null and contains none of target elements
107
+ * @param elements elements
83
108
* @param <T> type of an element
84
109
* @param <S> type of the iterable
85
110
* @return predicate
86
111
*/
112
+ @ SafeVarargs
87
113
public static <T , S extends Iterable <T >> Predicate <S > doesNotContainAnyOf (T ... elements ) {
114
+ return doesNotContainAnyOf (Arrays .asList (elements ));
115
+ }
116
+
117
+ /**
118
+ * Checks that the iterable is non-null and contains none of target elements
119
+ * @param elements elements
120
+ * @param <T> type of an element
121
+ * @param <S> type of the iterable
122
+ * @return predicate
123
+ */
124
+ public static <T , S extends Iterable <T >> Predicate <S > doesNotContainAnyOf (Iterable <T > elements ) {
88
125
return NPredicates .<S >notNull ().and (not (containsAny (elements )));
89
126
}
90
127
}
0 commit comments