Class Predicates

java.lang.Object
com.strobel.core.Predicates

public final class Predicates extends 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 final Predicate<Object>
    a predicate who's result is always false.
    static final Predicate<Object>
    a predicate that evaluates to true if the reference being tested is null.
    static final Predicate<Object>
    a predicate that evaluates to true if the reference being tested is not null.
    static final Predicate<Object>
    a predicate who's result is always true.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    singleton utils
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Predicate<T>
    Returns a predicate who's result is always false.
    static <T> Predicate<T>
    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, 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(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(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(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(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>
    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>
    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, 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(Iterable<Predicate<? super T>> components)
    Returns a predicate that evaluates to true if any of the component predicates evaluate to true.
    private static <T> List<T>
    safeCopyOf(Iterable<T> iterable)
     
    private static <T> T[]
    safeCopyOf(T... array)
     
    private static <T> List<T>
    safeCopyOf(T first, 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 Details

    • IS_NULL

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

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

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

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

    • Predicates

      private Predicates()
      singleton utils
  • Method Details

    • 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(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(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(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(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, 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(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, 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> List<T> safeCopyOf(T first, Iterable<T> iterable)
    • safeCopyOf

      private static <T> List<T> safeCopyOf(Iterable<T> iterable)