Class Compose

java.lang.Object
org.klojang.check.relation.Compose

public final class Compose extends Object
Utility methods that assist in the creation of new checks by combining multiple individual checks. Note that while the predicates in the CommonChecks class are, in fact, already either a ComposablePredicate or a ComposableIntPredicate, the relational checks obviously are not. Handwritten lambdas and method references (for example: i -> i % 3 == 0) neither are a ComposablePredicate or ComposableIntPredicate in and of themselves. The utility methods defined in this class make sure a composition can start with a Relation, lambda or method reference.
Author:
Ayco Holleman
  • Method Details

    • valid

      public static <T> ComposablePredicate<T> valid()
      Returns a ComposablePredicate that always evaluates to true. Can be used as the first of a series of AND-joined checks if there is no need for an initial notNull() null check.
      
       Check.that(color).is(valid().and(equalTo(), noneOf(), GREEN, BLUE, YELLOW));
       
      Type Parameters:
      T - the type of the value being tested (which is ignored by the returned ComposablePredicate)
      Returns:
      a ComposablePredicate that always evaluates to true
    • validInt

      public static ComposableIntPredicate validInt()
      Returns a ComposableIntPredicate that always evaluates to true. Can be used as the first of a series of AND-joined checks.
      Returns:
      a ComposableIntPredicate that always evaluates to true
    • invalid

      public static <T> ComposablePredicate<T> invalid()
      Returns a ComposablePredicate that always evaluates to false. Can be used as the first of a series of OR-joined checks if there is no need for an initial notNull() null check.
      
       Check.that(color).is(invalid().or(equalTo(), anyOf(), GREEN, BLUE, YELLOW));
       
      Type Parameters:
      T - the type of the value being tested (which is ignored by the returned ComposablePredicate)
      Returns:
      a ComposablePredicate that always evaluates to false
    • invalidInt

      public static ComposableIntPredicate invalidInt()
      Returns a ComposableIntPredicate that always evaluates to false. Can be used as the first of a series of OR-joined checks.
      Returns:
      a ComposableIntPredicate that always evaluates to false
    • validIf

      public static <T> ComposablePredicate<T> validIf(T value)
      Returns a ComposablePredicate that evaluates to true if the value to be tested has the specified value. The two values are compared using Objects.equals().
      Type Parameters:
      T - the type of the value being tested
      Parameters:
      value - the value to compare the value to be tested with
      Returns:
      a ComposablePredicate that evaluates to true if the value to be tested has the specified value
    • validIntIf

      public static ComposableIntPredicate validIntIf(int value)
      Returns a ComposablePredicate that evaluates to true if the value to be tested has the specified value.
      Parameters:
      value - the value to compare the value to be tested with
      Returns:
      a ComposablePredicate that evaluates to true if the value to be tested has the specified value
    • invalidIf

      public static <T> ComposablePredicate<T> invalidIf(T value)
      Returns a ComposablePredicate that evaluates to true if the value to be tested has the specified value. The two values are compared using Objects.equals().
      Type Parameters:
      T - the type of the value being tested
      Parameters:
      value - the value to compare the value to be tested with
      Returns:
      a ComposablePredicate that evaluates to true if the value to be tested has the specified value
    • invalidIntIf

      public static ComposableIntPredicate invalidIntIf(int value)
      Returns a ComposablePredicate that evaluates to true if the value to be tested has the specified value.
      Parameters:
      value - the value to compare the value to be tested with
      Returns:
      a ComposablePredicate that evaluates to true if the value to be tested has the specified value
    • validWhen

      public static <T> ComposablePredicate<T> validWhen(Predicate<T> test)
      Converts a Predicate to a ComposablePredicate. This method can be used to convert a predefined Predicate constant from outside Klojang Check to a ComposablePredicate, or to hard-cast a lambda or method reference to a ComposablePredicate. This method is only needed if the Predicate, lambda or method reference must be the first test of the composition.
      
       Check.that(sentence).is(validIf((String s) -> s.contains("to"))
          .orElse((String s) -> s.contains("be"));
          .orElse((String s) -> s.contains("or"));
          .orElse((String s) -> s.contains("not")));
       
      Type Parameters:
      T - the type of the value being tested
      Parameters:
      test - the Predicate to convert
      Returns:
      the equivalent ComposablePredicate
    • validIntWhen

      public static ComposableIntPredicate validIntWhen(IntPredicate test)
      Converts an IntPredicate to a ComposableIntPredicate. This method can be used to convert a predefined IntPredicate constant from outside Klojang Check to a ComposableIntPredicate, or to hard-cast a lambda or method reference. This method is only needed if the IntPredicate, lambda or method reference must be the first test of the composition.
      Parameters:
      test - the IntPredicate to convert
      Returns:
      the equivalent ComposableIntPredicate
    • validWhen

      public static <S, O> ComposablePredicate<S> validWhen(Relation<S,O> relation, O object)
      Converts a Relation to a ComposablePredicate. More precisely: this method returns a ComposablePredicate that evaluates to true if the value being tested has the specified relation to the specified value. This method is only needed if the Relation must be the first test of the composition.
      
       Check.that(Year.now()).is(validIf(GT(), Year.of(2000))
          .andAlso(LT(), Year.of(3000));
       
      Type Parameters:
      S - the type of the subject of the relation
      O - the type of the object of the relation
      Parameters:
      relation - the relationship test to execute
      object - the object of the relation
      Returns:
      a ComposablePredicate that evaluates to true if the value being tested has the specified relation to the specified value
    • validIntWhen

      public static <O> ComposableIntPredicate validIntWhen(IntObjRelation<O> relation, O object)
      Converts an IntObjRelation to a ComposableIntPredicate. More precisely: this method returns a ComposableIntPredicate that evaluates to true if the value being tested has the specified relation to the specified value. This method is only needed if the IntObjRelation must be the first test of the composition.
      Type Parameters:
      O - the type of the object of the relation
      Parameters:
      relation - the relationship test to execute
      object - the object of the relation
      Returns:
      a ComposableIntPredicate that evaluates to true if the value being tested has the specified relation to the specified value
    • validIntWhen

      public static ComposableIntPredicate validIntWhen(IntRelation relation, int object)
      Converts an Relation to a ComposableIntPredicate. More precisely: this method returns a ComposableIntPredicate that evaluates to true if the value being tested has the specified relation to the specified value. This method is only needed if the IntRelation must be the first test of the composition.
      Parameters:
      relation - the relationship test to execute
      object - the object of the relation
      Returns:
      a ComposableIntPredicate that evaluates to true if the value being tested has the specified relation to the specified value