Interface ConsumerWithException<T,E extends Exception>
-
- Type Parameters:
T
- the type of the input to the operationE
- the type of the potential exception of the operation
- All Superinterfaces:
ExceptionHandlerSupport<Consumer<T>,Consumer<T>,ConsumerWithException<T,E>>
,NoReturnExceptionHandlerSupport<Consumer<T>,Function<T,CompletionStage<Void>>,ConsumerWithException<T,E>>
- 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 ConsumerWithException<T,E extends Exception> extends NoReturnExceptionHandlerSupport<Consumer<T>,Function<T,CompletionStage<Void>>,ConsumerWithException<T,E>>
Represents an operation that accepts a single input argument, may thrown exception and returns no result. Unlike most other functional interfaces,ConsumerWithException
is expected to operate via side-effects.General contract
void accept(T t) throws E
- The functional method.- uncheck - Return a
Consumer<T>
- lift - Return a
Consumer<T>
- ignore - Return a
Consumer<T>
- See Also:
Consumer
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description void
accept(T t)
Performs this operation on the given argument.default ConsumerWithException<T,E>
andThen(ConsumerWithException<? super T,? extends E> after)
Returns a composedConsumerWithException
that performs, in sequence, this operation followed by theafter
operation.default <R> FunctionWithException<T,R,E>
asFunction()
Converts aConsumerWithException
to aFunctionWithException
returningnull
.static <T,R,E extends Exception>
FunctionWithException<T,R,E>asFunction(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to aFunctionWithException
returningnull
.static <T,E extends Exception>
ConsumerWithException<T,E>failing(Supplier<E> exceptionBuilder)
Returns aConsumerWithException
that always throw exception.default Consumer<T>
ignore()
Converts thisConsumerWithException
to a liftedConsumer
that ignore exception.static <T,E extends Exception>
Consumer<T>ignored(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to a liftedConsumer
ignoring exception.static <T,E extends Exception>
Consumer<T>lifted(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to a liftedConsumer
ignoring exception.default Function<T,CompletionStage<Void>>
stage()
Converts thisConsumerWithException
to a stagedFunction
that return aCompletionStage
.static <T,E extends Exception>
Function<T,CompletionStage<Void>>staged(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to a stagedFunction
.default Consumer<T>
uncheck()
Converts thisConsumerWithException
to aConsumer
that wraps exception toRuntimeException
.static <T,E extends Exception>
Consumer<T>unchecked(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to aConsumer
that wraps exception toRuntimeException
.static <T,E extends Exception>
Consumer<T>unchecked(ConsumerWithException<T,E> consumer, Function<Exception,RuntimeException> exceptionMapper)
Converts aConsumerWithException
to aConsumer
that wraps exception toRuntimeException
by using the provided mapping function.-
Methods inherited from interface ch.powerunit.extensions.exceptions.ExceptionHandlerSupport
documented, exceptionMapper
-
Methods inherited from interface ch.powerunit.extensions.exceptions.NoReturnExceptionHandlerSupport
lift, notThrowingHandler, throwingHandler
-
-
-
-
Method Detail
-
accept
void accept(T t) throws E extends Exception
Performs this operation on the given argument.- Parameters:
t
- the input argument- Throws:
E
- any exceptionE extends Exception
- See Also:
Consumer.accept(Object)
-
uncheck
default Consumer<T> uncheck()
Converts thisConsumerWithException
to aConsumer
that wraps exception toRuntimeException
.- Specified by:
uncheck
in interfaceExceptionHandlerSupport<Consumer<T>,Consumer<T>,ConsumerWithException<T,E extends Exception>>
- Specified by:
uncheck
in interfaceNoReturnExceptionHandlerSupport<Consumer<T>,Function<T,CompletionStage<Void>>,ConsumerWithException<T,E extends Exception>>
- Returns:
- the unchecked operation
- See Also:
unchecked(ConsumerWithException)
,unchecked(ConsumerWithException, Function)
,Consumer
-
ignore
default Consumer<T> ignore()
Converts thisConsumerWithException
to a liftedConsumer
that ignore exception.- Specified by:
ignore
in interfaceExceptionHandlerSupport<Consumer<T>,Consumer<T>,ConsumerWithException<T,E extends Exception>>
- Specified by:
ignore
in interfaceNoReturnExceptionHandlerSupport<Consumer<T>,Function<T,CompletionStage<Void>>,ConsumerWithException<T,E extends Exception>>
- Returns:
- the operation that ignore error
- See Also:
ignored(ConsumerWithException)
,Consumer
-
stage
default Function<T,CompletionStage<Void>> stage()
Converts thisConsumerWithException
to a stagedFunction
that return aCompletionStage
.- Specified by:
stage
in interfaceNoReturnExceptionHandlerSupport<Consumer<T>,Function<T,CompletionStage<Void>>,ConsumerWithException<T,E extends Exception>>
- Returns:
- the staged operation.
- Since:
- 1.1.0
-
andThen
default ConsumerWithException<T,E> andThen(ConsumerWithException<? super T,? extends E> after)
Returns a composedConsumerWithException
that performs, in sequence, this operation followed by theafter
operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, theafter
operation will not be performed.- Parameters:
after
- the operation to perform after this operation- Returns:
- a composed
ConsumerWithException
that performs in sequence this operation followed by theafter
operation - Throws:
NullPointerException
- ifafter
is null- See Also:
Consumer.andThen(Consumer)
-
failing
static <T,E extends Exception> ConsumerWithException<T,E> failing(Supplier<E> exceptionBuilder)
Returns aConsumerWithException
that always throw exception.- Type Parameters:
T
- the type of the input to the operationE
- the type of the exception- Parameters:
exceptionBuilder
- the supplier to create the exception- Returns:
- an operation that always throw exception
-
unchecked
static <T,E extends Exception> Consumer<T> unchecked(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to aConsumer
that wraps exception toRuntimeException
.For example :
ConsumerWithException<String, IOException> consumerThrowingException = ...; Consumer<String> consumerThrowingRuntimeException = ConsumerWithException.unchecked(consumerThrowingException); Stream....forEach(consumerThrowingRuntimeException);
In case of exception insideconsumerThrowingRuntimeException
an instance ofWrappedException
with the original exception as cause will be thrown.- Type Parameters:
T
- the type of the input to the operationE
- the type of the potential exception- Parameters:
consumer
- to be unchecked- Returns:
- the unchecked exception
- Throws:
NullPointerException
- if consumer is null- See Also:
uncheck()
,unchecked(ConsumerWithException, Function)
-
unchecked
static <T,E extends Exception> Consumer<T> unchecked(ConsumerWithException<T,E> consumer, Function<Exception,RuntimeException> exceptionMapper)
Converts aConsumerWithException
to aConsumer
that wraps exception toRuntimeException
by using the provided mapping function.For example :
ConsumerWithException<String, IOException> consumerThrowingException = ...; Consumer<String> consumerThrowingRuntimeException = ConsumerWithException.unchecked( consumerThrowingException, IllegalArgumentException::new ); Stream....forEach(consumerThrowingRuntimeException);
In case of exception insideconsumerThrowingRuntimeException
an instance ofIllegalArgumentException
with the original exception as cause will be thrown.- Type Parameters:
T
- the type of the input to the operationE
- the type of the potential exception- Parameters:
consumer
- the be uncheckedexceptionMapper
- a function to convert the exception to the runtime exception.- Returns:
- the unchecked exception
- Throws:
NullPointerException
- if consumer or exceptionMapper is null- See Also:
uncheck()
,unchecked(ConsumerWithException)
-
lifted
static <T,E extends Exception> Consumer<T> lifted(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to a liftedConsumer
ignoring exception.- Type Parameters:
T
- the type of the input to the operationE
- the type of the potential exception- Parameters:
consumer
- to be lifted- Returns:
- the lifted operation
- Throws:
NullPointerException
- if consumer is null- See Also:
NoReturnExceptionHandlerSupport.lift()
-
ignored
static <T,E extends Exception> Consumer<T> ignored(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to a liftedConsumer
ignoring exception.- Type Parameters:
T
- the type of the input to the operationE
- the type of the potential exception- Parameters:
consumer
- to be lifted- Returns:
- the lifted operation
- Throws:
NullPointerException
- if consumer is null- See Also:
ignore()
-
staged
static <T,E extends Exception> Function<T,CompletionStage<Void>> staged(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to a stagedFunction
.- Type Parameters:
T
- the type of the input to the operationE
- the type of the potential exception- Parameters:
consumer
- to be staged- Returns:
- the staged operation
- Throws:
NullPointerException
- if consumer is null- Since:
- 1.1.0
-
asFunction
static <T,R,E extends Exception> FunctionWithException<T,R,E> asFunction(ConsumerWithException<T,E> consumer)
Converts aConsumerWithException
to aFunctionWithException
returningnull
.- Type Parameters:
T
- the type of the input to the operationR
- the type of the return valueE
- the type of the potential exception- Parameters:
consumer
- the consumer to be converted- Returns:
- the function
- Throws:
NullPointerException
- if consumer is null
-
asFunction
default <R> FunctionWithException<T,R,E> asFunction()
Converts aConsumerWithException
to aFunctionWithException
returningnull
.- Type Parameters:
R
- the type of the return value- Returns:
- the function
- Since:
- 1.2.0
-
-