Package io.vavr
Class Predicates
java.lang.Object
io.vavr.Predicates
Defines general-purpose predicates which are particularly useful when working with
API.Match.- Author:
- Daniel Dietrich, Grzegorz Piwowarek
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Predicate<T> Returns a predicate that is satisfied only when all of the providedpredicatesreturntruefor a given input.static <T> Predicate<T> Returns a predicate that is satisfied if at least one of the providedpredicatesreturnstruefor a given input.Returns a predicate that checks if at least one element in anIterablesatisfies the givenpredicate.Returns a predicate that checks if all elements in anIterablesatisfy the givenpredicate.static <T> Predicate<T> instanceOf(@NonNull Class<? extends T> type) Returns aPredicatethat tests whether an object is an instance of the specifiedtype.static <T> Predicate<T> is(T value) Returns aPredicatethat tests whether an object is equal to the specifiedvalue, usingObjects.equals(Object, Object)for comparison.static <T> Predicate<T> isIn(T @NonNull ... values) Returns aPredicatethat tests whether an object is equal to at least one of the specifiedvalues, usingObjects.equals(Object, Object)for comparison.static <T> Predicate<T> Returns aPredicatethat tests whether an object is notnull.static <T> Predicate<T> isNull()Returns aPredicatethat tests whether an object isnull.static <T> Predicate<T> Returns a predicate that is satisfied if none of the providedpredicatesreturntruefor a given input.static <T> Predicate<T> Returns a predicate that negates the result of the givenpredicate.
-
Method Details
-
allOf
Returns a predicate that is satisfied only when all of the providedpredicatesreturntruefor a given input.If no predicates are supplied, the resulting predicate always evaluates to
true.Predicate<Integer> isGreaterThanOne = i -> i > 1; Predicate<Integer> isGreaterThanTwo = i -> i > 2; allOf().test(0); // true allOf(isGreaterThanOne, isGreaterThanTwo).test(3); // true allOf(isGreaterThanOne, isGreaterThanTwo).test(2); // false- Type Parameters:
T- the input type- Parameters:
predicates- the predicates to combine- Returns:
- a predicate representing the logical conjunction of all given predicates
- Throws:
NullPointerException- ifpredicatesis null
-
anyOf
Returns a predicate that is satisfied if at least one of the providedpredicatesreturnstruefor a given input.If no predicates are supplied, the resulting predicate always evaluates to
false.Predicate<Integer> isGreaterThanOne = i -> i > 1; Predicate<Integer> isGreaterThanTwo = i -> i > 2; anyOf().test(0); // false anyOf(isGreaterThanOne, isGreaterThanTwo).test(3); // true anyOf(isGreaterThanOne, isGreaterThanTwo).test(2); // true anyOf(isGreaterThanOne, isGreaterThanTwo).test(1); // false- Type Parameters:
T- the input type- Parameters:
predicates- the predicates to combine- Returns:
- a predicate representing the logical disjunction of all given predicates
- Throws:
NullPointerException- ifpredicatesis null
-
exists
Returns a predicate that checks if at least one element in anIterablesatisfies the givenpredicate.If the
Iterableis empty, the resulting predicate evaluates tofalse.Predicate<Integer> isGreaterThanOne = i -> i > 1; Predicate<Iterable<Integer>> existsGreaterThanOne = exists(isGreaterThanOne); existsGreaterThanOne.test(List.of(0, 1, 2)); // true existsGreaterThanOne.test(List.of(0, 1)); // false existsGreaterThanOne.test(Collections.emptyList()); // false- Type Parameters:
T- the type of elements in theIterable- Parameters:
predicate- the predicate to test elements against- Returns:
- a predicate that evaluates to
trueif any element satisfiespredicate - Throws:
NullPointerException- ifpredicateis null
-
forAll
Returns a predicate that checks if all elements in anIterablesatisfy the givenpredicate.If the
Iterableis empty, the resulting predicate evaluates totrue.Predicate<Integer> isGreaterThanOne = i -> i > 1; Predicate<Iterable<Integer>> forAllGreaterThanOne = forAll(isGreaterThanOne); forAllGreaterThanOne.test(List.of(0, 1, 2)); // false forAllGreaterThanOne.test(List.of(2, 3, 4)); // true forAllGreaterThanOne.test(Collections.emptyList()); // true- Type Parameters:
T- the type of elements in theIterable- Parameters:
predicate- the predicate to test elements against- Returns:
- a predicate that evaluates to
trueonly if all elements satisfypredicate - Throws:
NullPointerException- ifpredicateis null
-
instanceOf
Returns aPredicatethat tests whether an object is an instance of the specifiedtype.Predicate<Object> instanceOfNumber = instanceOf(Number.class); instanceOfNumber.test(1); // true instanceOfNumber.test("1"); // false- Type Parameters:
T- the type of objects being tested- Parameters:
type- the class to test instances against- Returns:
- a predicate that evaluates to
trueif the object is an instance oftype - Throws:
NullPointerException- iftypeis null
-
is
Returns aPredicatethat tests whether an object is equal to the specifiedvalue, usingObjects.equals(Object, Object)for comparison.Predicate<Integer> isOne = is(1); isOne.test(1); // true isOne.test(2); // false isOne.test(null); // false- Type Parameters:
T- the type of object being tested- Parameters:
value- the value to compare against; may benull- Returns:
- a predicate that evaluates to
trueif the tested object equalsvalue
-
isIn
Returns aPredicatethat tests whether an object is equal to at least one of the specifiedvalues, usingObjects.equals(Object, Object)for comparison.Predicate<Integer> isIn = isIn(1, 2, 3); isIn.test(1); // true isIn.test(0); // false isIn.test(null); // false- Type Parameters:
T- the type of objects being tested- Parameters:
values- the values to compare against; may not benull- Returns:
- a predicate that evaluates to
trueif the tested object equals any of the specifiedvalues - Throws:
NullPointerException- ifvaluesis null
-
isNotNull
Returns aPredicatethat tests whether an object is notnull.Predicate<Integer> isNotNull = isNotNull(); isNotNull.test(0); // true isNotNull.test(null); // false- Type Parameters:
T- the type of object being tested- Returns:
- a predicate that evaluates to
trueif the tested object is notnull
-
isNull
Returns aPredicatethat tests whether an object isnull.Predicate<Integer> isNull = isNull(); isNull.test(null); // true isNull.test(0); // false- Type Parameters:
T- the type of object being tested- Returns:
- a predicate that evaluates to
trueif the tested object isnull
-
noneOf
Returns a predicate that is satisfied if none of the providedpredicatesreturntruefor a given input.If no predicates are supplied, the resulting predicate always evaluates to
true.Predicate<Integer> isGreaterThanOne = i -> i > 1; Predicate<Integer> isGreaterThanTwo = i -> i > 2; noneOf().test(0); // true noneOf(isGreaterThanOne, isGreaterThanTwo).test(1); // true noneOf(isGreaterThanOne, isGreaterThanTwo).test(2); // false- Type Parameters:
T- the input type- Parameters:
predicates- the predicates to combine- Returns:
- a predicate that evaluates to
trueonly if all predicates returnfalse - Throws:
NullPointerException- ifpredicatesis null
-
not
Returns a predicate that negates the result of the givenpredicate.// Negates a method reference Predicate<String> isNotNull1 = not(Objects::isNull); isNotNull1.test(""); // true isNotNull1.test(null); // false // Negates a predicate instance Predicate<String> isNotNull2 = not(Predicates.isNull()); isNotNull2.test(""); // true isNotNull2.test(null); // false- Type Parameters:
T- the type of objects being tested- Parameters:
predicate- the predicate to negate- Returns:
- a predicate that evaluates to
trueif the original predicate evaluates tofalse, and vice versa - Throws:
NullPointerException- ifpredicateis null
-