Package io.microsphere.lang.function
Interface ThrowableSupplier<T>
-
- Type Parameters:
T
- the type of result supplied by this supplier
- 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 ThrowableSupplier<T>
A functional interface similar toSupplier
, but allows theget()
method to throw aThrowable
. This is useful for functional constructs where operations may throw checked exceptions that need to be handled or rethrown.Example Usage
// Using ThrowableSupplier to read a file content ThrowableSupplier<String> fileReader = () -> { Path path = Paths.get("example.txt"); return Files.readString(path); }; // Execute with default exception handling (converts to RuntimeException) String content = fileReader.execute(); // Execute with custom exception handling String contentWithHandler = fileReader.execute(ex -> { System.err.println("Error reading file: " + ex.getMessage()); return "default content"; });
This interface provides convenience methods to execute the supplier and handle exceptions using a custom handler, making it easier to work with functional patterns in environments where exceptions must be managed.
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
Supplier
,Throwable
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default T
execute()
Executeget()
withthe default exception handling
static <T> T
execute(ThrowableSupplier<T> supplier)
Executes the givenThrowableSupplier
with the default exception handling.static <T> T
execute(ThrowableSupplier<T> supplier, java.util.function.Function<java.lang.Throwable,T> exceptionHandler)
Executes the givenThrowableSupplier
with a custom exception handler.default T
execute(java.util.function.Function<java.lang.Throwable,T> exceptionHandler)
Executeget()
with the customizedexception
handlingT
get()
Applies this function to the given argument.default T
handleException(java.lang.Throwable failure)
Handle any exception that theget()
method throws
-
-
-
Method Detail
-
get
T get() throws java.lang.Throwable
Applies this function to the given argument.- Returns:
- the supplied result
- Throws:
java.lang.Throwable
- if met with any error
-
execute
default T execute()
Executeget()
withthe default exception handling
- Returns:
- the supplied result
- See Also:
get()
-
execute
default T execute(java.util.function.Function<java.lang.Throwable,T> exceptionHandler)
Executeget()
with the customizedexception
handling
-
handleException
default T handleException(java.lang.Throwable failure)
Handle any exception that theget()
method throws- Parameters:
failure
- the instance ofThrowable
- Returns:
- the result after the exception handling
-
execute
static <T> T execute(ThrowableSupplier<T> supplier) throws java.lang.NullPointerException
Executes the givenThrowableSupplier
with the default exception handling.This method is useful when you want to invoke a supplier that may throw checked exceptions without explicitly handling them. Any exception thrown during the execution of the supplier will be passed to the default exception handler, which wraps it into a
RuntimeException
.Example Usage
// Using the static execute method with default exception handling String content = ThrowableSupplier.execute(() -> { Path path = Paths.get("example.txt"); return Files.readString(path); });
- Type Parameters:
T
- The type of result supplied by the supplier.- Parameters:
supplier
- The supplier to execute.- Returns:
- The result of the supplier after successful execution.
- Throws:
java.lang.NullPointerException
- if the given supplier isnull
.
-
execute
static <T> T execute(ThrowableSupplier<T> supplier, java.util.function.Function<java.lang.Throwable,T> exceptionHandler) throws java.lang.NullPointerException
Executes the givenThrowableSupplier
with a custom exception handler.This method allows for flexible exception handling when executing a supplier that may throw checked exceptions. If the supplier or exception handler is null, a
NullPointerException
will be thrown.Example Usage
// Example 1: Using a custom exception handler to return a default value String content = execute( () -> Files.readString(Paths.get("example.txt")), ex -> "Default Content" ); // Example 2: Logging and rethrowing as a custom exception String content = execute( () -> Files.readString(Paths.get("example.txt")), ex -> { System.err.println("Failed to read file: " + ex.getMessage()); throw new RuntimeException(ex); } );
- Type Parameters:
T
- The type of result supplied by the supplier.- Parameters:
supplier
- The supplier to execute. Must not be null.exceptionHandler
- The handler to manage any exception thrown by the supplier. Must not be null.- Returns:
- The result of the supplier after execution or after handling an exception.
- Throws:
java.lang.NullPointerException
- if either the supplier or the exception handler is null.
-
-