Interface CondFunction<T,​R>

  • Type Parameters:
    T - the type of the argument
    R - the type of the wrapped return value
    All Superinterfaces:
    Function<T,​Optional<R>>
    All Known Implementing Classes:
    TypeTestFunction

    public interface CondFunction<T,​R>
    extends Function<T,​Optional<R>>
    Interface for conditional functions. Conditional functions test argument values before returning the result. A conditional function returns an Optional. If the argument passes the test, the value is mapped to a result and the result is wrapped into the Optional. But if the test is not successful, Optional.empty() is returned.
    Author:
    Herbert Praehofer
    • Method Detail

      • test

        boolean test​(T t)
        Tests an argument value if applicable.
        Parameters:
        t - the value to test
        Returns:
        the Boolean result of the test
      • of

        static <T,​R> CondFunction<T,​R> of​(Predicate<? super T> pred,
                                                      Function<? super T,​? extends R> func)
        Constructs a conditional function from a predicate and a function argument. The resulting conditional function first uses the predicate to test the argument value. If successful, the function is applied and the resulting value is wrapped into an Optional. Otherwise Optional.empty() is returned.
        Type Parameters:
        T - the type of the argument
        R - the type of the wrapped return value
        Parameters:
        pred - the predicate
        func - the function
        Returns:
        the conditional function
      • orElseIf

        default CondFunction<T,​R> orElseIf​(Predicate<? super T> pred,
                                                 Function<? super T,​? extends R> func)
        Returns a combined conditional function. The returned conditional function first tries to apply this conditional function, and if not successful will try to test the value with the given predicate and apply the given function.
        Parameters:
        pred - the predicate to test the argument
        func - the function to apply on the argument
        Returns:
        the combined conditional function
      • orElse

        default CondFunction<T,​R> orElse​(Function<? super T,​? extends R> func)
        Returns a combined conditional function. The returned conditional function first tries to apply this conditional function, and if not successful will alternatively apply the given function.
        Parameters:
        func - the function to apply on the argument as an alternative
        Returns:
        the combined conditional function