Class Predicates


  • public final class Predicates
    extends java.lang.Object
    Static utility methods pertaining to Predicate instances.

    All of the returned predicates are serializable if given serializable parameters.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static Predicate<java.lang.Object> FALSE
      a predicate who's result is always false.
      static Predicate<java.lang.Object> IS_NULL
      a predicate that evaluates to true if the reference being tested is null.
      static Predicate<java.lang.Object> NON_NULL
      a predicate that evaluates to true if the reference being tested is not null.
      static Predicate<java.lang.Object> TRUE
      a predicate who's result is always true.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Predicates()
      singleton utils
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Predicate<T> alwaysFalse()
      Returns a predicate who's result is always false.
      static <T> Predicate<T> alwaysTrue()
      Returns a predicate who's result is always true.
      static <T> Predicate<T> and​(Predicate<? super T>... components)
      Returns a predicate that evaluates to true if all of the component predicates evaluate to true.
      (package private) static <T> Predicate<T> and​(Predicate<? super T> first, Predicate<? super T>... components)
      Returns a predicate that evaluates to true if all of the component predicates evaluate to true.
      (package private) static <T> Predicate<T> and​(Predicate<? super T> first, java.lang.Iterable<Predicate<? super T>> components)
      Returns a predicate that evaluates to true if all of the component predicates evaluate to true.
      static <T> Predicate<T> and​(Predicate<T> first, Predicate<? super T> second)
      Returns a predicate that evaluates to true if all of the component predicates evaluate to true.
      static <T> Predicate<T> and​(java.lang.Iterable<Predicate<? super T>> components)
      Returns a predicate that evaluates to true if all of the component predicates evaluate to true.
      static <T> Predicate<T> contains​(java.util.Collection<? extends T> target)
      Creates a predicate that evaluates to true if the tested object is a member of the provided collection.
      static <T> Predicate<T> containsKey​(java.util.Map<? extends T,​?> target)
      Creates a predicate that evaluates to true if the tested object is a key in the provided map.
      static <T> Predicate<T> instanceOf​(java.lang.Class<?> clazz)
      Returns a predicate that evaluates to true if the object being tested is an instance of the provided class.
      static <T> Predicate<T> isEqual​(T target)
      Returns a predicate who's result matches Objects.equals(target, t).
      static <T> Predicate<T> isNull()
      Returns a predicate that evaluates to true if the reference being tested is null.
      static <T> Predicate<T> isSame​(T target)
      Returns a predicate that who's result is target == object.
      static <T> Predicate<T> negate​(Predicate<? super T> predicate)
      Returns a predicate that evaluates to true if the provided predicate evaluates to false
      static <T> Predicate<T> nonNull()
      Returns a predicate that evaluates to true if the reference being tested is non-null.
      static <T> Predicate<T> or​(Predicate<? super T>... components)
      Returns a predicate that evaluates to true if any of the component predicates evaluate to true.
      (package private) static <T> Predicate<T> or​(Predicate<? super T> first, java.lang.Iterable<Predicate<? super T>> components)
      Returns a predicate that evaluates to true if any of the component predicates evaluate to true.
      static <T> Predicate<T> or​(Predicate<T> first, Predicate<? super T> second)
      Returns a predicate that evaluates to true if any of the component predicates evaluate to true.
      (package private) static <T> Predicate<T> or​(Predicate<T> first, Predicate<? super T>... components)
      Returns a predicate that evaluates to true if any of the component predicates evaluate to true.
      static <T> Predicate<T> or​(java.lang.Iterable<Predicate<? super T>> components)
      Returns a predicate that evaluates to true if any of the component predicates evaluate to true.
      private static <T> java.util.List<T> safeCopyOf​(java.lang.Iterable<T> iterable)  
      private static <T> T[] safeCopyOf​(T... array)  
      private static <T> java.util.List<T> safeCopyOf​(T first, java.lang.Iterable<T> iterable)  
      private static <T> T[] safeCopyOf​(T first, T... array)  
      static <T> Predicate<T> xor​(Predicate<T> first, Predicate<? super T> second)
      Returns a predicate that evaluates to true if all or none of the component predicates evaluate to true.
      • Methods inherited from class java.lang.Object

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

      • IS_NULL

        public static final Predicate<java.lang.Object> IS_NULL
        a predicate that evaluates to true if the reference being tested is null.
      • NON_NULL

        public static final Predicate<java.lang.Object> NON_NULL
        a predicate that evaluates to true if the reference being tested is not null.
      • FALSE

        public static final Predicate<java.lang.Object> FALSE
        a predicate who's result is always false.
      • TRUE

        public static final Predicate<java.lang.Object> TRUE
        a predicate who's result is always true.
    • Constructor Detail

      • Predicates

        private Predicates()
        singleton utils
    • Method Detail

      • isNull

        public static <T> Predicate<T> isNull()
        Returns a predicate that evaluates to true if the reference being tested is null.
        Returns:
        a predicate that evaluates to true if the reference being tested is null
      • nonNull

        public static <T> Predicate<T> nonNull()
        Returns a predicate that evaluates to true if the reference being tested is non-null.
        Returns:
        a predicate that evaluates to true if the reference being tested is is non-null
      • alwaysFalse

        public static <T> Predicate<T> alwaysFalse()
        Returns a predicate who's result is always false.
        Returns:
        a predicate who's result is always false.
      • alwaysTrue

        public static <T> Predicate<T> alwaysTrue()
        Returns a predicate who's result is always true.
        Returns:
        a predicate who's result is always true.
      • instanceOf

        public static <T> Predicate<T> instanceOf​(java.lang.Class<?> clazz)
        Returns a predicate that evaluates to true if the object being tested is an instance of the provided class. If the object being tested is null this predicate evaluates to false.
        Parameters:
        clazz - The target class to be matched by the predicate.
        Returns:
        a predicate that evaluates to true if the object being tested is an instance of the provided class
      • isSame

        public static <T> Predicate<T> isSame​(T target)
        Returns a predicate that who's result is target == object.
        Type Parameters:
        T - the type of predicate values.
        Parameters:
        target - The target value to be compared for identity equality.
        Returns:
        a predicate that who's result is target == object
      • isEqual

        public static <T> Predicate<T> isEqual​(T target)
        Returns a predicate who's result matches Objects.equals(target, t).
        Type Parameters:
        T - the type of predicate values.
        Parameters:
        target - The target value to be compared for equality.
        Returns:
        a predicate who's result matches Objects.equals(target, t)
      • contains

        public static <T> Predicate<T> contains​(java.util.Collection<? extends T> target)
        Creates a predicate that evaluates to true if the tested object is a member of the provided collection. The collection is not defensively copied, so changes to it will alter the behavior of the predicate.
        Type Parameters:
        T - Type of predicate values.
        Parameters:
        target - the collection against which objects will be tested.
        Returns:
        a predicate that evaluates to true if the tested object is a member of the provided collection. The collection is not defensively copied, so changes to it will alter the behavior of the predicate.
      • containsKey

        public static <T> Predicate<T> containsKey​(java.util.Map<? extends T,​?> target)
        Creates a predicate that evaluates to true if the tested object is a key in the provided map. The map is not defensively copied, so changes to it will alter the behavior of the predicate.
        Type Parameters:
        T - Type of predicate values.
        Parameters:
        target - the map against which objects will be tested.
        Returns:
        a predicate that evaluates to true if the tested object is a key in the provided map. The map is not defensively copied, so changes to it will alter the behavior of the predicate.
      • negate

        public static <T> Predicate<T> negate​(Predicate<? super T> predicate)
        Returns a predicate that evaluates to true if the provided predicate evaluates to false
        Type Parameters:
        T - the type of values evaluated by the predicate.
        Parameters:
        predicate - The predicate to be evaluated.
        Returns:
        A predicate who's result is the logical inverse of the provided predicate.
      • and

        public static <T> Predicate<T> and​(Predicate<T> first,
                                           Predicate<? super T> second)
        Returns a predicate that evaluates to true if all of the component predicates evaluate to true. The components are evaluated in order, and evaluation will terminate upon the first false predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        first - initial component predicate to be evaluated.
        second - additional component predicate to be evaluated.
        Returns:
        A predicate who's result is true iff all component predicates are true.
      • and

        public static <T> Predicate<T> and​(java.lang.Iterable<Predicate<? super T>> components)
        Returns a predicate that evaluates to true if all of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first false predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated.
        Returns:
        A predicate who's result is true iff all component predicates are true.
      • and

        static <T> Predicate<T> and​(Predicate<? super T> first,
                                    java.lang.Iterable<Predicate<? super T>> components)
        Returns a predicate that evaluates to true if all of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first false predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated.
        Returns:
        A predicate who's result is true iff all component predicates are true.
      • and

        @SafeVarargs
        public static <T> Predicate<T> and​(Predicate<? super T>... components)
        Returns a predicate that evaluates to true if all of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first false predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated. A copy is made of the components.
        Returns:
        A predicate who's result is true iff all component predicates are true.
      • and

        @SafeVarargs
        static <T> Predicate<T> and​(Predicate<? super T> first,
                                    Predicate<? super T>... components)
        Returns a predicate that evaluates to true if all of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first false predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        first - first predicate to be evaluated.
        components - The predicates to be evaluated. A copy is made of the components.
        Returns:
        A predicate who's result is true iff all component predicates are true.
      • or

        public static <T> Predicate<T> or​(Predicate<T> first,
                                          Predicate<? super T> second)
        Returns a predicate that evaluates to true if any of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first true predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        first - initial component predicate to be evaluated.
        second - additional component predicate to be evaluated.
        Returns:
        A predicate who's result is true if any component predicate's result is true.
      • or

        public static <T> Predicate<T> or​(java.lang.Iterable<Predicate<? super T>> components)
        Returns a predicate that evaluates to true if any of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first true predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated. A copy is made of the components.
        Returns:
        A predicate who's result is true if any component predicate's result is true.
      • or

        static <T> Predicate<T> or​(Predicate<? super T> first,
                                   java.lang.Iterable<Predicate<? super T>> components)
        Returns a predicate that evaluates to true if any of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end upon the first true predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated. A copy is made of the components.
        Returns:
        A predicate who's result is true if any component predicate's result is true.
      • or

        @SafeVarargs
        public static <T> Predicate<T> or​(Predicate<? super T>... components)
        Returns a predicate that evaluates to true if any of the component predicates evaluate to true. The components are evaluated in order, and evaluation will terminate upon the first true predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated. A copy is made of the components.
        Returns:
        A predicate who's result is true if any component predicate's result is true.
      • or

        @SafeVarargs
        static <T> Predicate<T> or​(Predicate<T> first,
                                   Predicate<? super T>... components)
        Returns a predicate that evaluates to true if any of the component predicates evaluate to true. The components are evaluated in order, and evaluation will terminate upon the first true predicate.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        components - The predicates to be evaluated. A copy is made of the components.
        Returns:
        A predicate who's result is true if any component predicate's result is true.
      • xor

        public static <T> Predicate<T> xor​(Predicate<T> first,
                                           Predicate<? super T> second)
        Returns a predicate that evaluates to true if all or none of the component predicates evaluate to true. The components are evaluated in order, and evaluation will end if a predicate result fails to match the first predicate's result.
        Type Parameters:
        T - the type of values evaluated by the predicates.
        Parameters:
        first - initial component predicate to be evaluated.
        second - additional component predicate to be evaluated.
        Returns:
        a predicate that evaluates to true if all or none of the component predicates evaluate to true
      • safeCopyOf

        @SafeVarargs
        private static <T> T[] safeCopyOf​(T... array)
      • safeCopyOf

        @SafeVarargs
        private static <T> T[] safeCopyOf​(T first,
                                          T... array)
      • safeCopyOf

        private static <T> java.util.List<T> safeCopyOf​(T first,
                                                        java.lang.Iterable<T> iterable)
      • safeCopyOf

        private static <T> java.util.List<T> safeCopyOf​(java.lang.Iterable<T> iterable)