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/CheckedRunnable.java | |
parent | 60503351a30a91985b95ee8aa64e163ef084b34b (diff) |
Add dicore3-core, without really irrelevant packages
Diffstat (limited to 'dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedRunnable.java')
-rw-r--r-- | dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedRunnable.java | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedRunnable.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedRunnable.java new file mode 100644 index 0000000..55de6f8 --- /dev/null +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedRunnable.java @@ -0,0 +1,62 @@ +package io.dico.dicore.exceptions.checkedfunctions; + +import io.dico.dicore.exceptions.ExceptionHandler; + +/** + * checked mimic of {@link Runnable} + * + * @param <TException> + */ +@FunctionalInterface +public interface CheckedRunnable<TException extends Throwable> + extends CheckedFunctionalObject<Void, TException>, Runnable { + + /** + * The runnable action + * + * @throws TException if an exception occurs + */ + void checkedRun() throws TException; + + /** + * Unchecked version of {@link #checkedRun()} + * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)} + * + * @see #checkedRun() + * @see #resultOnError(Throwable, Object...) + */ + @Override + default void run() { + try { + checkedRun(); + } catch (Throwable ex) { + handleGenericException(ex); + } + } + + /** + * {@inheritDoc} + */ + @Override + default CheckedRunnable<TException> handleExceptionsWith(ExceptionHandler handler) { + return new CheckedRunnable<TException>() { + @Override + public void checkedRun() throws TException { + CheckedRunnable.this.checkedRun(); + } + + @Override + @SuppressWarnings("unchecked") + public Void handleGenericException(Throwable thrown, Object... args) { + handler.handleGenericException(thrown, args); + return null; + } + + @Override + public CheckedRunnable<TException> handleExceptionsWith(ExceptionHandler handler) { + return CheckedRunnable.this.handleExceptionsWith(handler); + } + }; + } + +} |