Package io.microsphere.lang.function
Interface ThrowableBiFunction<T,U,R>
-
- Type Parameters:
T
- the type of the first argument to the functionU
- the type of the second argument to the functionR
- the type of the result of the function
- 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 ThrowableBiFunction<T,U,R>
Represents a function that accepts two arguments and produces a result, which may throw aThrowable
.This is a functional interface whose functional method is
apply(Object, Object)
.Example Usage
ThrowableBiFunction<Integer, Integer, Integer> divide = (a, b) -> { if (b == 0) throw new ArithmeticException("Division by zero"); return a / b; }; // Using execute with default exception handling Integer result = ThrowableBiFunction.execute(10, 0, divide); // This will internally handle the exception using DEFAULT_EXCEPTION_HANDLER and throw a RuntimeException // Custom Exception Handling Example: Integer resultWithCustomHandler = ThrowableBiFunction.execute(10, 0, divide, (first, second, error) -> { System.out.println("Error occurred: " + error.getMessage()); return 0; // Default value on failure });
- Author:
- Mercy
- See Also:
BiFunction
,Throwable
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
ThrowableBiFunction.ExceptionHandler<T,U,R>
The handler interface forexception
-
Field Summary
Fields Modifier and Type Field Description static ThrowableBiFunction.ExceptionHandler
DEFAULT_EXCEPTION_HANDLER
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description R
apply(T first, U second)
Applies this function to the given argument.static <T,U,R>
Rexecute(T first, U second, ThrowableBiFunction<T,U,R> function)
Executes the givenThrowableBiFunction
with the provided arguments using the default exception handler.static <T,U,R>
Rexecute(T first, U second, ThrowableBiFunction<T,U,R> function, ThrowableBiFunction.ExceptionHandler<T,U,R> exceptionHandler)
Executes the givenThrowableBiFunction
with the provided arguments using a custom exception handler.
-
-
-
Field Detail
-
DEFAULT_EXCEPTION_HANDLER
static final ThrowableBiFunction.ExceptionHandler DEFAULT_EXCEPTION_HANDLER
-
-
Method Detail
-
apply
R apply(T first, U second) throws java.lang.Throwable
Applies this function to the given argument.- Parameters:
first
- the first argument to be applied for the functionsecond
- the second argument to be applied for the function- Returns:
- the function result
- Throws:
java.lang.Throwable
- if met with any error
-
execute
static <T,U,R> R execute(T first, U second, ThrowableBiFunction<T,U,R> function) throws java.lang.NullPointerException
Executes the givenThrowableBiFunction
with the provided arguments using the default exception handler.If an exception is thrown during the execution of the function, it will be handled by the
DEFAULT_EXCEPTION_HANDLER
, which wraps the exception into aRuntimeException
and throws it.Example Usage
ThrowableBiFunction<Integer, Integer, Integer> divide = (a, b) -> { if (b == 0) throw new ArithmeticException("Division by zero"); return a / b; }; // Execution with default exception handling Integer result = ThrowableBiFunction.execute(10, 0, divide); // This will throw a RuntimeException due to division by zero
- Type Parameters:
T
- the type of the first argumentU
- the type of the second argumentR
- the type of the result- Parameters:
first
- the first argument to apply to the functionsecond
- the second argument to apply to the functionfunction
- the function to execute; must not be null- Returns:
- the result of the function after successful execution
- Throws:
java.lang.NullPointerException
- if the given function is null
-
execute
static <T,U,R> R execute(T first, U second, ThrowableBiFunction<T,U,R> function, ThrowableBiFunction.ExceptionHandler<T,U,R> exceptionHandler) throws java.lang.NullPointerException
Executes the givenThrowableBiFunction
with the provided arguments using a custom exception handler.If an exception is thrown during the execution of the function, it will be handled by the specified
ThrowableBiFunction.ExceptionHandler
, which provides custom logic to recover or respond to the error.Example Usage
ThrowableBiFunction<Integer, Integer, Integer> divide = (a, b) -> { if (b == 0) throw new ArithmeticException("Division by zero"); return a / b; }; // Custom Exception Handling Example: Integer resultWithCustomHandler = ThrowableBiFunction.execute(10, 0, divide, (first, second, error) -> { System.out.println("Error occurred: " + error.getMessage()); return 0; // Default value on failure });
- Type Parameters:
T
- the type of the first argumentU
- the type of the second argumentR
- the type of the result- Parameters:
first
- the first argument to apply to the functionsecond
- the second argument to apply to the functionfunction
- the function to execute; must not be nullexceptionHandler
- the handler to manage exceptions thrown during execution; must not be null- Returns:
- the result of the function after execution (either successful or recovered via exception handling)
- Throws:
java.lang.NullPointerException
- if either the function or exceptionHandler is null
-
-