Package io.vavr
Class Predicates
java.lang.Object
io.vavr.Predicates
Defines general-purpose predicates which are particularly useful when working with
API.Match
.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Predicate
<T> A combinator that checks if all of the givenpredicates
are satisfied.static <T> Predicate
<T> A combinator that checks if at least one of the givenpredicates
is satisfies.A combinator that checks if one or more elements of anIterable
satisfy thepredicate
.A combinator that checks if all elements of anIterable
satisfy thepredicate
.static <T> Predicate
<T> instanceOf
(Class<? extends T> type) Creates aPredicate
that tests, if an object is instance of the specifiedtype
.static <T> Predicate
<T> is
(T value) Creates aPredicate
that tests, if an object is equal to the specifiedvalue
usingObjects.equals(Object, Object)
for comparison.static <T> Predicate
<T> isIn
(T... values) Creates aPredicate
that tests, if an object is equal to at least one of the specifiedvalues
usingObjects.equals(Object, Object)
for comparison.static <T> Predicate
<T> Creates aPredicate
that tests, if an object is not nullstatic <T> Predicate
<T> isNull()
Creates aPredicate
that tests, if an object is nullstatic <T> Predicate
<T> A combinator that checks if none of the givenpredicates
is satisfied.static <T> Predicate
<T> Negates a givenPredicate
.
-
Constructor Details
-
Predicates
private Predicates()
-
-
Method Details
-
allOf
A combinator that checks if all of the givenpredicates
are satisfied.By definition
allOf
is satisfied if the givenpredicates
are empty.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
- closure over tested object types- Parameters:
predicates
- An array of predicates- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifpredicates
is null
-
anyOf
A combinator that checks if at least one of the givenpredicates
is satisfies.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
- closure over tested object types- Parameters:
predicates
- An array of predicates- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifpredicates
is null
-
exists
A combinator that checks if one or more elements of anIterable
satisfy thepredicate
.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
- Type Parameters:
T
- tested object type- Parameters:
predicate
- APredicate
that tests elements of typeT
- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifpredicate
is null
-
forAll
A combinator that checks if all elements of anIterable
satisfy thepredicate
.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
- Type Parameters:
T
- tested object type- Parameters:
predicate
- APredicate
that tests elements of typeT
- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifpredicate
is null
-
instanceOf
Creates aPredicate
that tests, if an object is instance of the specifiedtype
.Predicate<Object> instanceOfNumber = instanceOf(Number.class); instanceOfNumber.test(1); // true instanceOfNumber.test("1"); // false
- Type Parameters:
T
- tested object type- Parameters:
type
- A type- Returns:
- A new
Predicate
- Throws:
NullPointerException
- iftype
is null
-
is
Creates aPredicate
that tests, if 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
- Type Parameters:
T
- tested object type- Parameters:
value
- A value, may be null- Returns:
- A new
Predicate
-
isIn
Creates aPredicate
that tests, if 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
- Type Parameters:
T
- closure over tested object types- Parameters:
values
- an array of values of type T- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifvalues
is null
-
isNotNull
Creates aPredicate
that tests, if an object is not nullPredicate<Integer> isNotNull = isNotNull(); isNotNull.test(0); // true isNotNull.test(null); // false
- Type Parameters:
T
- tested object type- Returns:
- A new
Predicate
-
isNull
Creates aPredicate
that tests, if an object is nullPredicate<Integer> isNull = isNull(); isNull.test(null); // true isNull.test(0); // false
- Type Parameters:
T
- tested object type- Returns:
- A new
Predicate
-
noneOf
A combinator that checks if none of the givenpredicates
is satisfied.Naturally
noneOf
is satisfied if the givenpredicates
are empty.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
- closure over tested object types- Parameters:
predicates
- An array of predicates- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifpredicates
is null
-
not
Negates a 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
- tested object type- Parameters:
predicate
- APredicate
that tests elements of typeT
- Returns:
- A new
Predicate
- Throws:
NullPointerException
- ifpredicate
is null
-