Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import java.util.Arrays;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.nitorcreations.predicates.NPredicates.*;
import static com.nitorcreations.streams.NStreams.asStream;
import static java.util.Arrays.asList;

public class NCollectionPredicates {
public final class NCollectionPredicates {
private NCollectionPredicates() { /** prevent instantiation */}


Expand Down Expand Up @@ -47,8 +47,20 @@ public static <T, S extends Iterable<T>> Predicate<S> contains(T element) {
* @param <S> type of the iterable
* @return predicate
*/
@SafeVarargs
public static <T, S extends Iterable<T>> Predicate<S> containsAll(T... elements) {
final Predicate<S> allmatch = Stream.of(elements)
return containsAll(asList(elements));
}

/**
* Checks that the iterable is non-null and contains all of the target elements (comparison by {@code #equals})
* @param elements
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> containsAll(Iterable<T> elements) {
final Predicate<S> allmatch = asStream(elements)
.map(NCollectionPredicates::<T, S>contains)
.reduce(notEmpty(), (p1, p2) -> p1.and(p2));
return NPredicates.<S>notNull().and(allmatch);
Expand All @@ -60,16 +72,28 @@ public static <T, S extends Iterable<T>> Predicate<S> containsAll(T... elements)
* @param <S> type of the iterable
* @return predicate
*/
@SafeVarargs
public static <T, S extends Iterable<T>> Predicate<S> containsAny(T... elements) {
final Predicate<S> anyMatches = Arrays.stream(elements)
return containsAny(asList(elements));
}

/**
* Checks that the iterable is non-null and contains any of the target elements (comparison by {@code #equals})
* @param elements
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> containsAny(Iterable<T> elements) {
final Predicate<S> anyMatches = asStream(elements)
.map(NCollectionPredicates::<T, S>contains)
.reduce(never(), (p1, p2) -> p1.or(p2));
return NPredicates.<S>notNull().and(anyMatches);
}


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

/**
* Checks that the iterable is non-null and contains none of target elements
* @param elements elements
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
@SafeVarargs
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAnyOf(T... elements) {
return doesNotContainAnyOf(Arrays.asList(elements));
}

/**
* Checks that the iterable is non-null and contains none of target elements
* @param elements elements
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAnyOf(Iterable<T> elements) {
return NPredicates.<S>notNull().and(not(containsAny(elements)));
}

/**
* 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
*
* @param elements elements to find
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
@SafeVarargs
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAllOf(T... elements) {
return doesNotContainAllOf(asList(elements));
}

/**
* 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
*
* @param elements
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> doesNotContainAllOf(Iterable<T> elements) {
return NPredicates.<S>notNull().and(not(containsAll(elements)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,23 @@ public static <T> Predicate<Optional<? extends T>> havingValue(T value) {
return havingValue(equalTo(value));
}

/**
* Shorthand for {@code havingValue(not(predicate))}
* @param predicate the predicate to apply to the optional's contents
* @param <T> type of containing optional
* @return the predicate
*/
public static <T> Predicate<Optional<? extends T>> notHavingValue(Predicate<T> predicate) {
return havingValue(not(predicate));
}

/**
* Shorthand for {@code havingValue(not(equalTo(value)))}
* @param value the value to compare the contents to
* @param <T> type of containing optional
* @return the predicate
*/
public static <T> Predicate<Optional<? extends T>> notHavingValue(T value) {
return notHavingValue(equalTo(value));
}
}
62 changes: 58 additions & 4 deletions src/main/java/com/nitorcreations/predicates/NStringPredicates.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.nitorcreations.predicates;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Predicate;

import static com.nitorcreations.predicates.NPredicates.not;
import static com.nitorcreations.predicates.NPredicates.notNull;
import static com.nitorcreations.streams.NStreams.asStream;
import static java.util.Arrays.asList;

public final class NStringPredicates {
private NStringPredicates() { /** prevent instantiation */}
Expand Down Expand Up @@ -69,8 +70,18 @@ public static Predicate<String> contains(String substring) {
* @return predicate
*/
public static Predicate<String> containsAll(String... substring) {
return containsAll(asList(substring));
}

/**
* Check that the non-null string contains all of the substrings in any order
*
* @return predicate
* @param substrings
*/
public static Predicate<String> containsAll(Iterable<String> substrings) {
return NPredicates.<String> notNull()
.and(s -> Arrays.stream(substring)
.and(s -> asStream(substrings)
.filter(Objects::nonNull)
.map(s::contains)
.reduce(true, Boolean::logicalAnd));
Expand All @@ -83,8 +94,18 @@ public static Predicate<String> containsAll(String... substring) {
* @return predicate
*/
public static Predicate<String> containsAny(String... substring) {
return containsAny(asList(substring));
}

/**
* Check that the non-null string contains any of the substrings
*
* @return predicate
* @param substrings
*/
public static Predicate<String> containsAny(Iterable<String> substrings) {
return NPredicates.<String> notNull()
.and(s -> Arrays.stream(substring)
.and(s -> asStream(substrings)
.filter(Objects::nonNull)
.anyMatch(s::contains));
}
Expand All @@ -108,6 +129,39 @@ public static Predicate<String> doesNotContain(String substring) {
* @return predicate
*/
public static Predicate<String> doesNotContainAnyOf(String... substring) {
return not(containsAny(substring)).and(notNull());
return doesNotContainAnyOf(asList(substring));
}

/**
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
* is target is {@code null}
*
* @return predicate
* @param substrings
*/
public static Predicate<String> doesNotContainAnyOf(Iterable<String> substrings) {
return not(containsAny(substrings)).and(notNull());
}

/**
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
* is target is {@code null}
*
* @param substring the string to find
* @return predicate
*/
public static Predicate<String> doesNotContainAllOf(String... substring) {
return doesNotContainAllOf(asList(substring));
}

/**
* Check that the non-null string does not contain any of the the substrings. The predicate will return {@code false}
* is target is {@code null}
*
* @return predicate
* @param substrings
*/
public static Predicate<String> doesNotContainAllOf(Iterable<String> substrings) {
return not(containsAll(substrings)).and(notNull());
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/nitorcreations/streams/NStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public static <T> Stream<T> asStream(Iterable<T> iterable) {
* @return a stream containing the values of the iterable
*/
public static <T> Stream<T> asStream(Iterable<T> iterable, boolean parallel) {
return StreamSupport.stream(iterable.spliterator(), parallel);
return Optional.ofNullable(iterable)
.map(it -> StreamSupport.stream(it.spliterator(), parallel))
.orElse(Stream.empty());
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/com/nitorcreations/collections/NMapsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nitorcreations.collections;

import com.nitorcreations.collections.NMaps;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
Expand All @@ -9,10 +8,10 @@
import static com.nitorcreations.streams.NMappers.entryOf;
import static com.nitorcreations.collections.NMaps.*;
import static com.nitorcreations.collections.NSets.asSet;
import static com.nitorcreations.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
import static java.util.Arrays.asList;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
import static com.nitorcreations.test.Assertions.assertThat;

public class NMapsTest {

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/nitorcreations/collections/NSetsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

import static com.nitorcreations.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.collections.NSets.asSet;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static com.nitorcreations.test.Assertions.assertThat;

public class NSetsTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
import java.util.ArrayList;
import java.util.HashSet;

import static com.nitorcreations.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.collections.NSets.asSet;
import static com.nitorcreations.predicates.NCollectionPredicates.*;
import static com.nitorcreations.predicates.PredicateAssert.assertThat;
import static com.nitorcreations.test.Assertions.assertThat;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

public class NCollectionPredicatesTest {

Expand All @@ -24,55 +25,61 @@ public void forCoverage() throws InvocationTargetException, NoSuchMethodExceptio
public void testEmpty() {
assertThat(empty())
.matchesAll(new HashSet<>(), new ArrayList<>())
.matchesNone(null, asList(1), asSet(1));
.matchesNone(null, singletonList(1), asSet(1));
}

@Test
public void testNotEmpty() {
assertThat(notEmpty())
.matchesAll(asList(1), asSet(1))
.matchesAll(singletonList(1), asSet(1))
.matchesNone(null, new HashSet<>(), new ArrayList<>());
}

@Test
public void testContains() {
final Long num = 666_666l;
final Long num = 666_666L;
assertThat(contains(num))
.matchesAll(asList(1l, num), asSet(num), asList(666_666l))
.matchesNone(null, emptyList(), asList(113l));
.matchesAll(asList(1L, num), asSet(num), singletonList(666_666L))
.matchesNone(null, emptyList(), singletonList(113L));
}

@Test
public void testContainsAll() {
final Long n1 = 123_123l;
final Long n2 = 321_321l;
final Long n1 = 123_123L;
final Long n2 = 321_321L;
assertThat(containsAll(n1, n2))
.matchesAll(asList(n1, n2), asList(n2, n1), asSet(n1, n2))
.matchesNone(null, emptyList(), asList(113l));
.matchesNone(null, emptyList(), singletonList(113L));
}

@Test
public void testContainsAny() {
final Long n1 = 123_123l;
final Long n2 = 321_321l;
final Long n1 = 123_123L;
final Long n2 = 321_321L;
assertThat(containsAny(n1, n2))
.matchesAll(asList(n1), asList(n2), asList(n1, n2), asList(n2, n1), asSet(n1, n2))
.matchesNone(null, emptyList(), asList(113l));
.matchesAll(singletonList(n1), singletonList(n2), asList(n1, n2), asList(n2, n1), asSet(n1, n2))
.matchesNone(null, emptyList(), singletonList(113L));
}

@Test
public void testDoesNotContain() {
final Long num = 666_666l;
final Long num = 666_666L;
assertThat(doesNotContain(num))
.matchesAll(emptyList(), asList(113l))
.matchesNone(null, asList(1l, num), asSet(num), asList(666_666l));
.matchesAll(emptyList(), singletonList(113L))
.matchesNone(null, asList(1L, num), asSet(num), singletonList(666_666L));
}

@Test
public void testDoesNotContainAnyOf() {
assertThat(doesNotContainAnyOf(123l, 321l))
.matchesAll(emptyList(), asList(113l))
.matchesNone(null, asList(1l, 123l), asSet(321l), asList(2l, 3l, 123l, 321l));
assertThat(doesNotContainAnyOf(123L, 321L))
.matchesAll(emptyList(), singletonList(113L))
.matchesNone(null, asList(1L, 123L), asSet(321L), asList(2L, 3L, 123L, 321L));
}

@Test
public void testDoesNotContainAllOf() {
assertThat(doesNotContainAllOf(123L, 321L))
.matchesAll(emptyList(), singletonList(123L), singletonList(321L))
.matchesNone(null, asList(2L, 3L, 123L, 321L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import java.lang.reflect.InvocationTargetException;

import static com.nitorcreations.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.test.TestUtils.invokePrivateConstructor;
import static com.nitorcreations.predicates.NComparablePredicates.*;
import static com.nitorcreations.predicates.PredicateAssert.assertThat;
import static com.nitorcreations.test.Assertions.assertThat;

public class NComparablePredicatesTest {

Expand Down
Loading