Interface ThrowingFunction<T,R,X extends Throwable>
-
- Type Parameters:
T- The type of the input to the function.R- The type of the result of the function.X- The type of checked exception that can be thrown.
- All Known Subinterfaces:
ThrowingUnaryOperator<T,X>
- 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 ThrowingFunction<T,R,X extends Throwable>
Represents a function that accepts one argument and produces a result. This is a checked-exception throwing equivalent ofFunction.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <V> ThrowingFunction<T,V,X>andThen(ThrowingFunction<? super R,? extends V,? extends X> after)Returns a composed function that first applies this function to its input, and then applies theafterfunction to the result.Rapply(T t)Applies this function to the given argument.static <T,R,X extends Throwable>
ThrowingFunction<T,R,X>checked(Function<? super T,? extends R> function)Returns a function that applies thefunctionfunction to its input.static <T,R,X extends Throwable>
ThrowingFunction<T,R,X>checked(Function<? super T,? extends R> function, Class<X> errorType)Returns a function that applies thefunctionfunction to its input.default <V> ThrowingFunction<V,R,X>compose(ThrowingFunction<? super V,? extends T,? extends X> before)Returns a composed function that first applies thebeforefunction to its input, and then applies this function to the result.static <T,X extends Throwable>
ThrowingFunction<T,T,X>identity()Returns a function that always returns its input argument.static <T,R,X extends Throwable>
ThrowingFunction<T,R,X>of(ThrowingFunction<T,R,X> function)Factory method for turningThrowingFunction-shaped lambdas intoThrowingFunctions.default <E extends Throwable>
ThrowingFunction<T,R,E>onErrorApplyChecked(ThrowingFunction<? super T,? extends R,? extends E> fallback)Returns a function that applies this function to its input.default Function<T,R>onErrorApplyUnchecked(Function<? super T,? extends R> fallback)Returns a function that applies this function to its input.default <E extends Throwable>
ThrowingFunction<T,R,E>onErrorGetChecked(ThrowingSupplier<? extends R,? extends E> fallback)Returns a function that applies this function to its input.default Function<T,R>onErrorGetUnchecked(Supplier<? extends R> fallback)Returns a function that applies this function to its input.default <E extends Throwable>
ThrowingFunction<T,R,E>onErrorHandleChecked(ThrowingFunction<? super X,? extends R,? extends E> errorHandler)Returns a function that applies this function to its input.default Function<T,R>onErrorHandleUnchecked(Function<? super X,? extends R> errorHandler)Returns a function that applies this function to its input.default Function<T,R>onErrorReturn(R fallback)Returns a function that applies this function to its input.default <E extends Throwable>
ThrowingFunction<T,R,E>onErrorThrowAsChecked(Function<? super X,? extends E> errorMapper)Returns a function that applies this function to its input.default <E extends RuntimeException>
Function<T,R>onErrorThrowAsUnchecked(Function<? super X,? extends E> errorMapper)Returns a function that applies this function to its input.default Function<T,R>unchecked()Returns a function that applies this function to its input.static <T,R>
Function<T,R>unchecked(ThrowingFunction<? super T,? extends R,?> function)Returns a function that applies thefunctionfunction to its input.
-
-
-
Method Detail
-
compose
default <V> ThrowingFunction<V,R,X> compose(ThrowingFunction<? super V,? extends T,? extends X> before)
Returns a composed function that first applies thebeforefunction to its input, and then applies this function to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.- Type Parameters:
V- The type of input to thebeforefunction, and to the composed function.- Parameters:
before- The function to apply before this function is applied.- Returns:
- A composed function that first applies the
beforefunction and then applies this function. - Throws:
NullPointerException- Ifbeforeisnull.- See Also:
andThen(ThrowingFunction)
-
andThen
default <V> ThrowingFunction<T,V,X> andThen(ThrowingFunction<? super R,? extends V,? extends X> after)
Returns a composed function that first applies this function to its input, and then applies theafterfunction to the result. If evaluation of either function throws an exception, it is relayed to the caller of the composed function.- Type Parameters:
V- The type of output of theafterfunction, and of the composed function.- Parameters:
after- The function to apply after this function is applied.- Returns:
- A composed function that first applies this function and then applies the
afterfunction. - Throws:
NullPointerException- Ifafterisnull.- See Also:
compose(ThrowingFunction)
-
onErrorThrowAsChecked
default <E extends Throwable> ThrowingFunction<T,R,E> onErrorThrowAsChecked(Function<? super X,? extends E> errorMapper)
Returns a function that applies this function to its input. Any checked exception thrown by this function is transformed using the given error mapper, and the returned function throws the transformation result.- Type Parameters:
E- The type of checked exception to transform to.- Parameters:
errorMapper- The function to use to transform any checked exception thrown by this function.- Returns:
- A function that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorMapperisnull.
-
onErrorThrowAsUnchecked
default <E extends RuntimeException> Function<T,R> onErrorThrowAsUnchecked(Function<? super X,? extends E> errorMapper)
Returns a function that applies this function to its input. Any checked exception thrown by this function is transformed using the given error mapper, and the returned function throws the transformation result.- Type Parameters:
E- The type of unchecked exception to transform to.- Parameters:
errorMapper- The function to use to transform any checked exception thrown by this function.- Returns:
- A function that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorMapperisnull.
-
onErrorHandleChecked
default <E extends Throwable> ThrowingFunction<T,R,E> onErrorHandleChecked(ThrowingFunction<? super X,? extends R,? extends E> errorHandler)
Returns a function that applies this function to its input. Any checked exception thrown by this function is transformed using the given error handler, and the returned function returns the transformation result.- Type Parameters:
E- The type of checked exception that can be thrown by the given error handler.- Parameters:
errorHandler- The function to use to transform any checked exception thrown by this function.- Returns:
- A function that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorHandlerisnull.
-
onErrorHandleUnchecked
default Function<T,R> onErrorHandleUnchecked(Function<? super X,? extends R> errorHandler)
Returns a function that applies this function to its input. Any checked exception thrown by this function is transformed using the given error handler, and the returned function returns the transformation result.- Parameters:
errorHandler- The function to use to transform any checked exception thrown by this function.- Returns:
- A function that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorHandlerisnull.
-
onErrorApplyChecked
default <E extends Throwable> ThrowingFunction<T,R,E> onErrorApplyChecked(ThrowingFunction<? super T,? extends R,? extends E> fallback)
Returns a function that applies this function to its input. If this function throws any checked exception, it is discarded and the given fallback function is invoked.- Type Parameters:
E- The type of checked exception that can be thrown by the given fallback function.- Parameters:
fallback- The function to invoke if this function throws any checked exception.- Returns:
- A function that invokes the
fallbackfunction if this function throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorApplyUnchecked
default Function<T,R> onErrorApplyUnchecked(Function<? super T,? extends R> fallback)
Returns a function that applies this function to its input. If this function throws any checked exception, it is discarded and the given fallback function is invoked.- Parameters:
fallback- The function to invoke if this function throws any checked exception.- Returns:
- A function that invokes the
fallbackfunction if this function throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorGetChecked
default <E extends Throwable> ThrowingFunction<T,R,E> onErrorGetChecked(ThrowingSupplier<? extends R,? extends E> fallback)
Returns a function that applies this function to its input. If this function throws any checked exception, it is discarded and the given fallback supplier is invoked.- Type Parameters:
E- The type of checked exception that can be thrown by the given fallback supplier.- Parameters:
fallback- The supplier to produce the value to return if this function throws any checked exception.- Returns:
- A function that invokes the
fallbacksupplier if this function throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorGetUnchecked
default Function<T,R> onErrorGetUnchecked(Supplier<? extends R> fallback)
Returns a function that applies this function to its input. If this function throws any checked exception, it is discarded and the given fallback supplier is invoked.- Parameters:
fallback- The supplier to produce the value to return if this function throws any checked exception.- Returns:
- A function that invokes the
fallbacksupplier if this function throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorReturn
default Function<T,R> onErrorReturn(R fallback)
Returns a function that applies this function to its input. If this function throws any checked exception, it is discarded and the given fallback value is returned.- Parameters:
fallback- The value to return if this function throws any checked exception.- Returns:
- A function that returns the
fallbackvalue if this function throws any checked exception.
-
unchecked
default Function<T,R> unchecked()
Returns a function that applies this function to its input. Any checked exception thrown by this function is wrapped in anUncheckedExceptionwithout a stack trace.- Returns:
- A function that wraps any checked exception in an
UncheckedException.
-
of
static <T,R,X extends Throwable> ThrowingFunction<T,R,X> of(ThrowingFunction<T,R,X> function)
Factory method for turningThrowingFunction-shaped lambdas intoThrowingFunctions.- Type Parameters:
T- The type of the input to the function.R- The type of the result of the function.X- The type of checked exception that can be thrown.- Parameters:
function- The lambda to return asThrowingFunction.- Returns:
- The given lambda as a
ThrowingFunction. - Throws:
NullPointerException- Iffunctionisnull.
-
identity
static <T,X extends Throwable> ThrowingFunction<T,T,X> identity()
Returns a function that always returns its input argument.- Type Parameters:
T- The type of the input and output objects to the function.X- The type of checked exception that can be thrown.- Returns:
- A function that always returns its input argument.
-
unchecked
static <T,R> Function<T,R> unchecked(ThrowingFunction<? super T,? extends R,?> function)
Returns a function that applies thefunctionfunction to its input. Any checked exception thrown by thefunctionfunction is wrapped in anUncheckedException.- Type Parameters:
T- The type of the input to the function.R- The type of the result of the function.- Parameters:
function- The function to apply when the returned function is applied.- Returns:
- A function that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- Iffunctionisnull.
-
checked
static <T,R,X extends Throwable> ThrowingFunction<T,R,X> checked(Function<? super T,? extends R> function)
Returns a function that applies thefunctionfunction to its input. Any unchecked exception thrown by thefunctionfunction is relayed to the caller. This method allows existingFunctioninstances to be used whereThrowingFunctionis expected.- Type Parameters:
T- The type of the input to the function.R- The type of the result of the function.X- The type of checked exception that can be thrown.- Parameters:
function- The function to apply when the returned function is applied.- Returns:
- A function that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- Iffunctionisnull.
-
checked
static <T,R,X extends Throwable> ThrowingFunction<T,R,X> checked(Function<? super T,? extends R> function, Class<X> errorType)
Returns a function that applies thefunctionfunction to its input. AnyUncheckedExceptionthrown by thefunctionfunction is unwrapped if its cause is an instance oferrorType, otherwise it is relayed to the caller.- Type Parameters:
T- The type of the input to the function.R- The type of the result of the function.X- The type of checked exception that can be thrown.- Parameters:
function- The function to apply when the returned function is applied.errorType- The type of checked exception that can be thrown.- Returns:
- A function that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- IffunctionorerrorTypeisnull.
-
-