Package io.vavr

Class Predicates


  • public final class Predicates
    extends java.lang.Object
    Defines general-purpose predicates which are particularly useful when working with API.Match.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Predicates()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.function.Predicate<T> allOf​(java.util.function.Predicate<T>... predicates)
      A combinator that checks if all of the given predicates are satisfied.
      static <T> java.util.function.Predicate<T> anyOf​(java.util.function.Predicate<T>... predicates)
      A combinator that checks if at least one of the given predicates is satisfies.
      static <T> java.util.function.Predicate<java.lang.Iterable<T>> exists​(java.util.function.Predicate<? super T> predicate)
      A combinator that checks if one or more elements of an Iterable satisfy the predicate.
      static <T> java.util.function.Predicate<java.lang.Iterable<T>> forAll​(java.util.function.Predicate<? super T> predicate)
      A combinator that checks if all elements of an Iterable satisfy the predicate.
      static <T> java.util.function.Predicate<T> instanceOf​(java.lang.Class<? extends T> type)
      Creates a Predicate that tests, if an object is instance of the specified type.
      static <T> java.util.function.Predicate<T> is​(T value)
      Creates a Predicate that tests, if an object is equal to the specified value using Objects.equals(Object, Object) for comparison.
      static <T> java.util.function.Predicate<T> isIn​(T... values)
      Creates a Predicate that tests, if an object is equal to at least one of the specified values using Objects.equals(Object, Object) for comparison.
      static <T> java.util.function.Predicate<T> isNotNull()
      Creates a Predicate that tests, if an object is not null
      static <T> java.util.function.Predicate<T> isNull()
      Creates a Predicate that tests, if an object is null
      static <T> java.util.function.Predicate<T> noneOf​(java.util.function.Predicate<T>... predicates)
      A combinator that checks if none of the given predicates is satisfied.
      static <T> java.util.function.Predicate<T> not​(java.util.function.Predicate<? super T> predicate)
      Negates a given Predicate.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Predicates

        private Predicates()
    • Method Detail

      • allOf

        @SafeVarargs
        public static <T> java.util.function.Predicate<T> allOf​(java.util.function.Predicate<T>... predicates)
        A combinator that checks if all of the given predicates are satisfied.

        By definition allOf is satisfied if the given predicates 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:
        java.lang.NullPointerException - if predicates is null
      • anyOf

        @SafeVarargs
        public static <T> java.util.function.Predicate<T> anyOf​(java.util.function.Predicate<T>... predicates)
        A combinator that checks if at least one of the given predicates 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:
        java.lang.NullPointerException - if predicates is null
      • exists

        public static <T> java.util.function.Predicate<java.lang.Iterable<T>> exists​(java.util.function.Predicate<? super T> predicate)
        A combinator that checks if one or more elements of an Iterable satisfy the predicate.
        
         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 - A Predicate that tests elements of type T
        Returns:
        A new Predicate
        Throws:
        java.lang.NullPointerException - if predicate is null
      • forAll

        public static <T> java.util.function.Predicate<java.lang.Iterable<T>> forAll​(java.util.function.Predicate<? super T> predicate)
        A combinator that checks if all elements of an Iterable satisfy the predicate.
        
         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 - A Predicate that tests elements of type T
        Returns:
        A new Predicate
        Throws:
        java.lang.NullPointerException - if predicate is null
      • instanceOf

        @GwtIncompatible
        public static <T> java.util.function.Predicate<T> instanceOf​(java.lang.Class<? extends T> type)
        Creates a Predicate that tests, if an object is instance of the specified type.
        
         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:
        java.lang.NullPointerException - if type is null
      • is

        public static <T> java.util.function.Predicate<T> is​(T value)
        Creates a Predicate that tests, if 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
         
        Type Parameters:
        T - tested object type
        Parameters:
        value - A value, may be null
        Returns:
        A new Predicate
      • isIn

        @SafeVarargs
        public static <T> java.util.function.Predicate<T> isIn​(T... values)
        Creates a Predicate that tests, if 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
         
        Type Parameters:
        T - closure over tested object types
        Parameters:
        values - an array of values of type T
        Returns:
        A new Predicate
        Throws:
        java.lang.NullPointerException - if values is null
      • isNotNull

        public static <T> java.util.function.Predicate<T> isNotNull()
        Creates a Predicate that tests, if an object is not null
        
         Predicate<Integer> isNotNull = isNotNull();
         isNotNull.test(0);    // true
         isNotNull.test(null); // false
         
        Type Parameters:
        T - tested object type
        Returns:
        A new Predicate
      • isNull

        public static <T> java.util.function.Predicate<T> isNull()
        Creates a Predicate that tests, if an object is null
        
         Predicate<Integer> isNull = isNull();
         isNull.test(null); // true
         isNull.test(0);    // false
         
        Type Parameters:
        T - tested object type
        Returns:
        A new Predicate
      • noneOf

        @SafeVarargs
        public static <T> java.util.function.Predicate<T> noneOf​(java.util.function.Predicate<T>... predicates)
        A combinator that checks if none of the given predicates is satisfied.

        Naturally noneOf is satisfied if the given predicates 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:
        java.lang.NullPointerException - if predicates is null
      • not

        public static <T> java.util.function.Predicate<T> not​(java.util.function.Predicate<? super T> predicate)
        Negates a 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 - tested object type
        Parameters:
        predicate - A Predicate that tests elements of type T
        Returns:
        A new Predicate
        Throws:
        java.lang.NullPointerException - if predicate is null