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
6 changes: 3 additions & 3 deletions src/main/java/com/nitorcreations/collections/NSets.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public static <V> Set<V> asSet(Iterable<V> values) {
/**
* Create a new set with the values of the given iterator
*
* @param values the values to add to the set.
* @param iterator the values to add to the set.
* @param <V> the type of the element
* @return the set containing the values
*/
public static <V> Set<V> asSet(Iterator<V> values) {
return NStreams.asStream(values).collect(toSet());
public static <V> Set<V> asSet(Iterator<V> iterator) {
return asSet(() -> iterator);
}
}
50 changes: 50 additions & 0 deletions src/main/java/com/nitorcreations/predicates/NPredicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

public final class NPredicates {
private NPredicates() { /** prevent instantiation */}
Expand Down Expand Up @@ -142,4 +143,53 @@ public static <T> Predicate<T> having(Function<T, Boolean> fn) {
public static <T> Predicate<T> notHaving(Function<T, Boolean> fn) {
return having(fn, equalTo(false));
}


/**
* Matches all of target predicates. Similar to {@code p1.and(p2).and(p3)...}
* @param preds predicates
* @param <T> type of predicate
* @return predicate
*/
@SafeVarargs
public static <T> Predicate<T> allOf(Predicate<T>... preds) {
arrayNotEmpty(preds);
return Stream.of(preds).reduce(always(), (p1, p2) -> p1.and(p2));
}

/**
* Matches any of target predicates. Similar to {@code p1.or(p2).or(p3)...}
* @param preds predicates
* @param <T> type of predicate
* @return predicate
*/
@SafeVarargs
public static <T> Predicate<T> anyOf(Predicate<T>... preds) {
arrayNotEmpty(preds);
return Stream.of(preds).reduce(never(), (p1, p2) -> p1.or(p2));
}

private static <T> void arrayNotEmpty(T[] arr) {
if (arr.length == 0) {
throw new IllegalArgumentException("Empty list of predicates");
}
}

/**
* Predicate that always returns {@code true}
* @param <T> type of predicate
* @return predicate
*/
public static <T> Predicate<T> always() {
return ignore -> true;
}

/**
* Predicate that always returns {@code false}
* @param <T> type of predicate
* @return predicate
*/
public static <T> Predicate<T> never() {
return ignore -> false;
}
}
113 changes: 113 additions & 0 deletions src/main/java/com/nitorcreations/predicates/NStringPredicates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
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;

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

/**
* Null-safely check that the string starts with the prefix. The predicate returns {@code false} if
* target is {@code null}.
*
* @param prefix the prefix to match
* @return predicate
*/
public static Predicate<String> startsWith(String prefix) {
return NPredicates.<String>notNull().and(s -> s.startsWith(prefix));
}

/**
* Null-safely check that the string ends with the suffix. The predicate returns {@code false} if
* target is {@code null}.
*
* @param suffix the suffix to match
* @return predicate
*/
public static Predicate<String> endsWith(String suffix) {
return NPredicates.<String>notNull().and(s -> s.endsWith(suffix));
}

/**
* Check that the non-null string is either empty or contains only whitespace. The predicate returns {@code true} if
* target is {@code null}.
*
* @return predicate
*/
public static Predicate<String> isBlank() {
return NPredicates.<String>isNull().or(s -> s.trim().isEmpty());
}

/**
* Check that the non-null string is either empty or contains only whitespace. The predicate returns {@code false} if
* target is {@code null}.
*
* @return predicate
*/
public static Predicate<String> notBlank() {
return not(isBlank());
}

/**
* Check that the non-null string contains the substring
*
* @param substring the string to find
* @return predicate
*/
public static Predicate<String> contains(String substring) {
return containsAll(substring);
}

/**
* Check that the non-null string contains all of the substrings in any order
*
* @param substring the strings to find
* @return predicate
*/
public static Predicate<String> containsAll(String... substring) {
return NPredicates.<String> notNull()
.and(s -> Arrays.stream(substring)
.filter(Objects::nonNull)
.map(s::contains)
.reduce(true, Boolean::logicalAnd));
}

/**
* Check that the non-null string contains any of the substrings
*
* @param substring the strings to find
* @return predicate
*/
public static Predicate<String> containsAny(String... substring) {
return NPredicates.<String> notNull()
.and(s -> Arrays.stream(substring)
.filter(Objects::nonNull)
.anyMatch(s::contains));
}

/**
* Check that the non-null string does not contain the substring. The predicate will return {@code false}
* is target is {@code null}
*
* @param substring the string to find
* @return predicate
*/
public static Predicate<String> doesNotContain(String substring) {
return doesNotContainAnyOf(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}
*
* @param substring the string to find
* @return predicate
*/
public static Predicate<String> doesNotContainAnyOf(String... substring) {
return not(containsAny(substring)).and(notNull());
}
}
16 changes: 14 additions & 2 deletions src/test/java/com/nitorcreations/collections/NSetsTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.nitorcreations.collections;

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

import java.lang.reflect.InvocationTargetException;
import java.util.Set;

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

public class NSetsTest {
Expand All @@ -28,4 +28,16 @@ public void testAsSet_nullValues() {
final Set<String> foo = asSet(null, null, null, "Foo");
assertThat(foo).hasSize(2).contains(null, "Foo");
}

@Test
public void testAsSet_iterable() {
final Set<String> strings = asSet(asList("foo", "bar", "baz"));
assertThat(strings).contains("foo", "bar", "baz");
}

@Test
public void testAsSet_iterator() {
final Set<String> strings = asSet(asList("foo", "bar", "baz").iterator());
assertThat(strings).contains("foo", "bar", "baz");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.nitorcreations.predicates;

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;

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


/**
* Checks that the iterable is non-null and not empty
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> notEmpty() {
return NPredicates.<S>notNull().and(s -> s.iterator().hasNext());
}

/**
* Checks that the iterable is non-null and empty
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> empty() {
return NPredicates.<S>notNull().and(s -> !s.iterator().hasNext());
}

/**
* Checks that the iterable is non-null and contains target element (comparison by {@code #equals})
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> contains(T element) {
return NPredicates.<S>notNull().and(it -> asStream(it).anyMatch(equalTo(element)));
}

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

/**
* Checks that the iterable is non-null and contains any of the target elements (comparison by {@code #equals})
* @param <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> containsAny(T... elements) {
final Predicate<S> anyMatches = Arrays.stream(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 <T> type of an element
* @param <S> type of the iterable
* @return predicate
*/
public static <T, S extends Iterable<T>> Predicate<S> doesNotContain(T element) {
return doesNotContainAnyOf(element);
}

/**
* Checks that the iterable is non-null and contains none of target 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(T... elements) {
return NPredicates.<S>notNull().and(not(containsAny(elements)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.nitorcreations.predicates;

import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;

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

public class NCollectionPredicatesTest {

@Test
public void forCoverage() throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
invokePrivateConstructor(NCollectionPredicates.class);
}

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

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

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

@Test
public void testContainsAll() {
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));
}

@Test
public void testContainsAny() {
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));
}

@Test
public void testDoesNotContain() {
final Long num = 666_666l;
assertThat(doesNotContain(num))
.matchesAll(emptyList(), asList(113l))
.matchesNone(null, asList(1l, num), asSet(num), asList(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));
}

}
Loading