Skip to content

Commit f6f36a7

Browse files
committed
Merge pull request #11 from NitorCreations/feature/predicate-fix
Fix predicate functionality and add more predicates
2 parents 622887c + a6816b0 commit f6f36a7

19 files changed

+238
-64
lines changed

src/test/java/com/nitorcreations/predicates/NCollectionPredicates.java renamed to src/main/java/com/nitorcreations/predicates/NCollectionPredicates.java

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import java.util.Arrays;
44
import java.util.function.Predicate;
5-
import java.util.stream.Stream;
65

76
import static com.nitorcreations.predicates.NPredicates.*;
87
import static com.nitorcreations.streams.NStreams.asStream;
8+
import static java.util.Arrays.asList;
99

10-
public class NCollectionPredicates {
10+
public final class NCollectionPredicates {
1111
private NCollectionPredicates() { /** prevent instantiation */}
1212

1313

@@ -47,8 +47,20 @@ public static <T, S extends Iterable<T>> Predicate<S> contains(T element) {
4747
* @param <S> type of the iterable
4848
* @return predicate
4949
*/
50+
@SafeVarargs
5051
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)
5264
.map(NCollectionPredicates::<T, S>contains)
5365
.reduce(notEmpty(), (p1, p2) -> p1.and(p2));
5466
return NPredicates.<S>notNull().and(allmatch);
@@ -60,16 +72,28 @@ public static <T, S extends Iterable<T>> Predicate<S> containsAll(T... elements)
6072
* @param <S> type of the iterable
6173
* @return predicate
6274
*/
75+
@SafeVarargs
6376
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)
6589
.map(NCollectionPredicates::<T, S>contains)
6690
.reduce(never(), (p1, p2) -> p1.or(p2));
6791
return NPredicates.<S>notNull().and(anyMatches);
6892
}
6993

70-
7194
/**
7295
* Checks that the iterable is non-null and does not contain the target element (comparison by {@code #equals})
96+
* @param element the element
7397
* @param <T> type of an element
7498
* @param <S> type of the iterable
7599
* @return predicate
@@ -80,11 +104,49 @@ public static <T, S extends Iterable<T>> Predicate<S> doesNotContain(T element)
80104

81105
/**
82106
* Checks that the iterable is non-null and contains none of target elements
107+
* @param elements elements
83108
* @param <T> type of an element
84109
* @param <S> type of the iterable
85110
* @return predicate
86111
*/
112+
@SafeVarargs
87113
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) {
88125
return NPredicates.<S>notNull().and(not(containsAny(elements)));
89126
}
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+
}
90152
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,23 @@ public static <T> Predicate<Optional<? extends T>> havingValue(T value) {
6666
return havingValue(equalTo(value));
6767
}
6868

69+
/**
70+
* Shorthand for {@code havingValue(not(predicate))}
71+
* @param predicate the predicate to apply to the optional's contents
72+
* @param <T> type of containing optional
73+
* @return the predicate
74+
*/
75+
public static <T> Predicate<Optional<? extends T>> notHavingValue(Predicate<T> predicate) {
76+
return havingValue(not(predicate));
77+
}
78+
79+
/**
80+
* Shorthand for {@code havingValue(not(equalTo(value)))}
81+
* @param value the value to compare the contents to
82+
* @param <T> type of containing optional
83+
* @return the predicate
84+
*/
85+
public static <T> Predicate<Optional<? extends T>> notHavingValue(T value) {
86+
return notHavingValue(equalTo(value));
87+
}
6988
}

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

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.nitorcreations.predicates;
22

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

76
import static com.nitorcreations.predicates.NPredicates.not;
87
import static com.nitorcreations.predicates.NPredicates.notNull;
8+
import static com.nitorcreations.streams.NStreams.asStream;
9+
import static java.util.Arrays.asList;
910

