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 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.