From 5457dd918b7b4e9f774f03e45f47b10c045f9e82 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 6 Aug 2022 22:18:43 +0200 Subject: [PATCH] Some new exception enabled functional interfaces --- .../fr/pandacube/lib/util/ThrowableUtil.java | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java index 91cc93b..b4a92f0 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/ThrowableUtil.java @@ -5,6 +5,7 @@ import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; +import java.util.function.BiConsumer; /** * Utility class to easily manipulate {@link Throwable}s. @@ -101,7 +102,7 @@ public class ThrowableUtil { * A supplier that can possibly throw a checked exception. */ @FunctionalInterface - public interface SupplierException { + public interface SupplierException { // TODO make exception type generic /** * Gets a result. * @return a result. @@ -116,7 +117,7 @@ public class ThrowableUtil { * A runnable that can possibly throw a checked exception. */ @FunctionalInterface - public interface RunnableException { + public interface RunnableException { // TODO make exception type generic /** * Run any code implemented. * @throws Exception if implementation failed to run. @@ -126,6 +127,53 @@ public class ThrowableUtil { + /** + * A predicate that can possibly throw a checked exception. + */ + @FunctionalInterface + public interface PredicateException { + /** + * Test the predicate on the specified value. + * @param value the value to test against. + * @return the result of the test. + * @throws E if implementation failed to run. + */ + boolean test(T value) throws E; + } + + + + /** + * A function that can possibly throw a checked exception. + */ + public interface ToIntBiFunctionException { + /** + * Run on the specified parameters to return an int value. + * @param t the first parameter of the function. + * @param u the second parameter of the function. + * @return the result of the function. + * @throws E if the function fails. + */ + int applyAsInt(T t, U u) throws E; + } + + + + /** + * A consumer that can possibly throw a checked exception. + */ + public interface BiConsumer { + /** + * Run the consumer on the specified parameters. + * @param t the first parameter of the consumer. + * @param u the second parameter of the consumer. + * @throws E if the function fails. + */ + void accept(T t, U u) throws E; + } + + + private static RuntimeException uncheck(Throwable t, boolean convertReflectionExceptionToError) { if (t instanceof Error er) {