Interface CheckedBiFunction<T1,T2,R>
-
- 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 CheckedBiFunction<T1,T2,R>
Variant ofBiFunctionthat behaves likeBiFunction, but which allows checkedException's to be thrown from itsapply(Object, Object)method
– the first function argument type – the second function argument type – the function result type
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description Rapply(T1 arg1, T2 arg2)This method performs the operation that may fail with a CheckedExceptionstatic <T1,T2,R>
BiFunction<T1,T2,R>safe(CheckedBiFunction<T1,T2,R> functionThatCanFailWithACheckedException)Wraps aCheckedBiFunction(basically a lambda with two arguments that returns a result and which throws a CheckedException) by returning a newBiFunctioninstance
The returnedBiFunction.apply(Object, Object)method delegates directly to theapply(Object, Object)and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedBiFunction)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 aBiFunctionwith the purpose of the calling theBiFunction.apply(Object, Object).static <T1,T2,R>
BiFunction<T1,T2,R>safe(String contextMessage, CheckedBiFunction<T1,T2,R> functionThatCanFailWithACheckedException)Wraps aCheckedBiFunction(basically a lambda with two arguments that returns a result and which throws a CheckedException) by returning a newBiFunctioninstance
The returnedBiFunction.apply(Object, Object)method delegates directly to theapply(Object, 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 aBiFunctionwith the purpose of the calling theBiFunction.apply(Object, Object).
-
-
-
Method Detail
-
safe
static <T1,T2,R> BiFunction<T1,T2,R> safe(CheckedBiFunction<T1,T2,R> functionThatCanFailWithACheckedException)
Wraps aCheckedBiFunction(basically a lambda with two arguments that returns a result and which throws a CheckedException) by returning a newBiFunctioninstance
The returnedBiFunction.apply(Object, Object)method delegates directly to theapply(Object, Object)and catches any thrown checkedException's and rethrows them as aCheckedExceptionRethrownException
Unless you provide a context-message (usingsafe(String, CheckedBiFunction)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 aBiFunctionwith the purpose of the calling theBiFunction.apply(Object, Object).
The problem willpublic void someOperation(Function<Integer, Boolean, String> operation) { // ... Logic ... String value = operation.apply(10, true); // ... More logic --- }BiFunction.apply(Object, Object)occurs whenBiFunctioncalls any API that throws a checkedException, e.g. theFileAPI.
SinceBiFunction.apply(Object, 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, enabled) -> { try { // Logic that uses the File API return "some-value"; } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedBiFunctioncomes to the aid as itsapply(Object, Object)methods defines that it throws a CheckedExceptionand itssafe(CheckedBiFunction)will return a newBiFunctioninstance with aBiFunction.apply(Object, Object)method that ensures that theapply(Object, Object)method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedBiFunction.safe((value, enabled) -> { // Logic that uses the File API that throws IOException return "some-value"; }));- Type Parameters:
T1- the first argument typeT2- the second argument typeR- the return type- Parameters:
functionThatCanFailWithACheckedException- theCheckedBiFunctioninstance that will be wrapped as aBiFunction- Returns:
- a
BiFunctionwith aBiFunction.apply(Object, Object)method that ensures that theapply(Object, Object)method is called whenBiFunction.apply(Object, 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 theapply(Object, Object)method throws a checkedException
-
safe
static <T1,T2,R> BiFunction<T1,T2,R> safe(String contextMessage, CheckedBiFunction<T1,T2,R> functionThatCanFailWithACheckedException)
Wraps aCheckedBiFunction(basically a lambda with two arguments that returns a result and which throws a CheckedException) by returning a newBiFunctioninstance
The returnedBiFunction.apply(Object, Object)method delegates directly to theapply(Object, 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 aBiFunctionwith the purpose of the calling theBiFunction.apply(Object, Object).
The problem willpublic void someOperation(Function<Integer, Boolean, String> operation) { // ... Logic ... String value = operation.apply(10, true); // ... More logic --- }BiFunction.apply(Object, Object)occurs whenBiFunctioncalls any API that throws a checkedException, e.g. theFileAPI.
SinceBiFunction.apply(Object, 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, enabled) -> { try { // Logic that uses the File API return "some-value"; } catch (IOException e) { throw new RuntimeException(e); } }));
This is where theCheckedBiFunctioncomes to the aid as itsapply(Object, Object)methods defines that it throws a CheckedExceptionand itssafe(CheckedBiFunction)will return a newBiFunctioninstance with aBiFunction.apply(Object, Object)method that ensures that theapply(Object, Object)method is called and any checkedException's thrown will be caught and rethrown as aCheckedExceptionRethrownException:someOperation(CheckedBiFunction.safe(msg("Processing file {}", fileName), (value, enabled) -> { // Logic that uses the File API that throws IOException return "some-value"; }));- Type Parameters:
T1- the first argument typeT2- the second argument typeR- the return type- Parameters:
contextMessage- a string that the described the content for theCheckedBiFunctionand which will be used as message in anyCheckedExceptionRethrownExceptionthrownfunctionThatCanFailWithACheckedException- theCheckedBiFunctioninstance that will be wrapped as aBiFunction- Returns:
- a
BiFunctionwith aBiFunction.apply(Object, Object)method that ensures that theapply(Object, Object)method is called whenBiFunction.apply(Object, 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 theapply(Object, Object)method throws a checkedException
-
apply
R apply(T1 arg1, T2 arg2) throws Exception
This method performs the operation that may fail with a CheckedException- Parameters:
arg1- the first function argumentarg2- the second function argument- Returns:
- the result of performing the function
- Throws:
Exception- this method can throw both checkedException's as well asRuntimeException's
-
-