Skip to content

Commit c4fd01e

Browse files
author
Reko Jokelainen
committed
feat(predicates): add doesNotContainAllOf
1 parent 02a748f commit c4fd01e

File tree

4 files changed

+81
-21
lines changed

4 files changed

+81
-21
lines changed

src/main/java/com/nitorcreations/predicates/NCollectionPredicates.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,29 @@ public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAnyOf(T... e
124124
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAnyOf(Iterable<T> elements) {
125125
return NPredicates.<S>notNull().and(not(containsAny(elements)));
126126
}
127+
128+
/**
129+
* Checks that iterable is non-null and does not contain all of the target elements. Will return {@code true} if some of the elements are present
130+
*
131+
* @param elements elements to find
132+
* @param <T> type of an element
133+
* @param <S> type of the iterable
134+
* @return predicate
135+
*/
136+
@SafeVarargs
137+
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAllOf(T... elements) {
138+
return doesNotContainAllOf(asList(elements));
139+
}
140+
141+
/**
142+
* Checks that iterable is non-null and does not contain all of the target elements. Will return {@code true} if some of the elements are present
143+
*
144+
* @param elements
145+
* @param <T> type of an element
146+
* @param <S> type of the iterable
147+
* @return predicate
148+
*/
149+
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAllOf(Iterable<T> elements) {
150+
return NPredicates.<S>notNull().and(not(containsAll(elements)));
151+
}
127152
}

src/main/java/com/nitorcreations/predicates/NStringPredicates.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.nitorcreations.predicates;
22

3-
import java.util.List;
43
import java.util.Objects;
54
import java.util.function.Predicate;
65

