Class When


  • public class When
    extends Object
    Class with static methods implementing some methods functioning like conditional statements.
    Author:
    Herbert Praehofer
    • Constructor Detail

      • When

        public When()
    • Method Detail

      • when

        @SafeVarargs
        public static <T,​R> R when​(T value,
                                         CondFunction<? super T,​? extends R>... cases)
        Methods applying conditional functions on a given value. The first conditional function which is applicable is applied and the result is returned. Throws a NoSuchElementException if none of the conditional functions is applicable.
        Type Parameters:
        T - the type of the argument
        R - the type of the return value
        Parameters:
        value - the value to apply
        cases - the conditional functions to test and apply
        Returns:
        the result value of the conditional application
      • is

        public static <T,​R> CondFunction<T,​R> is​(Predicate<? super T> pred,
                                                             Function<? super T,​? extends R> func)
        Creates a conditional function from a predicate and a function.
        Type Parameters:
        T - the type of the argument
        R - the type of the return value of the function
        Parameters:
        pred - the predicate
        func - the function
        Returns:
        the constructed conditional function
      • otherwise

        public static <T,​R> CondFunction<T,​R> otherwise​(Function<? super T,​? extends R> func)
        Creates a conditional function from a function and with a test predicate always returning true
        Type Parameters:
        T - the type of the argument
        R - the type of the return value of the function
        Parameters:
        func - the function
        Returns:
        the constructed conditional function
      • when

        @SafeVarargs
        public static <R> R when​(Object o,
                                 TypeTestFunction<?,​? extends R>... ttFns)
        Methods applying typetest functions on a given value. The first typetest function which is applicable is applied and the result is returned. Throws a NoSuchElementException if none of the conditional functions is applicable.
        Type Parameters:
        R - the type of the return value
        Parameters:
        o - the value to apply
        ttFns - the typetest functions
        Returns:
        the result value of the application of the first applicable typtest function
      • is

        public static <T,​R> TypeTestFunction<T,​R> is​(Class<T> clazz,
                                                                 Function<? super T,​? extends R> func)
        Creates a typetest function from a class and a function.
        Type Parameters:
        T - the type of the argument
        R - the type of the return value of the function
        Parameters:
        clazz - the class for the typetest
        func - the function
        Returns:
        the constructed typetest function