Interface CheckedConsumer<T>
-
- 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 CheckedConsumer<T>
Variant ofConsumerthat behaves likeConsumer, but which allows checkedException's to be thrown from itsaccept(Object)method
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaccept(T arg)This method performs the operation that may fail with a CheckedExceptionstatic <T> Consumer<T>safe(CheckedConsumer<T> runnableThatCanFailWithACheckedException)Wraps aCheckedConsumer(basically a lambda with one argument that doesn't return any result and which throws a CheckedException) by returning a newConsumerinstance
The returnedConsumer.accept(Object)method delegates directly to theaccept(Object)and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedConsumer)then any caught checkedException's message also becomes theCheckedExceptionRethrownException's message.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aConsumerwith the purpose of the calling theConsumer.accept(Object).static <T> Consumer<T>safe(String contextMessage, CheckedConsumer<T> runnableThatCanFailWithACheckedException)Wraps aCheckedConsumer(basically a lambda with one argument that doesn't return any result and which throws a CheckedException) by returning a newConsumerinstance
The returnedConsumer.accept(Object)method delegates directly to theaccept(Object)and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aConsumerwith the purpose of the calling theConsumer.accept(Object).
-
-
-
Method Detail
-
safe
static <T> Consumer<T> safe(CheckedConsumer<T> runnableThatCanFailWithACheckedException)
Wraps aCheckedConsumer(basically a lambda with one argument that doesn't return any result and which throws a CheckedException) by returning a newConsumerinstance
The returnedConsumer.accept(Object)method delegates directly to theaccept(Object)and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedConsumer)then any caught checkedException's message also becomes theCheckedExceptionRethrownException's message.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aConsumerwith the purpose of the calling theConsumer.accept(Object).
The problem willpublic void someOperation(Consumer<String> operation) { // ... Logic ... operation.accept("some-value"); // ... More logic --- }Consumer.accept(Object)occurs whenConsumercalls any API that throws a checkedException, e.g. theFileAPI.
SinceConsumer.accept(Object)doesn't define that it throws anyException's we're forced to add a try/catch to handle theIOExceptionfor the code to compile:someOperation(value -> { try { // Logic that uses the File API } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedConsumercomes to the aid as itsaccept(Object)methods defines that it throws a CheckedExceptionand itssafe(CheckedConsumer)will return a newConsumerinstance with aConsumer.accept(Object)method that ensures that theaccept(Object)method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedConsumer.safe(value -> { // Logic that uses the File API that throws IOException }));- Parameters:
runnableThatCanFailWithACheckedException- theCheckedConsumerinstance that will be wrapped as aConsumer- Returns:
- a
Consumerwith aConsumer.accept(Object)method that ensures that theaccept(Object)method is called whenConsumer.accept(Object)and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown. - Throws:
CheckedExceptionRethrownException- in case theaccept(Object)method throws a checkedException
-
safe
static <T> Consumer<T> safe(String contextMessage, CheckedConsumer<T> runnableThatCanFailWithACheckedException)
Wraps aCheckedConsumer(basically a lambda with one argument that doesn't return any result and which throws a CheckedException) by returning a newConsumerinstance
The returnedConsumer.accept(Object)method delegates directly to theaccept(Object)and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown.
Usage example:
Let's say we have a method calledsomeOperationthat cannot change, but which accepts aConsumerwith the purpose of the calling theConsumer.accept(Object).
The problem willpublic void someOperation(Consumer<String> operation) { // ... Logic ... operation.accept("some-value"); // ... More logic --- }Consumer.accept(Object)occurs whenConsumercalls any API that throws a checkedException, e.g. theFileAPI.
SinceConsumer.accept(Object)doesn't define that it throws anyException's we're forced to add a try/catch to handle theIOExceptionfor the code to compile:someOperation(value -> { try { // Logic that uses the File API } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedConsumercomes to the aid as itsaccept(Object)methods defines that it throws a CheckedExceptionand itssafe(CheckedConsumer)will return a newConsumerinstance with aConsumer.accept(Object)method that ensures that theaccept(Object)method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedConsumer.safe(msg("Processing file {}", fileName), value -> { // Logic that uses the File API that throws IOException }));- Parameters:
contextMessage- a string that the described the content for theCheckedConsumerand which will be used as message in anyCheckedExceptionRethrownExceptionthrownrunnableThatCanFailWithACheckedException- theCheckedConsumerinstance that will be wrapped as aConsumer- Returns:
- a
Consumerwith aConsumer.accept(Object)method that ensures that theaccept(Object)method is called whenConsumer.accept(Object)and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException.
AnyRuntimeException's thrown aren't caught and the calling code will receive the originalRuntimeExceptionthrown. - Throws:
CheckedExceptionRethrownException- in case theaccept(Object)method throws a checkedException
-
-