Class Predicates

java.lang.Object
eu.bandm.tools.util.java.Predicates

public abstract class Predicates extends Object
Library of constants and operations for working with predicates.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <A> Predicate<A>
    and(Predicate<? super A> p, Predicate<? super A> q)
    Returns the logical conjunction of the given predicates.
    anyOf(int... values)
    Returns a predicate that matches an argument if and only if contained in the given array.
    anyOf(long... values)
    Returns a predicate that matches an argument if and only if contained in the given array.
    static <A> Predicate<A>
    Returns a predicate which never matches.
    static <A> Function<A,Integer>
    bracket(Predicate<A> pred)
    Iverson bracket.
    static <A> Function<A,Boolean>
    Returns a function which delivers the test result of the given predicate.
    static <A> void
    classify(Predicate<? super A> pred, Collection<? extends A> in, Collection<? super A> positive, Collection<? super A> negative)
    Sorts all given objets into one of two collections, whether the predicate applies or not.
    static <A> Predicate<A>
    eq(A a)
    Returns a predicate which matches only the given object.
    static <A> Predicate<A>
    Returns a predicate which matches all objects which are equal to the given object.
    static <A extends Comparable<? super A>>
    Predicate<A>
    geq(A a)
    Returns a predicate which matches all objects greater or equal the given object.
    static <A extends Comparable<? super A>>
    Predicate<A>
    gt(A a)
    Returns a predicate which matches all objects greater the given object.
    static <A> Predicate<A>
    Returns a predicate which matches all instances of the given class.
    static <A> Predicate<A>
    keyOf(Map<? super A,?> map)
    Returns a predicate which matches the keys contained in the given map.
    static <A extends Comparable<? super A>>
    Predicate<A>
    leq(A a)
    Returns a predicate which matches all objects less or equal the given object.
    static <A extends Comparable<? super A>>
    Predicate<A>
    lt(A a)
    Returns a predicate which matches all objects less than the given object.
    static <A> Predicate<A>
    memberOf(Collection<? super A> c)
    Returns a predicate which matches the members of the given collection.
    static <A> Predicate<A>
    not(Predicate<A> p)
    Returns the logical inversion of the given predicate.
    static <A> Predicate<A>
    Returns a predicate which matches every non-null argument.
    static <A> Predicate<A>
    or(Predicate<? super A> p, Predicate<? super A> q)
    Returns the logical disjunction of the given predicates.
    static <A> Predicate<A>
    Returns a predicate which tests the given boolean function.
    static <A> Predicate<A>
    top()
    Returns a predicate which always matches.
    static <A, B> Predicate<A>
    totalize(Class<B> c, Predicate<? super B> pred)
    Returns a predicate which is tested only for instances of the given class.
    static <A> Predicate<A>
    xor(Predicate<? super A> p, Predicate<? super A> q)
    Returns the logical exclusive disjunction of the given predicates.

    Methods inherited from class java.lang.Object

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

    • top

      public static <A> Predicate<A> top()
      Returns a predicate which always matches.
      Type Parameters:
      A - the domain of the predicate.
      Returns:
      a predicate which always matches.
    • bottom

      public static <A> Predicate<A> bottom()
      Returns a predicate which never matches.
      Type Parameters:
      A - the domain of the predicate.
      Returns:
      a predicate which never matches.
    • notNull

      public static <A> Predicate<A> notNull()
      Returns a predicate which matches every non-null argument.
      Type Parameters:
      A - the domain of the predicate. (Should have the form @Opt B for this method to be sensible.)
      Returns:
      a predicate which matches every non-null argument.
    • not

      public static <A> Predicate<A> not(Predicate<A> p)
      Returns the logical inversion of the given predicate. The result will match ever object the given predicate does not match, et vice versa.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      p - the predicate to negate.
      Returns:
      the logical inversion of the given predicate.
    • and

      public static <A> Predicate<A> and(Predicate<? super A> p, Predicate<? super A> q)
      Returns the logical conjunction of the given predicates. The result will match every object both given predicate match.
      Type Parameters:
      A - the domain of the predicates.
      Parameters:
      p - the first predicate.
      q - the second predicate.
      Returns:
      the logical conjunction of the given predicates.
    • or

      public static <A> Predicate<A> or(Predicate<? super A> p, Predicate<? super A> q)
      Returns the logical disjunction of the given predicates. The result will match every object at least one of the given predicates match. The test function of both given predicates may not be called.
      Type Parameters:
      A - the domain of the predicates.
      Parameters:
      p - the first predicate.
      q - the second predicate.
      Returns:
      the logical disjunction of the given predicates.
    • xor

      public static <A> Predicate<A> xor(Predicate<? super A> p, Predicate<? super A> q)
      Returns the logical exclusive disjunction of the given predicates. The result will match every object exactly one of the given predicates match. The test function of both given predicates are called.
      Type Parameters:
      A - the domain of the predicates.
      Parameters:
      p - the first predicate.
      q - the second predicate.
      Returns:
      the logical exclusive disjunction of the given predicates.
    • eq

      public static <A> Predicate<A> eq(A a)
      Returns a predicate which matches only the given object. The identity is tested by the == operator.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      a - the object to test for.
      Returns:
      a predicate which matches only the given object.
    • leq

      public static <A extends Comparable<? super A>> Predicate<A> leq(A a)
      Returns a predicate which matches all objects less or equal the given object.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      a - the object to compare with.
      Returns:
      a predicate which matches all objects less or equal the given object.
    • lt

      public static <A extends Comparable<? super A>> Predicate<A> lt(A a)
      Returns a predicate which matches all objects less than the given object.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      a - the object to compare with.
      Returns:
      a predicate which matches all objects less or equal the given object.
    • geq

      public static <A extends Comparable<? super A>> Predicate<A> geq(A a)
      Returns a predicate which matches all objects greater or equal the given object.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      a - the object to compare with.
      Returns:
      a predicate which matches all objects greater or equal the given object.
    • gt

      public static <A extends Comparable<? super A>> Predicate<A> gt(A a)
      Returns a predicate which matches all objects greater the given object.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      a - the object to compare with.
      Returns:
      a predicate which matches all objects greater the given object.
    • equal

      public static <A> Predicate<A> equal(Object a)
      Returns a predicate which matches all objects which are equal to the given object. The identity is tested by applying the Object.equals(java.lang.Object) method.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      a - the object to test for.
      Returns:
      a predicate which matches all objects which are equal to the given object.
    • instanceOf

      public static <A> Predicate<A> instanceOf(Class<?> c)
      Returns a predicate which matches all instances of the given class.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      c - the class for which to test.
      Returns:
      a predicate which matches all instances of the given class.
    • test

      public static <A> Predicate<A> test(Function<A,Boolean> fun)
      Returns a predicate which tests the given boolean function.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      fun - the function to apply for getting the test result.
      Returns:
      a predicate which tests the given boolean function.
    • characteristic

      public static <A> Function<A,Boolean> characteristic(Predicate<A> pred)
      Returns a function which delivers the test result of the given predicate.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      pred - the predicate for which the characteristic function is returned.
      Returns:
      a function which delivers the test result of the given predicate.
    • bracket

      public static <A> Function<A,Integer> bracket(Predicate<A> pred)
      Iverson bracket. Returns 1 if the predicate matches, else 0.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      pred - the predicate for which the Iverson bracket is returned.
      Returns:
      1 if the predicate matches, else 0.
    • memberOf

      public static <A> Predicate<A> memberOf(Collection<? super A> c)
      Returns a predicate which matches the members of the given collection.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      c - the collection to test for.
      Returns:
      a predicate which matches the members of the given collection.
    • keyOf

      public static <A> Predicate<A> keyOf(Map<? super A,?> map)
      Returns a predicate which matches the keys contained in the given map.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      map - the map to test for.
      Returns:
      a predicate which matches the keys contained in the given map.
    • totalize

      public static <A, B> Predicate<A> totalize(Class<B> c, Predicate<? super B> pred)
      Returns a predicate which is tested only for instances of the given class. All objects outside this class do not match.
      Type Parameters:
      A - the domain of the predicate.
      B - the class to test for.
      Parameters:
      c - the class to test for.
      pred - the predicate to test for.
      Returns:
      a predicate which is tested only for instances of the given class.
    • classify

      public static <A> void classify(Predicate<? super A> pred, Collection<? extends A> in, Collection<? super A> positive, Collection<? super A> negative)
      Sorts all given objets into one of two collections, whether the predicate applies or not.
      Type Parameters:
      A - the domain of the predicate.
      Parameters:
      pred - the classifying predicate.
      in - the objects to sort.
      positive - result parameter of all objects the predicate matches.
      negative - result parameter of all objects the predicate does not match.
    • anyOf

      public static IntPredicate anyOf(int... values)
      Returns a predicate that matches an argument if and only if contained in the given array.

      The resulting predicate is equivalent to the lambda expression x -> Arrays.indexOf(x) >= 0, but may execute with significantly better performance.

      Parameters:
      values - an array of values
      Returns:
      a predicate that matches its argument if and only if an equal value is containes in values
      Throws:
      NullPointerException - if values is null
    • anyOf

      public static LongPredicate anyOf(long... values)
      Returns a predicate that matches an argument if and only if contained in the given array.

      The resulting predicate is equivalent to the lambda expression x -> Arrays.indexOf(x) >= 0, but may execute with significantly better performance.

      Parameters:
      values - an array of values
      Returns:
      a predicate that matches its argument if and only if an equal value is containes in values
      Throws:
      NullPointerException - if values is null