Interface ThrowingConsumer<T,X extends Throwable>
-
- Type Parameters:
T- The type of the input to the operation.X- The type of checked exception that can be thrown.
- 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 ThrowingConsumer<T,X extends Throwable>
Represents an operation that accepts a single input argument and returns no result. This is a checked-exception throwing equivalent ofConsumer.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidaccept(T t)Performs this operation on the given argument.default ThrowingConsumer<T,X>andThen(ThrowingConsumer<? super T,? extends X> after)Returns a composedThrowingConsumerthat performs, in sequence, this operation followed by theafteroperation.static <T,X extends Throwable>
ThrowingConsumer<T,X>checked(Consumer<? super T> operation)Returns an operation that performs theoperationoperation to its input.static <T,X extends Throwable>
ThrowingConsumer<T,X>checked(Consumer<? super T> operation, Class<X> errorType)Returns an operation that performs theoperationoperation to its input.static <T,X extends Throwable>
ThrowingConsumer<T,X>of(ThrowingConsumer<T,X> operation)Factory method for turningThrowingConsumer-shaped lambdas intoThrowingConsumers.default <E extends Throwable>
ThrowingConsumer<T,E>onErrorAcceptChecked(ThrowingConsumer<? super T,? extends E> fallback)Returns an operation that performs this operation on its input.default Consumer<T>onErrorAcceptUnchecked(Consumer<? super T> fallback)Returns an operation that performs this operation on its input.default Consumer<T>onErrorDiscard()Returns an operation that performs this operation on its input.default <E extends Throwable>
ThrowingConsumer<T,E>onErrorHandleChecked(ThrowingConsumer<? super X,? extends E> errorHandler)Returns an operation that performs this operation on its input.default Consumer<T>onErrorHandleUnchecked(Consumer<? super X> errorHandler)Returns an operation that performs this operation on its input.default <E extends Throwable>
ThrowingConsumer<T,E>onErrorThrowAsChecked(Function<? super X,? extends E> errorMapper)Returns an operation that performs this operation on its input.default <E extends RuntimeException>
Consumer<T>onErrorThrowAsUnchecked(Function<? super X,? extends E> errorMapper)Returns an operation that performs this operation on its input.default Consumer<T>unchecked()Returns an operation that performs this operation on its input.static <T> Consumer<T>unchecked(ThrowingConsumer<? super T,?> operation)Returns an operation that performs theoperationoperation to its input.
-
-
-
Method Detail
-
andThen
default ThrowingConsumer<T,X> andThen(ThrowingConsumer<? super T,? extends X> after)
Returns a composedThrowingConsumerthat performs, in sequence, this operation followed by theafteroperation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, theafteroperation will not be performed.- Parameters:
after- The operation to perform after this operation.- Returns:
- A composed
ThrowingConsumerthat performs in sequence this operation followed by theafteroperation. - Throws:
NullPointerException- Ifafterisnull.
-
onErrorThrowAsChecked
default <E extends Throwable> ThrowingConsumer<T,E> onErrorThrowAsChecked(Function<? super X,? extends E> errorMapper)
Returns an operation that performs this operation on its input. Any checked exception thrown by this operation is transformed using the given error mapper, and the returned operation 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 operation.- Returns:
- An operation that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorMapperisnull.
-
onErrorThrowAsUnchecked
default <E extends RuntimeException> Consumer<T> onErrorThrowAsUnchecked(Function<? super X,? extends E> errorMapper)
Returns an operation that performs this operation on its input. Any checked exception thrown by this operation is transformed using the given error mapper, and the returned operation 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 operation.- Returns:
- An operation that transforms any thrown checked exception.
- Throws:
NullPointerException- IferrorMapperisnull.
-
onErrorHandleChecked
default <E extends Throwable> ThrowingConsumer<T,E> onErrorHandleChecked(ThrowingConsumer<? super X,? extends E> errorHandler)
Returns an operation that performs this operation on its input. Any checked exception thrown by this operation is handled by the given error handler.- Type Parameters:
E- The type of checked exception that can be thrown by the given error handler.- Parameters:
errorHandler- The operation to perform on any checked exception thrown by this operation.- Returns:
- An operation that handles any thrown checked exception.
- Throws:
NullPointerException- IferrorHandlerisnull.
-
onErrorHandleUnchecked
default Consumer<T> onErrorHandleUnchecked(Consumer<? super X> errorHandler)
Returns an operation that performs this operation on its input. Any checked exception thrown by this operation is handled by the given error handler.- Parameters:
errorHandler- The operation to perform on any checked exception thrown by this operation.- Returns:
- An operation that handles any thrown checked exception.
- Throws:
NullPointerException- IferrorHandlerisnull.
-
onErrorAcceptChecked
default <E extends Throwable> ThrowingConsumer<T,E> onErrorAcceptChecked(ThrowingConsumer<? super T,? extends E> fallback)
Returns an operation that performs this operation on its input. If this operation throws any checked exception, it is discarded and the given fallback operation is invoked.- Type Parameters:
E- The type of checked exception that can be thrown by the given fallback operation.- Parameters:
fallback- The operation to invoke if this operation throws any checked exception.- Returns:
- An operation that invokes the
fallbackoperation if this operation throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorAcceptUnchecked
default Consumer<T> onErrorAcceptUnchecked(Consumer<? super T> fallback)
Returns an operation that performs this operation on its input. If this operation throws any checked exception, it is discarded and the given fallback operation is invoked.- Parameters:
fallback- The operation to invoke if this operation throws any checked exception.- Returns:
- An operation that invokes the
fallbackoperation if this operation throws any checked exception. - Throws:
NullPointerException- Iffallbackisnull.
-
onErrorDiscard
default Consumer<T> onErrorDiscard()
Returns an operation that performs this operation on its input. Any checked exception thrown by this operation is discarded.- Returns:
- An operation that discards any thrown checked exception.
-
unchecked
default Consumer<T> unchecked()
Returns an operation that performs this operation on its input. Any checked exception thrown by this operation is wrapped in anUncheckedExceptionwithout a stack trace.- Returns:
- An operation that wraps any checked exception in an
UncheckedException.
-
of
static <T,X extends Throwable> ThrowingConsumer<T,X> of(ThrowingConsumer<T,X> operation)
Factory method for turningThrowingConsumer-shaped lambdas intoThrowingConsumers.- Type Parameters:
T- The type of the input to the operation.X- The type of checked exception that can be thrown.- Parameters:
operation- The lambda to return asThrowingConsumer.- Returns:
- The given lambda as a
ThrowingConsumer. - Throws:
NullPointerException- Ifoperationisnull.
-
unchecked
static <T> Consumer<T> unchecked(ThrowingConsumer<? super T,?> operation)
Returns an operation that performs theoperationoperation to its input. Any checked exception thrown by theoperationoperation is wrapped in anUncheckedException.- Type Parameters:
T- The type of the input to the operation.- Parameters:
operation- The operation to perform when the returned operation is performed.- Returns:
- An operation that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- Ifoperationisnull.
-
checked
static <T,X extends Throwable> ThrowingConsumer<T,X> checked(Consumer<? super T> operation)
Returns an operation that performs theoperationoperation to its input. Any checked exception thrown by theoperationoperation is relayed to the caller. This method allows existingConsumerinstances to be used whereThrowingConsumeris expected.- Type Parameters:
T- The type of the input to the operation.X- The type of checked exception that can be thrown.- Parameters:
operation- The operation to perform when the returned operation is performed.- Returns:
- An operation that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- Ifoperationisnull.
-
checked
static <T,X extends Throwable> ThrowingConsumer<T,X> checked(Consumer<? super T> operation, Class<X> errorType)
Returns an operation that performs theoperationoperation to its input. AnyUncheckedExceptionthrown by theoperationoperation 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 operation.X- The type of checked exception that can be thrown.- Parameters:
operation- The operation to perform when the returned operation is performed.errorType- The type of checked exception that can be thrown.- Returns:
- An operation that wraps any checked exception in an
UncheckedException. - Throws:
NullPointerException- IfoperationorerrorTypeisnull.
-
-