Package io.vavr
Class Predicates
- java.lang.Object
-
- io.vavr.Predicates
-
public final class Predicates extends java.lang.ObjectDefines general-purpose predicates which are particularly useful when working withAPI.Match.
-
-
Constructor Summary
Constructors Modifier Constructor Description privatePredicates()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.function.Predicate<T>allOf(@NonNull java.util.function.Predicate<T> @NonNull ... predicates)Returns a predicate that is satisfied only when all of the providedpredicatesreturntruefor a given input.static <T> java.util.function.Predicate<T>anyOf(@NonNull java.util.function.Predicate<T> @NonNull ... predicates)Returns a predicate that is satisfied if at least one of the providedpredicatesreturnstruefor a given input.static <T> java.util.function.Predicate<java.lang.Iterable<T>>exists(@NonNull java.util.function.Predicate<? super T> predicate)Returns a predicate that checks if at least one element in anIterablesatisfies the givenpredicate.static <T> java.util.function.Predicate<java.lang.Iterable<T>>forAll(@NonNull java.util.function.Predicate<? super T> predicate)Returns a predicate that checks if all elements in anIterablesatisfy the givenpredicate.static <T> java.util.function.Predicate<T>instanceOf(@NonNull java.lang.Class<? extends T> type)Returns aPredicatethat tests whether an object is an instance of the specifiedtype.static <T> java.util.function.Predicate<T>is(T value)Returns aPredicatethat tests whether an object is equal to the specifiedvalue, usingObjects.equals(Object, Object)for comparison.static <T> java.util.function.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> java.util.function.Predicate<T>isNotNull()Returns aPredicatethat tests whether an object is notnull.static <T> java.util.function.Predicate<T>isNull()Returns aPredicatethat tests whether an object isnull.static <T> java.util.function.Predicate<T>noneOf(@NonNull java.util.function.Predicate<T> @NonNull ... predicates)Returns a predicate that is satisfied if none of the providedpredicatesreturntruefor a given input.static <T> java.util.function.Predicate<T>not(@NonNull java.util.function.Predicate<? super T> predicate)Returns a predicate that negates the result of the givenpredicate.
-
-
-
Method Detail
-
allOf
@SafeVarargs public static <T> java.util.function.Predicate<T> allOf(@NonNull java.util.function.Predicate<T> @NonNull ... predicates)
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:
java.lang.NullPointerException- ifpredicatesis null
-
anyOf
@SafeVarargs public static <T> java.util.function.Predicate<T> anyOf(@NonNull java.util.function.Predicate<T> @NonNull ... predicates)
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:
java.lang.NullPointerException- ifpredicatesis null
-
exists
public static <T> java.util.function.Predicate<java.lang.Iterable<T>> exists(@NonNull java.util.function.Predicate<? super T> predicate)
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:
java.lang.NullPointerException- ifpredicateis null
-
forAll
public static <T> java.util.function.Predicate<java.lang.Iterable<T>> forAll(@NonNull java.util.function.Predicate<? super T> predicate)
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:
java.lang.NullPointerException- ifpredicateis null
-
instanceOf
@GwtIncompatible public static <T> java.util.function.Predicate<T> instanceOf(@NonNull java.lang.Class<? extends T> type)
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:
java.lang.NullPointerException- iftypeis null
-
is
public static <T> java.util.function.Predicate<T> is(T value)
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
@SafeVarargs public static <T> java.util.function.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.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:
java.lang.NullPointerException- ifvaluesis null
-
isNotNull
public static <T> java.util.function.Predicate<T> 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
public static <T> java.util.function.Predicate<T> 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
@SafeVarargs public static <T> java.util.function.Predicate<T> noneOf(@NonNull java.util.function.Predicate<T> @NonNull ... predicates)
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:
java.lang.NullPointerException- ifpredicatesis null
-
not
public static <T> java.util.function.Predicate<T> not(@NonNull java.util.function.Predicate<? super T> predicate)
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:
java.lang.NullPointerException- ifpredicateis null
-
-