diff options
author | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:02 +0100 |
---|---|---|
committer | Dico200 <dico.karssiens@gmail.com> | 2018-07-25 01:53:02 +0100 |
commit | 5e168847c2624b767deb9da310ecfdf169e0f43c (patch) | |
tree | 56b76556a6837fff20b800d5a22218286a975581 /dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiFunction.java | |
parent | 60503351a30a91985b95ee8aa64e163ef084b34b (diff) |
Add dicore3-core, without really irrelevant packages
Diffstat (limited to 'dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiFunction.java')
-rw-r--r-- | dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiFunction.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiFunction.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiFunction.java new file mode 100644 index 0000000..9b39325 --- /dev/null +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiFunction.java @@ -0,0 +1,76 @@ +package io.dico.dicore.exceptions.checkedfunctions; + +import io.dico.dicore.exceptions.ExceptionHandler; + +import java.util.function.BiFunction; + +/** + * checked mimic of {@link BiFunction} + * + * @param <TParam1> + * @param <TParam2> + * @param <TResult> + * @param <TException> + */ +@FunctionalInterface +public interface CheckedBiFunction<TParam1, TParam2, TResult, TException extends Throwable> + extends CheckedFunctionalObject<TResult, TException>, BiFunction<TParam1, TParam2, TResult> { + + /** + * the functional method + * + * @param t the first input + * @param u the second input + * @return the function output + * @throws TException if an exception occurs + */ + TResult checkedApply(TParam1 t, TParam2 u) throws TException; + + /** + * unchecked wrapper for {@link #checkedApply(Object, Object)} + * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)} + * + * @param t the first input + * @param u the second input + * @return the function output, or the result from {@link #resultOnError(Throwable, Object...)} if an exception was thrown + * @see #checkedApply(Object, Object) + * @see #resultOnError(Throwable, Object...) + */ + @Override + default TResult apply(TParam1 t, TParam2 u) { + try { + return checkedApply(t, u); + } catch (Throwable ex) { + return handleGenericException(ex, t, u); + } + } + + /** + * {@inheritDoc} + */ + @Override + default CheckedBiFunction<TParam1, TParam2, TResult, TException> handleExceptionsWith(ExceptionHandler handler) { + return new CheckedBiFunction<TParam1, TParam2, TResult, TException>() { + @Override + public TResult checkedApply(TParam1 t, TParam2 u) throws TException { + return CheckedBiFunction.this.checkedApply(t, u); + } + + @Override + @SuppressWarnings("unchecked") + public TResult handleGenericException(Throwable thrown, Object... args) { + Object result = handler.handleGenericException(thrown, args); + try { + return (TResult) result; + } catch (Exception ex) { + return null; + } + } + + @Override + public CheckedBiFunction<TParam1, TParam2, TResult, TException> handleExceptionsWith(ExceptionHandler handler) { + return CheckedBiFunction.this.handleExceptionsWith(handler); + } + }; + } +} |