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/CheckedFunction.java | |
parent | 60503351a30a91985b95ee8aa64e163ef084b34b (diff) |
Add dicore3-core, without really irrelevant packages
Diffstat (limited to 'dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunction.java')
-rw-r--r-- | dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunction.java | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunction.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunction.java new file mode 100644 index 0000000..9ff806a --- /dev/null +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunction.java @@ -0,0 +1,74 @@ +package io.dico.dicore.exceptions.checkedfunctions; + +import io.dico.dicore.exceptions.ExceptionHandler; + +import java.util.function.Function; + +/** + * checked mimic of {@link Function} + * + * @param <TParam> + * @param <TResult> + * @param <TException> + */ +@FunctionalInterface +public interface CheckedFunction<TParam, TResult, TException extends Throwable> + extends CheckedFunctionalObject<TResult, TException>, Function<TParam, TResult> { + + /** + * the functional method + * + * @param t the input + * @return the function output + * @throws TException if an exception occurs + */ + TResult checkedApply(TParam t) throws TException; + + /** + * unchecked wrapper for {@link #checkedApply(Object)} + * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)} + * + * @param t the input + * @return the function output, or the result from {@link #resultOnError(Throwable, Object...)} if an exception was thrown + * @see #checkedApply(Object) + * @see #resultOnError(Throwable, Object...) + */ + @Override + default TResult apply(TParam t) { + try { + return checkedApply(t); + } catch (Throwable ex) { + return handleGenericException(ex, t); + } + } + + /** + * {@inheritDoc} + */ + @Override + default CheckedFunction<TParam, TResult, TException> handleExceptionsWith(ExceptionHandler handler) { + return new CheckedFunction<TParam, TResult, TException>() { + @Override + public TResult checkedApply(TParam t) throws TException { + return CheckedFunction.this.checkedApply(t); + } + + @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 CheckedFunction<TParam, TResult, TException> handleExceptionsWith(ExceptionHandler handler) { + return CheckedFunction.this.handleExceptionsWith(handler); + } + }; + } + +} |