Package io.vavr

Class Predicates

java.lang.Object
io.vavr.Predicates

public final class Predicates extends Object
Defines general-purpose predicates which are particularly useful when working with API.Match.
Author:
Daniel Dietrich, Grzegorz Piwowarek
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Predicate<T>
    allOf(@NonNull Predicate<T> @NonNull ... predicates)
    Returns a predicate that is satisfied only when all of the provided predicates return true for a given input.
    static <T> Predicate<T>
    anyOf(@NonNull Predicate<T> @NonNull ... predicates)
    Returns a predicate that is satisfied if at least one of the provided predicates returns true for a given input.
    static <T> Predicate<Iterable<T>>
    exists(@NonNull Predicate<? super T> predicate)
    Returns a predicate that checks if at least one element in an Iterable satisfies the given predicate.
    static <T> Predicate<Iterable<T>>
    forAll(@NonNull Predicate<? super T> predicate)
    Returns a predicate that checks if all elements in an Iterable satisfy the given predicate.
    static <T> Predicate<T>
    instanceOf(@NonNull Class<? extends T> type)
    Returns a Predicate that tests whether an object is an instance of the specified type.
    static <T> Predicate<T>
    is(T value)
    Returns a Predicate that tests whether an object is equal to the specified value, using Objects.equals(Object, Object) for comparison.
    static <T> Predicate<T>
    isIn(T @NonNull ... values)
    Returns a Predicate that tests whether an object is equal to at least one of the specified values, using Objects.equals(Object, Object) for comparison.
    static <T> Predicate<T>
    Returns a Predicate that tests whether an object is not null.
    static <T> Predicate<T>
    Returns a Predicate that tests whether an object is null.
    static <T> Predicate<T>
    noneOf(@NonNull Predicate<T> @NonNull ... predicates)
    Returns a predicate that is satisfied if none of the provided predicates return true for a given input.
    static <T> Predicate<T>
    not(@NonNull Predicate<? super T> predicate)
    Returns a predicate that negates the result of the given predicate.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • allOf

      @SafeVarargs public static <T> Predicate<T> allOf(@NonNull Predicate<T> @NonNull ... predicates)
      Returns a predicate that is satisfied only when all of the provided predicates return true for 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 - if predicates is null
    • anyOf

      @SafeVarargs public static <T> Predicate<T> anyOf(@NonNull Predicate<T> @NonNull ... predicates)
      Returns a predicate that is satisfied if at least one of the provided predicates returns true for 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 - if predicates is null
    • exists

      public static <T> Predicate<Iterable<T>> exists(@NonNull Predicate<? super T> predicate)
      Returns a predicate that checks if at least one element in an Iterable satisfies the given predicate.

      If the Iterable is empty, the resulting predicate evaluates to false.

      
       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 the Iterable
      Parameters:
      predicate - the predicate to test elements against
      Returns:
      a predicate that evaluates to true if any element satisfies predicate
      Throws:
      NullPointerException - if predicate is null
    • forAll

      public static <T> Predicate<Iterable<T>> forAll(@NonNull Predicate<? super T> predicate)
      Returns a predicate that checks if all elements in an Iterable satisfy the given predicate.

      If the Iterable is empty, the resulting predicate evaluates to true.

      
       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 the Iterable
      Parameters:
      predicate - the predicate to test elements against
      Returns:
      a predicate that evaluates to true only if all elements satisfy predicate
      Throws:
      NullPointerException - if predicate is null
    • instanceOf

      @GwtIncompatible public static <T> Predicate<T> instanceOf(@NonNull Class<? extends T> type)
      Returns a Predicate that tests whether an object is an instance of the specified type.
      
       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 true if the object is an instance of type
      Throws:
      NullPointerException - if type is null
    • is

      public static <T> Predicate<T> is(T value)
      Returns a Predicate that tests whether an object is equal to the specified value, using Objects.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 be null
      Returns:
      a predicate that evaluates to true if the tested object equals value
    • isIn

      @SafeVarargs public static <T> Predicate<T> isIn(T @NonNull ... values)
      Returns a Predicate that tests whether an object is equal to at least one of the specified values, using Objects.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 be null
      Returns:
      a predicate that evaluates to true if the tested object equals any of the specified values
      Throws:
      NullPointerException - if values is null
    • isNotNull

      public static <T> Predicate<T> isNotNull()
      Returns a Predicate that tests whether an object is not null.
      
       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 true if the tested object is not null
    • isNull

      public static <T> Predicate<T> isNull()
      Returns a Predicate that tests whether an object is null.
      
       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 true if the tested object is null
    • noneOf

      @SafeVarargs public static <T> Predicate<T> noneOf(@NonNull Predicate<T> @NonNull ... predicates)
      Returns a predicate that is satisfied if none of the provided predicates return true for 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 true only if all predicates return false
      Throws:
      NullPointerException - if predicates is null
    • not

      public static <T> Predicate<T> not(@NonNull Predicate<? super T> predicate)
      Returns a predicate that negates the result of the given predicate.
      
       // 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 true if the original predicate evaluates to false, and vice versa
      Throws:
      NullPointerException - if predicate is null