@@ -80,9 +79,9 @@ public static Predicate<String> containsAll(String... substring) {
8079
* @return predicate
8180
* @param substrings
8281
*/
83-
public static Predicate<String> containsAll(List<String> substrings) {
82+
public static Predicate<String> containsAll(Iterable<String> substrings) {
8483
return NPredicates.<String> notNull()
85-
.and(s -> substrings.stream()
84+
.and(s -> asStream(substrings)
8685
.filter(Objects::nonNull)
8786
.map(s::contains)
8887
.reduce(true, Boolean::logicalAnd));
@@ -143,4 +142,26 @@ public static Predicate<String> doesNotContainAnyOf(String... substring) {
143142
public static Predicate<String> doesNotContainAnyOf(Iterable<String> substrings) {
144143
return not(containsAny(substrings)).and(notNull());
145144
}
145+
146+
/**
147+
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
148+
* is target is {@code null}
149+
*
150+
* @param substring the string to find
151+
* @return predicate
152+
*/
153+
public static Predicate<String> doesNotContainAllOf(String... substring) {
154+
return doesNotContainAllOf(asList(substring));
155+
}
156+
157+
/**
158+
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
159+
* is target is {@code null}
160+
*
161+
* @return predicate
162+
* @param substrings
163+
*/
164+
public static Predicate<String> doesNotContainAllOf(Iterable<String> substrings) {
165+
return not(containsAll(substrings)).and(notNull());
166+
}
146167
}

src/test/java/com/nitorcreations/predicates/NCollectionPredicatesTest.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static com.nitorcreations.predicates.PredicateAssert.assertThat;
1313
import static java.util.Arrays.asList;
1414
import static java.util.Collections.emptyList;
15+
import static java.util.Collections.singletonList;
1516

1617
public class NCollectionPredicatesTest {
1718

@@ -24,55 +25,61 @@ public void forCoverage() throws InvocationTargetException, NoSuchMethodExceptio
2425
public void testEmpty() {
2526
assertThat(empty())
2627
.matchesAll(new HashSet<>(), new ArrayList<>())
27-
.matchesNone(null, asList(1), asSet(1));
28+
.matchesNone(null, singletonList(1), asSet(1));
2829
}
2930

3031
@Test
3132
public void testNotEmpty() {
3233
assertThat(notEmpty())
33-
.matchesAll(asList(1), asSet(1))
34+
.matchesAll(singletonList(1), asSet(1))
3435
.matchesNone(null, new HashSet<>(), new ArrayList<>());
3536
}
3637

3738
@Test
3839
public void testContains() {
39-
final Long num = 666_666l;
40+
final Long num = 666_666L;
4041
assertThat(contains(num))
41-
.matchesAll(asList(1l, num), asSet(num), asList(666_666l))
42-
.matchesNone(null, emptyList(), asList(113l));
42+
.matchesAll(asList(1L, num), asSet(num), singletonList(666_666L))
43+
.matchesNone(null, emptyList(), singletonList(113L));
4344
}
4445

4546
@Test
4647
public void testContainsAll() {
47-
final Long n1 = 123_123l;
48-
final Long n2 = 321_321l;
48+
final Long n1 = 123_123L;
49+
final Long n2 = 321_321L;
4950
assertThat(containsAll(n1, n2))
5051
.matchesAll(asList(n1, n2), asList(n2, n1), asSet(n1, n2))
51-
.matchesNone(null, emptyList(), asList(113l));
52+
.matchesNone(null, emptyList(), singletonList(113L));
5253
}
5354

5455
@Test
5556
public void testContainsAny() {
56-
final Long n1 = 123_123l;
57-
final Long n2 = 321_321l;
57+
final Long n1 = 123_123L;
58+
final Long n2 = 321_321L;
5859
assertThat(containsAny(n1, n2))
59-
.matchesAll(asList(n1), asList(n2), asList(n1, n2), asList(n2, n1), asSet(n1, n2))
60-
.matchesNone(null, emptyList(), asList(113l));
60+
.matchesAll(singletonList(n1), singletonList(n2), asList(n1, n2), asList(n2, n1), asSet(n1, n2))
61+
.matchesNone(null, emptyList(), singletonList(113L));
6162
}
6263

6364
@Test
6465
public void testDoesNotContain() {
65-
final Long num = 666_666l;
66+
final Long num = 666_666L;
6667
assertThat(doesNotContain(num))
67-
.matchesAll(emptyList(), asList(113l))
68-
.matchesNone(null, asList(1l, num), asSet(num), asList(666_666l));
68+
.matchesAll(emptyList(), singletonList(113L))
69+
.matchesNone(null, asList(1L, num), asSet(num), singletonList(666_666L));
6970
}
7071

7172
@Test
7273
public void testDoesNotContainAnyOf() {
73-
assertThat(doesNotContainAnyOf(123l, 321l))
74-
.matchesAll(emptyList(), asList(113l))
75-
.matchesNone(null, asList(1l, 123l), asSet(321l), asList(2l, 3l, 123l, 321l));
74+
assertThat(doesNotContainAnyOf(123L, 321L))
75+
.matchesAll(emptyList(), singletonList(113L))
76+
.matchesNone(null, asList(1L, 123L), asSet(321L), asList(2L, 3L, 123L, 321L));
7677
}
7778

79+
@Test
80+
public void testDoesNotContainAllOf() {
81+
assertThat(doesNotContainAllOf(123L, 321L))
82+
.matchesAll(emptyList(), singletonList(123L), singletonList(321L))
83+
.matchesNone(null, asList(2L, 3L, 123L, 321L));
84+
}
7885
}

src/test/java/com/nitorcreations/predicates/NStringPredicatesTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,11 @@ public void testDoesNotContainAnyOf() {
8585
.matchesNone(null, "foo", "bar", "foobar", "ooffoo", "barfoo", "barfooazx");
8686
}
8787

88+
@Test
89+
public void testDoesNotContainAllOf() {
90+
assertThat(doesNotContainAllOf("foo", "bar"))
91+
.matchesAll("", "baz", "of", "fo", "fo of", "foo ba", "fo bar", "foo", "bar")
92+
.matchesNone(null, "foobar", "barfoo", "barfooazx");
93+
}
94+
8895
}

0 commit comments

Comments
 (0)