Class If<I>

  • Type Parameters:
    I - the type of input to be validated
    All Implemented Interfaces:
    Predicate<I>

    public class If<I>
    extends KoryphePredicate<I>
    An If is a Predicate that conditionally applies one of two predicates to a provided input.

    Note that the If has both a number of constructors as well as a Builder. The use case for constructors would generally be for testing a single input.

    The use case for the Builder allows greater flexibility, mainly for allowing multiple inputs such as an Array of objects, and control over which of these objects is tested by each predicate.

    For example, Given an input array of 3 objects, one may wish to test the first object in the array against the initial predicate, then pass both the second and third objects to the resulting predicate, based on the outcome of the initial test. This would require use of the Builder, passing a selection of 0 along with the first predicate, and a selection of 1, 2 with the other predicates.

    This would look something like:
         final If ifPredicate = new If.SelectedBuilder()
              .predicate(firstPredicate, 0)
              .then(thenPredicate, 1, 2)
              .otherwise(otherwisePredicate, 1, 2)
              .build();
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  If.SelectedBuilder  
    • Constructor Summary

      Constructors 
      Constructor Description
      If()  
      If​(boolean condition, Predicate<? super I> then)
      Constructs a new If object, with a boolean condition, and one predicate that will test the input, if the boolean condition resolves to true.
      If​(boolean condition, Predicate<? super I> then, Predicate<? super I> otherwise)
      Constructs a new If object, with a boolean condition, one predicate to test the input if the condition is true, and another predicate to test the input if the condition is false.
      If​(Predicate<? super I> predicate, Predicate<? super I> then)
      Constructs a new If object, with an initial predicate, and another predicate to test the input if the initial predicate resolves to true.
      If​(Predicate<? super I> predicate, Predicate<? super I> then, Predicate<? super I> otherwise)
      Constructs a new If object, with an initial predicate, a second predicate to test the input if the initial predicate resolves to true, and a third predicate to test the input if the initial predicate resolves to false.
    • Constructor Detail

      • If

        public If()
      • If

        public If​(boolean condition,
                  Predicate<? super I> then)
        Constructs a new If object, with a boolean condition, and one predicate that will test the input, if the boolean condition resolves to true.
        Parameters:
        condition - a boolean condition
        then - the predicate to apply if true
      • If

        public If​(boolean condition,
                  Predicate<? super I> then,
                  Predicate<? super I> otherwise)
        Constructs a new If object, with a boolean condition, one predicate to test the input if the condition is true, and another predicate to test the input if the condition is false.
        Parameters:
        condition - a boolean condition
        then - the predicate to apply if true
        otherwise - the predicate to apply if false
      • If

        public If​(Predicate<? super I> predicate,
                  Predicate<? super I> then)
        Constructs a new If object, with an initial predicate, and another predicate to test the input if the initial predicate resolves to true.
        Parameters:
        predicate - the initial predicate applied to the input
        then - the predicate to apply if true
      • If

        public If​(Predicate<? super I> predicate,
                  Predicate<? super I> then,
                  Predicate<? super I> otherwise)
        Constructs a new If object, with an initial predicate, a second predicate to test the input if the initial predicate resolves to true, and a third predicate to test the input if the initial predicate resolves to false.
        Parameters:
        predicate - the initial predicate applied to the input
        then - the predicate to apply if true
        otherwise - the predicate to apply if false
    • Method Detail

      • test

        public boolean test​(I input)
        If the condition is not being used or has not been set, then the provided predicate will test the input (assuming it is also not null). If this resolves to true, the then predicate will test the input, else the otherwise predicate will be used. The result of either of these being applied to the input is finally returned.
        Parameters:
        input - the input to be tested
        Returns:
        true if the input passes the predicate, otherwise false
      • getCondition

        public Boolean getCondition()
      • setCondition

        public void setCondition​(boolean condition)
      • setThen

        public void setThen​(Predicate<? super I> then)
      • getOtherwise

        public Predicate<? super I> getOtherwise()
      • setOtherwise

        public void setOtherwise​(Predicate<? super I> otherwise)
      • getPredicate

        public Predicate<? super I> getPredicate()
      • setPredicate

        public void setPredicate​(Predicate<? super I> predicate)