Package io.microsphere.lang.function
Interface ThrowableConsumer<T>
-
- Type Parameters:
T- the type of the input to the operation
- 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 ThrowableConsumer<T>Represents an operation that accepts a single input argument and returns no result, which may throw aThrowable.This is the two-arity specialization of
Functionand the throwable-aware functional interface whose functional method isaccept(Object).Example Usage
// Basic usage: ThrowableConsumer<String> printer = System.out::println; printer.accept("Hello World"); // Outputs: Hello World // Throwing an exception: ThrowableConsumer<Integer> riskyConsumer = i -> { if (i < 0) { throw new IllegalArgumentException("Negative value not allowed"); } }; try { riskyConsumer.accept(-1); } catch (Throwable t) { System.err.println("Caught exception: " + t.getMessage()); }- Author:
- Mercy
- See Also:
Consumer,Throwable
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidaccept(T t)Applies this function to the given argument.default voidexecute(T t)Executes#accept(T)withthe default exception handlingstatic <T> voidexecute(T t, ThrowableConsumer<T> consumer)Executes the givenThrowableConsumerwith the default exception handling.static <T> voidexecute(T t, ThrowableConsumer<T> consumer, java.util.function.BiConsumer<T,java.lang.Throwable> exceptionHandler)Executes the givenThrowableConsumerwith a custom exception handler.default voidexecute(T t, java.util.function.BiConsumer<T,java.lang.Throwable> exceptionHandler)Executes#accept(T)with the customized exception handlingdefault voidhandleException(T t, java.lang.Throwable failure)Handle any exception that the#accept(T)method throws
-
-
-
Method Detail
-
accept
void accept(T t) throws java.lang.Throwable
Applies this function to the given argument.- Parameters:
t- the function argument- Throws:
java.lang.Throwable- if met with any error
-
execute
default void execute(T t)
Executes#accept(T)withthe default exception handling- Parameters:
t- the function argument- See Also:
#accept(T)
-
execute
default void execute(T t, java.util.function.BiConsumer<T,java.lang.Throwable> exceptionHandler) throws java.lang.NullPointerException
Executes#accept(T)with the customized exception handling- Parameters:
t- the function argumentexceptionHandler- the handler to handle the function argument and the exception that the#accept(T)method throws- Throws:
java.lang.NullPointerException- ifexceptionHandlerisnull
-
handleException
default void handleException(T t, java.lang.Throwable failure)
Handle any exception that the#accept(T)method throws- Parameters:
t- the value to be consumedfailure- the instance ofThrowable
-
execute
static <T> void execute(T t, ThrowableConsumer<T> consumer) throws java.lang.NullPointerExceptionExecutes the givenThrowableConsumerwith the default exception handling.If an exception is thrown during execution, it will be handled by the
handleException(Object, Throwable)method of the consumer.Example Usage
// Example 1: Basic usage without any exception ThrowableConsumer<String> printConsumer = System.out::println; ThrowableConsumer.execute("Hello World", printConsumer); // Output: Hello World // Example 2: Consumer that throws an exception ThrowableConsumer<Integer> riskyConsumer = i -> { if (i < 0) { throw new IllegalArgumentException("Negative value not allowed"); } }; try { ThrowableConsumer.execute(-1, riskyConsumer); } catch (RuntimeException e) { System.err.println("Caught exception: " + e.getCause().getMessage()); // Output: Caught exception: Negative value not allowed }- Type Parameters:
T- the type of the input argument- Parameters:
t- the input argument to be consumedconsumer- the instance ofThrowableConsumerto execute- Throws:
java.lang.NullPointerException- if the given consumer is null
-
execute
static <T> void execute(T t, ThrowableConsumer<T> consumer, java.util.function.BiConsumer<T,java.lang.Throwable> exceptionHandler) throws java.lang.NullPointerExceptionExecutes the givenThrowableConsumerwith a custom exception handler.If an exception is thrown during execution, it will be passed to the provided
exceptionHandleralong with the input value that caused the exception.Example Usage
// Example 1: Basic usage without any exception ThrowableConsumer<String> printConsumer = System.out::println; ThrowableConsumer.execute("Hello World", printConsumer, (t, e) -> {}); // Output: Hello World // Example 2: Custom exception handling ThrowableConsumer<Integer> riskyConsumer = i -> { if (i < 0) { throw new IllegalArgumentException("Negative value not allowed"); } }; BiConsumer<Integer, Throwable> handler = (value, ex) -> { System.err.println("Error at value " + value + ": " + ex.getMessage()); }; ThrowableConsumer.execute(-1, riskyConsumer, handler); // Output: Error at value -1: Negative value not allowed- Type Parameters:
T- the type of the input argument- Parameters:
t- the input argument to be consumedconsumer- the instance ofThrowableConsumerto executeexceptionHandler- the handler to manage exceptions thrown during execution- Throws:
java.lang.NullPointerException- if the givenconsumerorexceptionHandleris null
-
-