Class If<I,​O>

  • Type Parameters:
    I - the type of input to be validated
    All Implemented Interfaces:
    Function<I,​O>

    public class If<I,​O>
    extends KorypheFunction<I,​O>
    An If is a Function that conditionally applies one of two functions to a provided input.

    Note that the If has both a number of constructors as well as a SelectedBuilder. 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 function.

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

    This would look something like:
         final If this = new If()
              .predicate(0, firstPredicate)
              .then(1, thenFunction, 2)
              .otherwise([1,2], otherwiseFunction, [2]);
     
    • Constructor Detail

      • If

        public If()
    • Method Detail

      • apply

        public O apply​(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 function will be applied the input, else the otherwise function will be applied. The result of either of these being applied to the input is finally returned.
        Parameters:
        input - the input to be tested
        Returns:
        the result from the conditional functions
      • getCondition

        public Boolean getCondition()
      • setCondition

        public void setCondition​(boolean condition)
      • condition

        public If<I,​O> condition​(boolean condition)
      • getPredicate

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

        public void setPredicate​(Predicate<?> predicate)
      • predicate

        public If<I,​O> predicate​(Predicate<?> predicate)
      • getThen

        public Function<? super I,​O> getThen()
      • setThen

        public void setThen​(Function<?,​?> then)
      • getOtherwise

        public Function<? super I,​O> getOtherwise()
      • setOtherwise

        public void setOtherwise​(Function<?,​?> otherwise)
      • otherwise

        public If<I,​O> otherwise​(Function<?,​?> otherwise)
      • otherwise

        public If<I,​O> otherwise​(Integer selectionProjection,
                                       Function<?,​?> then)
      • otherwise

        public If<I,​O> otherwise​(Integer[] selectionProjection,
                                       Function<?,​?> then)