diff options
Diffstat (limited to 'dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions')
7 files changed, 503 insertions, 503 deletions
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiConsumer.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiConsumer.java index badf05b..bfcafe8 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiConsumer.java +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedBiConsumer.java @@ -1,69 +1,69 @@ -package io.dico.dicore.exceptions.checkedfunctions; - -import io.dico.dicore.exceptions.ExceptionHandler; - -import java.util.function.BiConsumer; - -/** - * checked mimic of {@link BiConsumer} - * - * @param <TParam1> - * @param <TParam2> - * @param <TException> - */ -@FunctionalInterface -public interface CheckedBiConsumer<TParam1, TParam2, TException extends Throwable> - extends CheckedFunctionalObject<Void, TException>, BiConsumer<TParam1, TParam2> { - - /** - * The consuming action - * - * @param t the first argument to consume - * @param u the second argument to consume - * @throws TException if an exception occurs - */ - void checkedAccept(TParam1 t, TParam2 u) throws TException; - - /** - * unchecked wrapper for {@link #checkedAccept(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 - * @see #checkedAccept(Object, Object) - * @see #resultOnError(Throwable, Object...) - */ - @Override - default void accept(TParam1 t, TParam2 u) { - try { - checkedAccept(t, u); - } catch (Throwable ex) { - handleGenericException(ex, t, u); - } - } - - /** - * {@inheritDoc} - */ - @Override - default CheckedBiConsumer<TParam1, TParam2, TException> handleExceptionsWith(ExceptionHandler handler) { - return new CheckedBiConsumer<TParam1, TParam2, TException>() { - @Override - public void checkedAccept(TParam1 t, TParam2 u) throws TException { - CheckedBiConsumer.this.checkedAccept(t, u); - } - - @Override - public Void handleGenericException(Throwable thrown, Object... args) { - handler.handleGenericException(thrown, args); - return null; - } - - @Override - public CheckedBiConsumer<TParam1, TParam2, TException> handleExceptionsWith(ExceptionHandler handler) { - return CheckedBiConsumer.this.handleExceptionsWith(handler); - } - }; - } - -} +package io.dico.dicore.exceptions.checkedfunctions;
+
+import io.dico.dicore.exceptions.ExceptionHandler;
+
+import java.util.function.BiConsumer;
+
+/**
+ * checked mimic of {@link BiConsumer}
+ *
+ * @param <TParam1>
+ * @param <TParam2>
+ * @param <TException>
+ */
+@FunctionalInterface
+public interface CheckedBiConsumer<TParam1, TParam2, TException extends Throwable>
+ extends CheckedFunctionalObject<Void, TException>, BiConsumer<TParam1, TParam2> {
+
+ /**
+ * The consuming action
+ *
+ * @param t the first argument to consume
+ * @param u the second argument to consume
+ * @throws TException if an exception occurs
+ */
+ void checkedAccept(TParam1 t, TParam2 u) throws TException;
+
+ /**
+ * unchecked wrapper for {@link #checkedAccept(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
+ * @see #checkedAccept(Object, Object)
+ * @see #resultOnError(Throwable, Object...)
+ */
+ @Override
+ default void accept(TParam1 t, TParam2 u) {
+ try {
+ checkedAccept(t, u);
+ } catch (Throwable ex) {
+ handleGenericException(ex, t, u);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ default CheckedBiConsumer<TParam1, TParam2, TException> handleExceptionsWith(ExceptionHandler handler) {
+ return new CheckedBiConsumer<TParam1, TParam2, TException>() {
+ @Override
+ public void checkedAccept(TParam1 t, TParam2 u) throws TException {
+ CheckedBiConsumer.this.checkedAccept(t, u);
+ }
+
+ @Override
+ public Void handleGenericException(Throwable thrown, Object... args) {
+ handler.handleGenericException(thrown, args);
+ return null;
+ }
+
+ @Override
+ public CheckedBiConsumer<TParam1, TParam2, TException> handleExceptionsWith(ExceptionHandler handler) {
+ return CheckedBiConsumer.this.handleExceptionsWith(handler);
+ }
+ };
+ }
+
+}
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 index 9b39325..11fca3f 100644 --- 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 @@ -1,76 +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); - } - }; - } -} +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);
+ }
+ };
+ }
+}
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedConsumer.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedConsumer.java index fa48ae8..abe6d53 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedConsumer.java +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedConsumer.java @@ -1,67 +1,67 @@ -package io.dico.dicore.exceptions.checkedfunctions; - -import io.dico.dicore.exceptions.ExceptionHandler; - -import java.util.function.Consumer; - -/** - * checked mimic of {@link Consumer} - * - * @param <TParam> - * @param <TException> - */ -@FunctionalInterface -public interface CheckedConsumer<TParam, TException extends Throwable> - extends CheckedFunctionalObject<Void, TException>, Consumer<TParam> { - - /** - * The consuming action - * - * @param t the argument to consume - * @throws TException if an error occurs - */ - void checkedAccept(TParam t) throws TException; - - /** - * Unchecked version of {@link #checkedAccept(Object)} - * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)} - * - * @param t the argument to consume - * @see #checkedAccept(Object) - * @see #resultOnError(Throwable, Object...) - */ - @Override - default void accept(TParam t) { - try { - checkedAccept(t); - } catch (Throwable ex) { - handleGenericException(ex, t); - } - } - - /** - * {@inheritDoc} - */ - @Override - default CheckedConsumer<TParam, TException> handleExceptionsWith(ExceptionHandler handler) { - return new CheckedConsumer<TParam, TException>() { - @Override - public void checkedAccept(TParam t) throws TException { - CheckedConsumer.this.checkedAccept(t); - } - - @Override - @SuppressWarnings("unchecked") - public Void handleGenericException(Throwable thrown, Object... args) { - handler.handleGenericException(thrown, args); - return null; - } - - @Override - public CheckedConsumer<TParam, TException> handleExceptionsWith(ExceptionHandler handler) { - return CheckedConsumer.this.handleExceptionsWith(handler); - } - }; - } - -} +package io.dico.dicore.exceptions.checkedfunctions;
+
+import io.dico.dicore.exceptions.ExceptionHandler;
+
+import java.util.function.Consumer;
+
+/**
+ * checked mimic of {@link Consumer}
+ *
+ * @param <TParam>
+ * @param <TException>
+ */
+@FunctionalInterface
+public interface CheckedConsumer<TParam, TException extends Throwable>
+ extends CheckedFunctionalObject<Void, TException>, Consumer<TParam> {
+
+ /**
+ * The consuming action
+ *
+ * @param t the argument to consume
+ * @throws TException if an error occurs
+ */
+ void checkedAccept(TParam t) throws TException;
+
+ /**
+ * Unchecked version of {@link #checkedAccept(Object)}
+ * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)}
+ *
+ * @param t the argument to consume
+ * @see #checkedAccept(Object)
+ * @see #resultOnError(Throwable, Object...)
+ */
+ @Override
+ default void accept(TParam t) {
+ try {
+ checkedAccept(t);
+ } catch (Throwable ex) {
+ handleGenericException(ex, t);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ default CheckedConsumer<TParam, TException> handleExceptionsWith(ExceptionHandler handler) {
+ return new CheckedConsumer<TParam, TException>() {
+ @Override
+ public void checkedAccept(TParam t) throws TException {
+ CheckedConsumer.this.checkedAccept(t);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Void handleGenericException(Throwable thrown, Object... args) {
+ handler.handleGenericException(thrown, args);
+ return null;
+ }
+
+ @Override
+ public CheckedConsumer<TParam, TException> handleExceptionsWith(ExceptionHandler handler) {
+ return CheckedConsumer.this.handleExceptionsWith(handler);
+ }
+ };
+ }
+
+}
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 index 9ff806a..48ac207 100644 --- 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 @@ -1,74 +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); - } - }; - } - -} +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);
+ }
+ };
+ }
+
+}
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunctionalObject.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunctionalObject.java index e84b33d..d9d7a53 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunctionalObject.java +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedFunctionalObject.java @@ -1,85 +1,85 @@ -package io.dico.dicore.exceptions.checkedfunctions; - -import io.dico.dicore.exceptions.ExceptionHandler; - -/** - * Base interface for all checked functional interfaces - * Most subinterfaces will mimic interfaces in the package {@link java.util.function} - * <p> - * Checked functional interfaces are functions with throws declarations. - * The name comes from the fact that they can throw <b>checked</b> exceptions. - * <p> - * They extend their non-checked counterparts, whose methods are implemented by - * returning the result of {@link #resultOnError(TException, Object...)} when an exception is thrown - * <p> - * Made public to allow more specialized checked functional interfaces to subclass it. - * Making primitive versions shouldn't provide a significant performance increase because we're checking for exceptions, - * the performance impact of which is probably a few magnitudes larger. Don't quote me on this. - * - * @param <TResult> The return type of this functional interface's method - * @param <TException> The type of exception that might be thrown - */ -public interface CheckedFunctionalObject<TResult, TException extends Throwable> extends ExceptionHandler { - - /** - * {@inheritDoc} - * - * @param ex The exception to be handled - */ - @Override - default void handle(Throwable ex) { - handleGenericException(ex); - } - - /** - * {@inheritDoc} - * <p> - * Method to handle exceptions thrown by the default implementations of subinterfaces. - * Since you can't catch a type parameter as exception, this code is in place to take care of it. - * <p> - * If the thrown exception is not a TException, an unchecked version is thrown by calling this method. - * - * @param thrown The thrown exception - * @param args the arguments supplied to the method that threw the exception, if any. These are not guaranteed to be given if parameters are present. - * @return The result computed by {@link #resultOnError(Throwable, Object...)} - * @see #resultOnError(Throwable, Object...) - */ - @Override - @SuppressWarnings("unchecked") - default TResult handleGenericException(Throwable thrown, Object... args) { - - // check if the throwable is a TException - TException castedException; - try { - castedException = (TException) thrown; - } catch (ClassCastException ex) { - // if not, throw an unchecked version of it - ExceptionHandler.UNCHECKED.handleGenericException(thrown); - // this code is never reached. - return null; - } - - // if it is a TException, use resultOnError to compute result. - return resultOnError(castedException, args); - } - - /** - * This method handles the exceptions thrown by the checked method of this object, when called by its unchecked wrapper. - * - * @param ex The exception thrown - * @param args The (typed) arguments passed, if any - * @return The result to return, if any - */ - default TResult resultOnError(TException ex, Object... args) { - return null; - } - - /** - * Creates a new functional object that uses the given exception handler to handle exceptions. - * - * @param handler The handler to handle exceptions - * @return a new functional object that uses the given exception handler to handle exceptions - */ - CheckedFunctionalObject<TResult, TException> handleExceptionsWith(ExceptionHandler handler); - -} +package io.dico.dicore.exceptions.checkedfunctions;
+
+import io.dico.dicore.exceptions.ExceptionHandler;
+
+/**
+ * Base interface for all checked functional interfaces
+ * Most subinterfaces will mimic interfaces in the package {@link java.util.function}
+ * <p>
+ * Checked functional interfaces are functions with throws declarations.
+ * The name comes from the fact that they can throw <b>checked</b> exceptions.
+ * <p>
+ * They extend their non-checked counterparts, whose methods are implemented by
+ * returning the result of {@link #resultOnError(TException, Object...)} when an exception is thrown
+ * <p>
+ * Made public to allow more specialized checked functional interfaces to subclass it.
+ * Making primitive versions shouldn't provide a significant performance increase because we're checking for exceptions,
+ * the performance impact of which is probably a few magnitudes larger. Don't quote me on this.
+ *
+ * @param <TResult> The return type of this functional interface's method
+ * @param <TException> The type of exception that might be thrown
+ */
+public interface CheckedFunctionalObject<TResult, TException extends Throwable> extends ExceptionHandler {
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param ex The exception to be handled
+ */
+ @Override
+ default void handle(Throwable ex) {
+ handleGenericException(ex);
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * Method to handle exceptions thrown by the default implementations of subinterfaces.
+ * Since you can't catch a type parameter as exception, this code is in place to take care of it.
+ * <p>
+ * If the thrown exception is not a TException, an unchecked version is thrown by calling this method.
+ *
+ * @param thrown The thrown exception
+ * @param args the arguments supplied to the method that threw the exception, if any. These are not guaranteed to be given if parameters are present.
+ * @return The result computed by {@link #resultOnError(Throwable, Object...)}
+ * @see #resultOnError(Throwable, Object...)
+ */
+ @Override
+ @SuppressWarnings("unchecked")
+ default TResult handleGenericException(Throwable thrown, Object... args) {
+
+ // check if the throwable is a TException
+ TException castedException;
+ try {
+ castedException = (TException) thrown;
+ } catch (ClassCastException ex) {
+ // if not, throw an unchecked version of it
+ ExceptionHandler.UNCHECKED.handleGenericException(thrown);
+ // this code is never reached.
+ return null;
+ }
+
+ // if it is a TException, use resultOnError to compute result.
+ return resultOnError(castedException, args);
+ }
+
+ /**
+ * This method handles the exceptions thrown by the checked method of this object, when called by its unchecked wrapper.
+ *
+ * @param ex The exception thrown
+ * @param args The (typed) arguments passed, if any
+ * @return The result to return, if any
+ */
+ default TResult resultOnError(TException ex, Object... args) {
+ return null;
+ }
+
+ /**
+ * Creates a new functional object that uses the given exception handler to handle exceptions.
+ *
+ * @param handler The handler to handle exceptions
+ * @return a new functional object that uses the given exception handler to handle exceptions
+ */
+ CheckedFunctionalObject<TResult, TException> handleExceptionsWith(ExceptionHandler handler);
+
+}
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 index 55de6f8..110c988 100644 --- 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 @@ -1,62 +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); - } - }; - } - -} +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);
+ }
+ };
+ }
+
+}
diff --git a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedSupplier.java b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedSupplier.java index dec2e7e..7820428 100644 --- a/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedSupplier.java +++ b/dicore3/core/src/main/java/io/dico/dicore/exceptions/checkedfunctions/CheckedSupplier.java @@ -1,70 +1,70 @@ -package io.dico.dicore.exceptions.checkedfunctions; - -import io.dico.dicore.exceptions.ExceptionHandler; - -import java.util.function.Supplier; - -/** - * checked mimic of {@link Supplier} - * - * @param <TResult> - * @param <TException> - */ -@FunctionalInterface -public interface CheckedSupplier<TResult, TException extends Throwable> - extends CheckedFunctionalObject<TResult, TException>, Supplier<TResult> { - - /** - * The computation - * - * @return the result of this computation - * @throws TException if an error occurs - */ - TResult checkedGet() throws TException; - - /** - * Unchecked version of {@link #checkedGet()} - * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)} - * - * @return the result of this computation - * @see #checkedGet() - * @see #resultOnError(Throwable, Object...) - */ - @Override - default TResult get() { - try { - return checkedGet(); - } catch (Throwable ex) { - return handleGenericException(ex); - } - } - - /** - * {@inheritDoc} - */ - @Override - default CheckedSupplier<TResult, TException> handleExceptionsWith(ExceptionHandler handler) { - return new CheckedSupplier<TResult, TException>() { - @Override - public TResult checkedGet() throws TException { - return CheckedSupplier.this.checkedGet(); - } - - @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 CheckedSupplier<TResult, TException> handleExceptionsWith(ExceptionHandler handler) { - return CheckedSupplier.this.handleExceptionsWith(handler); - } - }; - } -} +package io.dico.dicore.exceptions.checkedfunctions;
+
+import io.dico.dicore.exceptions.ExceptionHandler;
+
+import java.util.function.Supplier;
+
+/**
+ * checked mimic of {@link Supplier}
+ *
+ * @param <TResult>
+ * @param <TException>
+ */
+@FunctionalInterface
+public interface CheckedSupplier<TResult, TException extends Throwable>
+ extends CheckedFunctionalObject<TResult, TException>, Supplier<TResult> {
+
+ /**
+ * The computation
+ *
+ * @return the result of this computation
+ * @throws TException if an error occurs
+ */
+ TResult checkedGet() throws TException;
+
+ /**
+ * Unchecked version of {@link #checkedGet()}
+ * If a {@link TException} occurs, an unchecked one might be thrown by {@link #resultOnError(Throwable, Object...)}
+ *
+ * @return the result of this computation
+ * @see #checkedGet()
+ * @see #resultOnError(Throwable, Object...)
+ */
+ @Override
+ default TResult get() {
+ try {
+ return checkedGet();
+ } catch (Throwable ex) {
+ return handleGenericException(ex);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ default CheckedSupplier<TResult, TException> handleExceptionsWith(ExceptionHandler handler) {
+ return new CheckedSupplier<TResult, TException>() {
+ @Override
+ public TResult checkedGet() throws TException {
+ return CheckedSupplier.this.checkedGet();
+ }
+
+ @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 CheckedSupplier<TResult, TException> handleExceptionsWith(ExceptionHandler handler) {
+ return CheckedSupplier.this.handleExceptionsWith(handler);
+ }
+ };
+ }
+}
|