Skip to content

Commit 67c44b7

Browse files
author
Reko Jokelainen
committed
feat(predicates): overloads for collections
1 parent 622887c commit 67c44b7

File tree

4 files changed

+90
-10
lines changed

4 files changed

+90
-10
lines changed

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

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

3-
import java.util.Arrays;
3+
import java.util.List;
44
import java.util.Objects;
55
import java.util.function.Predicate;
66

77
import static com.nitorcreations.predicates.NPredicates.not;
88
import static com.nitorcreations.predicates.NPredicates.notNull;
9+
import static com.nitorcreations.streams.NStreams.asStream;
10+
import static java.util.Arrays.asList;
911

1012
public final class NStringPredicates {
1113
private NStringPredicates() { /** prevent instantiation */}
@@ -69,8 +71,18 @@ public static Predicate<String> contains(String substring) {
6971
* @return predicate
7072
*/
7173
public static Predicate<String> containsAll(String... substring) {
74+
return containsAll(asList(substring));
75+
}
76+
77+
/**
78+
* Check that the non-null string contains all of the substrings in any order
79+
*
80+
* @return predicate
81+
* @param substrings
82+
*/
83+
public static Predicate<String> containsAll(List<String> substrings) {
7284
return NPredicates.<String> notNull()
73-
.and(s -> Arrays.stream(substring)
85+
.and(s -> substrings.stream()
7486
.filter(Objects::nonNull)
7587
.map(s::contains)
7688
.reduce(true, Boolean::logicalAnd));
@@ -83,8 +95,18 @@ public static Predicate<String> containsAll(String... substring) {
8395
* @return predicate
8496
*/
8597
public static Predicate<String> containsAny(String... substring) {
98+
return containsAny(asList(substring));
99+
}
100+
101+
/**
102+
* Check that the non-null string contains any of the substrings
103+
*
104+
* @return predicate
105+
* @param substrings
106+
*/
107+
public static Predicate<String> containsAny(Iterable<String> substrings) {
86108
return NPredicates.<String> notNull()
87-
.and(s -> Arrays.stream(substring)
109+
.and(s -> asStream(substrings)
88110
.filter(Objects::nonNull)
89111
.anyMatch(s::contains));
90112
}
@@ -108,6 +130,17 @@ public static Predicate<String> doesNotContain(String substring) {
108130
* @return predicate
109131
*/
110132
public static Predicate<String> doesNotContainAnyOf(String... substring) {
111-
return not(containsAny(substring)).and(notNull());
133+
return doesNotContainAnyOf(asList(substring));
134+
}
135+
136+
/**
137+
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
138+
* is target is {@code null}
139+
*
140+
* @return predicate
141+
* @param substrings
142+
*/
143+
public static Predicate<String> doesNotContainAnyOf(Iterable<String> substrings) {
144+
return not(containsAny(substrings)).and(notNull());
112145
}
113146
}

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/predicates/NCollectionPredicates.java

Lines changed: 42 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,24 @@ 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
}
90127
}

src/test/java/com/nitorcreations/streams/NStreamsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.List;
2525
import java.util.NoSuchElementException;
2626
import java.util.Optional;
27+
import java.util.stream.Collectors;
28+
import java.util.stream.Stream;
2729

2830
import static com.nitorcreations.streams.NStreams.asStream;
2931
import static com.nitorcreations.TestUtils.invokePrivateConstructor;
@@ -61,6 +63,12 @@ public void iteratorToStream() {
6163
assertThat(actual).containsExactlyElementsOf(list);
6264
}
6365

66+
@Test
67+
public void nullToEmptyStream() {
68+
final Stream<String> actual = asStream((Iterable<String>) null);
69+
assertThat(actual.collect(Collectors.toList())).isEmpty();
70+
}
71+
6472
@Test
6573
public void iteratorToStream_parallel() {
6674
final List<Integer> actual = asStream(list.iterator(), true).collect(toList());

0 commit comments

Comments
 (0)