1011
public final class NStringPredicates {
1112
private NStringPredicates() { /** prevent instantiation */}
@@ -69,8 +70,18 @@ public static Predicate<String> contains(String substring) {
6970
* @return predicate
7071
*/
7172
public static Predicate<String> containsAll(String... substring) {
73+
return containsAll(asList(substring));
74+
}
75+
76+
/**
77+
* Check that the non-null string contains all of the substrings in any order
78+
*
79+
* @return predicate
80+
* @param substrings
81+
*/
82+
public static Predicate<String> containsAll(Iterable<String> substrings) {
7283
return NPredicates.<String> notNull()
73-
.and(s -> Arrays.stream(substring)
84+
.and(s -> asStream(substrings)
7485
.filter(Objects::nonNull)
7586
.map(s::contains)
7687
.reduce(true, Boolean::logicalAnd));
@@ -83,8 +94,18 @@ public static Predicate<String> containsAll(String... substring) {
8394
* @return predicate
8495
*/
8596
public static Predicate<String> containsAny(String... substring) {
97+
return containsAny(asList(substring));
98+
}
99+
100+
/**
101+
* Check that the non-null string contains any of the substrings
102+
*
103+
* @return predicate
104+
* @param substrings
105+
*/
106+
public static Predicate<String> containsAny(Iterable<String> substrings) {
86107
return NPredicates.<String> notNull()
87-
.and(s -> Arrays.stream(substring)
108+
.and(s -> asStream(substrings)
88109
.filter(Objects::nonNull)
89110
.anyMatch(s::contains));
90111
}
@@ -108,6 +129,39 @@ public static Predicate<String> doesNotContain(String substring) {
108129
* @return predicate
109130
*/
110131
public static Predicate<String> doesNotContainAnyOf(String... substring) {
111-
return not(containsAny(substring)).and(notNull());
132+
return doesNotContainAnyOf(asList(substring));
133+
}
134+
135+
/**
136+
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
137+
* is target is {@code null}
138+
*
139+
* @return predicate
140+
* @param substrings
141+
*/
142+
public static Predicate<String> doesNotContainAnyOf(Iterable<String> substrings) {
143+
return not(containsAny(substrings)).and(notNull());
144+
}
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());
112166
}
113167
}

src/main/java/com/nitorcreations/streams/NStreams.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public static <T> Stream<T> asStream(Iterable<T> iterable) {
7676
* @return a stream containing the values of the iterable
7777
*/
7878
public static <T> Stream<T> asStream(Iterable<T> iterable, boolean parallel) {
79-
return StreamSupport.stream(iterable.spliterator(), parallel);
79+
return Optional.ofNullable(iterable)
80+
.map(it -> StreamSupport.stream(it.spliterator(), parallel))
81+
.orElse(Stream.empty());
8082
}
8183

8284
/**

src/test/java/com/nitorcreations/collections/NMapsTest.java

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

3-
import com.nitorcreations.collections.NMaps;
43
import org.junit.Test;
54

65
import java.lang.reflect.InvocationTargetException;
@@ -9,10 +8,10 @@
98
import static com.nitorcreations.streams.NMappers.entryOf;
109
import static com.nitorcreations.collections.NMaps.*;
1110
import static com.nitorcreations.collections.NSets.asSet;
12-
import static com.nitorcreations.TestUtils.invokePrivateConstructor;
11+
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
1312
import static java.util.Arrays.asList;
1413
import static java.util.function.Function.identity;
15-
import static org.assertj.core.api.Assertions.assertThat;
14+
import static com.nitorcreations.test.Assertions.assertThat;
1615

1716
public class NMapsTest {
1817

src/test/java/com/nitorcreations/collections/NSetsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import java.lang.reflect.InvocationTargetException;
66
import java.util.Set;
77

8-
import static com.nitorcreations.TestUtils.invokePrivateConstructor;
8+
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
99
import static com.nitorcreations.collections.NSets.asSet;
1010
import static java.util.Arrays.asList;
11-
import static org.assertj.core.api.Assertions.assertThat;
11+
import static com.nitorcreations.test.Assertions.assertThat;
1212

1313
public class NSetsTest {
1414

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import java.util.ArrayList;
77
import java.util.HashSet;
88

9-
import static com.nitorcreations.TestUtils.invokePrivateConstructor;
9+
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
1010
import static com.nitorcreations.collections.NSets.asSet;
1111
import static com.nitorcreations.predicates.NCollectionPredicates.*;
12-
import static com.nitorcreations.predicates.PredicateAssert.assertThat;
12+
import static com.nitorcreations.test.Assertions.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/NComparablePredicatesTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
import java.lang.reflect.InvocationTargetException;
66

7-
import static com.nitorcreations.TestUtils.invokePrivateConstructor;
7+
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
88
import static com.nitorcreations.predicates.NComparablePredicates.*;
9-
import static com.nitorcreations.predicates.PredicateAssert.assertThat;
9+
import static com.nitorcreations.test.Assertions.assertThat;
1010

1111
public class NComparablePredicatesTest {
1212

0 commit comments

Comments
 (0)