Interface CheckedSupplier<R>
-
- Type Parameters:
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 CheckedSupplier<R>
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description Rget()This method performs the operation that may fail with a CheckedExceptionstatic <R> Supplier<R>safe(CheckedSupplier<R> supplierThatCanFailedWithACheckedException)Wraps aCheckedSupplier(basically a lambda with no arguments that returns a result and which throws a CheckedException) by returning a newSupplierinstance
The returnedSupplier.get()method delegates directly to theget()and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedSupplier)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 aSupplierwith the purpose of the calling theSupplier.get().static <R> Supplier<R>safe(String contextMessage, CheckedSupplier<R> supplierThatCanFailedWithACheckedException)Wraps aCheckedSupplier(basically a lambda with no arguments that returns a result and which throws a CheckedException) by returning a newSupplierinstance
The returnedSupplier.get()method delegates directly to theget()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 aSupplierwith the purpose of the calling theSupplier.get().
-
-
-
Method Detail
-
safe
static <R> Supplier<R> safe(CheckedSupplier<R> supplierThatCanFailedWithACheckedException)
Wraps aCheckedSupplier(basically a lambda with no arguments that returns a result and which throws a CheckedException) by returning a newSupplierinstance
The returnedSupplier.get()method delegates directly to theget()and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedSupplier)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 aSupplierwith the purpose of the calling theSupplier.get().
The problem willpublic void someOperation(Supplier<String> operation) { // ... Logic ... String value = operation.get(); // ... More logic --- }Supplier.get()occurs whenSuppliercalls any API that throws a checkedException, e.g. theFileAPI.
SinceSupplier.get()doesn't define that it throws anyException's we're forced to add a try/catch to handle theIOExceptionfor the code to compile:someOperation(() -> { try { // Logic that uses the File API return "some-value"; } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedSuppliercomes to the aid as itsget()methods defines that it throws a CheckedExceptionand itssafe(CheckedSupplier)will return a newSupplierinstance with aSupplier.get()method that ensures that theget()method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedSupplier.safe(() -> { // Logic that uses the File API that throws IOException return "some-value"; }));- Type Parameters:
R- the return type- Parameters:
supplierThatCanFailedWithACheckedException- theCheckedSupplierinstance that will be wrapped as aSupplier- Returns:
- a
Supplierwith aSupplier.get()method that ensures that theget()method is called whenSupplier.get()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 theget()method throws a checkedException
-
safe
static <R> Supplier<R> safe(String contextMessage, CheckedSupplier<R> supplierThatCanFailedWithACheckedException)
Wraps aCheckedSupplier(basically a lambda with no arguments that returns a result and which throws a CheckedException) by returning a newSupplierinstance
The returnedSupplier.get()method delegates directly to theget()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 aSupplierwith the purpose of the calling theSupplier.get().
The problem willpublic void someOperation(Supplier<String> operation) { // ... Logic ... String value = operation.get(); // ... More logic --- }Supplier.get()occurs whenSuppliercalls any API that throws a checkedException, e.g. theFileAPI.
SinceSupplier.get()doesn't define that it throws anyException's we're forced to add a try/catch to handle theIOExceptionfor the code to compile:someOperation(() -> { try { // Logic that uses the File API return "some-value"; } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedSuppliercomes to the aid as itsget()methods defines that it throws a CheckedExceptionand itssafe(CheckedSupplier)will return a newSupplierinstance with aSupplier.get()method that ensures that theget()method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedSupplier.safe(msg("Processing file {}", fileName), () -> { // Logic that uses the File API that throws IOException return "some-value"; }));- Type Parameters:
R- the return type- Parameters:
contextMessage- a string that the described the content for theCheckedSupplierand which will be used as message in anyCheckedExceptionRethrownExceptionthrownsupplierThatCanFailedWithACheckedException- theCheckedSupplierinstance that will be wrapped as aSupplier- Returns:
- a
Supplierwith aSupplier.get()method that ensures that theget()method is called whenSupplier.get()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 theget()method throws a checkedException
-
-