Interface ThrowableFunction<T,​R>

  • Type Parameters:
    T - the source type
    R - the return type
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface ThrowableFunction<T,​R>
    Function with Throwable
    Since:
    1.0.0
    See Also:
    Function, Throwable
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default <V> ThrowableFunction<T,​V> andThen​(ThrowableFunction<? super R,​? extends V> after)
      Returns a composed throwable-function that first applies this throwable-function to its input, and then applies the after throwable-function to the result.
      R apply​(T t)
      Applies this function to the given argument.
      default <V> ThrowableFunction<V,​R> compose​(ThrowableFunction<? super V,​? extends T> before)
      Returns a composed throwable-function that first applies the before throwable-function to its input, and then applies this throwable-function to the result.
      default R execute​(T t)
      Executes #apply(T) with the default exception handling
      static <T,​R>
      R
      execute​(T t, ThrowableFunction<T,​R> function)
      Executes ThrowableFunction with the default exception handling
      static <T,​R>
      R
      execute​(T t, ThrowableFunction<T,​R> function, java.util.function.BiFunction<T,​java.lang.Throwable,​R> exceptionHandler)
      Executes ThrowableFunction with the customized exception handling
      default R execute​(T t, java.util.function.BiFunction<T,​java.lang.Throwable,​R> exceptionHandler)
      Executes #apply(T) with the customized exception handling
      default R handleException​(T t, java.lang.Throwable failure)
      Handle any exception that the #apply(T) method throws
    • Method Detail

      • apply

        R apply​(T t)
         throws java.lang.Throwable
        Applies this function to the given argument.
        Parameters:
        t - the function argument
        Returns:
        the function result
        Throws:
        java.lang.Throwable - if met with any error
      • execute

        default R execute​(T t)
                   throws java.lang.RuntimeException
        Executes #apply(T) with the default exception handling
        Parameters:
        t - the function argument
        Returns:
        the function result
        Throws:
        java.lang.RuntimeException
      • execute

        default R execute​(T t,
                          java.util.function.BiFunction<T,​java.lang.Throwable,​R> exceptionHandler)
                   throws java.lang.RuntimeException
        Executes #apply(T) with the customized exception handling
        Parameters:
        t - the function argument
        exceptionHandler - the handler to handle the function argument and the exception that the #apply(T) method throws
        Returns:
        the function result
        Throws:
        java.lang.RuntimeException
      • handleException

        default R handleException​(T t,
                                  java.lang.Throwable failure)
        Handle any exception that the #apply(T) method throws
        Parameters:
        t - the value to be consumed
        failure - the instance of Throwable
      • compose

        default <V> ThrowableFunction<V,​R> compose​(ThrowableFunction<? super V,​? extends T> before)
        Returns a composed throwable-function that first applies the before throwable-function to its input, and then applies this throwable-function to the result. If evaluation of either throwable-function throws an exception, it is relayed to the caller of the composed throwable-function.
        Type Parameters:
        V - the type of input to the before throwable-function, and to the composed throwable-function
        Parameters:
        before - the throwable-function to apply before this throwable-function is applied
        Returns:
        a composed throwable-function that first applies the before throwable-function and then applies this throwable-function
        Throws:
        java.lang.NullPointerException - if before is null
        See Also:
        andThen(ThrowableFunction)
      • andThen

        default <V> ThrowableFunction<T,​V> andThen​(ThrowableFunction<? super R,​? extends V> after)
        Returns a composed throwable-function that first applies this throwable-function to its input, and then applies the after throwable-function to the result. If evaluation of either throwable-function throws an exception, it is relayed to the caller of the composed throwable-function.
        Type Parameters:
        V - the type of output of the after throwable-function, and of the composed throwable-function
        Parameters:
        after - the throwable-function to apply after this throwable-function is applied
        Returns:
        a composed throwable-function that first applies this throwable-function and then applies the after throwable-function
        Throws:
        java.lang.NullPointerException - if after is null
        See Also:
        compose(ThrowableFunction)
      • execute

        static <T,​R> R execute​(T t,
                                     ThrowableFunction<T,​R> function)
                              throws java.lang.NullPointerException
        Executes ThrowableFunction with the default exception handling
        Type Parameters:
        T - the source type
        R - the return type
        Parameters:
        t - the throwable-function argument
        function - ThrowableFunction
        Returns:
        the result after execution
        Throws:
        java.lang.NullPointerException - if function is null
      • execute

        static <T,​R> R execute​(T t,
                                     ThrowableFunction<T,​R> function,
                                     java.util.function.BiFunction<T,​java.lang.Throwable,​R> exceptionHandler)
                              throws java.lang.NullPointerException
        Executes ThrowableFunction with the customized exception handling
        Type Parameters:
        T - the source type
        R - the return type
        Parameters:
        t - the function argument
        exceptionHandler - the handler to handle the function argument and the exception that the #apply(T) method throws
        function - ThrowableFunction
        Returns:
        the result after execution
        Throws:
        java.lang.NullPointerException - if function and exceptionHandler is null