From 4755be3914863e8356580525bd2c4d08a355a584 Mon Sep 17 00:00:00 2001 From: Paul Ferraro Date: Mon, 24 Apr 2023 18:23:53 -0400 Subject: [PATCH 1/3] WFCOM-71 Port Consumers.close() and Functions.identity() from org.wildfly:wildfly-clustering-common --- .../wildfly/common/function/Functions.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/org/wildfly/common/function/Functions.java b/src/main/java/org/wildfly/common/function/Functions.java index 1ee810e..8402b12 100644 --- a/src/main/java/org/wildfly/common/function/Functions.java +++ b/src/main/java/org/wildfly/common/function/Functions.java @@ -24,6 +24,7 @@ import java.util.function.Function; import java.util.function.Supplier; +import org.jboss.logging.Logger; import org.wildfly.common.Assert; /** @@ -32,6 +33,38 @@ public final class Functions { private Functions() {} + private static final Logger LOGGER = Logger.getLogger(Functions.class); + private static final Function IDENTITY_FUNCTION = Function.identity(); + + /** + * Returns a function that always returns its input argument. + * A subclass permissive variation of {@link Function#identity()}. + */ + @SuppressWarnings("unchecked") + public static Function identityFunction() { + return (Function) IDENTITY_FUNCTION; + } + + private static final Consumer CLOSING_CONSUMER = new Consumer() { + @Override + public void accept(AutoCloseable value) { + try { + value.close(); + } catch (Exception e) { + LOGGER.warn(e.getLocalizedMessage(), e); + } + } + }; + + /** + * Returns a consumer that quietly closes its argument, logging any exceptions. + * @return a closing consumer + */ + @SuppressWarnings("unchecked") + public static Consumer closingConsumer() { + return (Consumer) CLOSING_CONSUMER; + } + /** * Get the singleton consumer which accepts and runs runnable instances. * From 9c19a4160452ed94d4ff111dcccbf88991078f0e Mon Sep 17 00:00:00 2001 From: Paul Ferraro Date: Fri, 28 Apr 2023 10:20:47 -0400 Subject: [PATCH 2/3] Generalize logic for closingConsumer(). Add quiet(...) methods to convert from an ExceptionConsumer -> Consumer, ExceptionBiConsumer -> BiConsumer, etc. Add exception consumer methods for logging and RuntimeException wrapping. --- .../wildfly/common/function/Functions.java | 133 ++++++++++++++++-- 1 file changed, 123 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/wildfly/common/function/Functions.java b/src/main/java/org/wildfly/common/function/Functions.java index 8402b12..943382c 100644 --- a/src/main/java/org/wildfly/common/function/Functions.java +++ b/src/main/java/org/wildfly/common/function/Functions.java @@ -22,6 +22,8 @@ import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.ObjIntConsumer; +import java.util.function.ObjLongConsumer; import java.util.function.Supplier; import org.jboss.logging.Logger; @@ -45,16 +47,7 @@ public static Function identityFunction() { return (Function) IDENTITY_FUNCTION; } - private static final Consumer CLOSING_CONSUMER = new Consumer() { - @Override - public void accept(AutoCloseable value) { - try { - value.close(); - } catch (Exception e) { - LOGGER.warn(e.getLocalizedMessage(), e); - } - } - }; + private static final Consumer CLOSING_CONSUMER = quiet(AutoCloseable::close, exceptionLoggingConsumer()); /** * Returns a consumer that quietly closes its argument, logging any exceptions. @@ -65,6 +58,126 @@ public static Consumer closingConsumer() { return (Consumer) CLOSING_CONSUMER; } + private static final Consumer EXCEPTION_LOGGER = new Consumer() { + @Override + public void accept(Exception e) { + LOGGER.warn(e.getLocalizedMessage(), e); + } + }; + + /** + * Returns a consumer that logs its exception parameter as a warning. + * @param the exception type + * @return an exception consumer + */ + @SuppressWarnings("unchecked") + public static Consumer exceptionLoggingConsumer() { + return (Consumer) EXCEPTION_LOGGER; + } + + /** + * Returns a consumer that wraps and throws its exception parameter as a {@link RuntimeException}. + * @param the exception type + * @return an exception consumer + */ + public static Consumer runtimeExceptionThrowingConsumer(Function runtimeExceptionWrapper) { + return new Consumer() { + @Override + public void accept(E exception) { + throw runtimeExceptionWrapper.apply(exception); + } + }; + } + + /** + * Converts an {@link ExceptionConsumer} to a standard {@link Consumer} using the specified exception handler. + * @param the parameter type of the consumer + * @param the exception type + * @param consumer an exception consumer + * @param exceptionHandler an exception handler + * @return a standard consumer + */ + public static Consumer quiet(ExceptionConsumer consumer, Consumer exceptionHandler) { + return new Consumer() { + @SuppressWarnings("unchecked") + @Override + public void accept(T value) { + try { + consumer.accept(value); + } catch (Exception e) { + exceptionHandler.accept((E) e); + } + } + }; + } + + /** + * Converts an {@link ExceptionBiConsumer} to a standard {@link BiConsumer} using the specified exception handler. + * @param the first parameter type of the consumer + * @param the second parameter type of the consumer + * @param the exception type + * @param consumer a binary exception consumer + * @param exceptionHandler an exception handler + * @return a standard binary consumer + */ + public static BiConsumer quiet(ExceptionBiConsumer consumer, Consumer exceptionHandler) { + return new BiConsumer() { + @SuppressWarnings("unchecked") + @Override + public void accept(T value1, U value2) { + try { + consumer.accept(value1, value2); + } catch (Exception e) { + exceptionHandler.accept((E) e); + } + } + }; + } + + /** + * Converts an {@link ExceptionObjIntConsumer} to a standard {@link ObjIntConsumer} using the specified exception handler. + * @param the first parameter type of the consumer + * @param the exception type + * @param consumer an object/int exception consumer + * @param exceptionHandler an exception handler + * @return a standard object/int consumer + */ + public static ObjIntConsumer quiet(ExceptionObjIntConsumer consumer, Consumer exceptionHandler) { + return new ObjIntConsumer() { + @SuppressWarnings("unchecked") + @Override + public void accept(T object, int i) { + try { + consumer.accept(object, i); + } catch (Exception e) { + exceptionHandler.accept((E) e); + } + } + }; + } + + /** + * Converts an {@link ExceptionObjLongConsumer} to a standard {@link ObjLongConsumer} using the specified exception handler. + * @param the first parameter type of the consumer + * @param the exception type + * @param consumer an object/long exception consumer + * @param exceptionHandler an exception handler + * @return a standard object/long consumer + */ + public static ObjLongConsumer quiet(ExceptionObjLongConsumer consumer, Consumer exceptionHandler) { + return new ObjLongConsumer() { + @SuppressWarnings("unchecked") + @Override + public void accept(T object, long i) { + try { + consumer.accept(object, i); + } catch (Exception e) { + exceptionHandler.accept((E) e); + } + } + }; + } + /** * Get the singleton consumer which accepts and runs runnable instances. * From 2c1b500069ad3610647651fdeb9ff820bc59d6bf Mon Sep 17 00:00:00 2001 From: Paul Ferraro Date: Fri, 28 Apr 2023 10:34:48 -0400 Subject: [PATCH 3/3] Generalize identityFunction() as cast methods for all java.util.function.* types and exception variants. --- .../wildfly/common/function/Functions.java | 490 +++++++++++++++++- 1 file changed, 480 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/wildfly/common/function/Functions.java b/src/main/java/org/wildfly/common/function/Functions.java index 943382c..afc7cd8 100644 --- a/src/main/java/org/wildfly/common/function/Functions.java +++ b/src/main/java/org/wildfly/common/function/Functions.java @@ -20,11 +20,23 @@ import java.util.function.BiConsumer; import java.util.function.BiFunction; +import java.util.function.BiPredicate; import java.util.function.Consumer; +import java.util.function.DoubleFunction; import java.util.function.Function; +import java.util.function.IntFunction; +import java.util.function.LongFunction; +import java.util.function.ObjDoubleConsumer; import java.util.function.ObjIntConsumer; import java.util.function.ObjLongConsumer; +import java.util.function.Predicate; import java.util.function.Supplier; +import java.util.function.ToDoubleBiFunction; +import java.util.function.ToDoubleFunction; +import java.util.function.ToIntBiFunction; +import java.util.function.ToIntFunction; +import java.util.function.ToLongBiFunction; +import java.util.function.ToLongFunction; import org.jboss.logging.Logger; import org.wildfly.common.Assert; @@ -36,16 +48,6 @@ public final class Functions { private Functions() {} private static final Logger LOGGER = Logger.getLogger(Functions.class); - private static final Function IDENTITY_FUNCTION = Function.identity(); - - /** - * Returns a function that always returns its input argument. - * A subclass permissive variation of {@link Function#identity()}. - */ - @SuppressWarnings("unchecked") - public static Function identityFunction() { - return (Function) IDENTITY_FUNCTION; - } private static final Consumer CLOSING_CONSUMER = quiet(AutoCloseable::close, exceptionLoggingConsumer()); @@ -471,6 +473,474 @@ public static ExceptionBiConsumer discardin return DiscardingBiConsumer.INSTANCE; } + /** + * Returns a {@link Consumer} with identical behavior to the specified {@link Consumer} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static Consumer cast(Consumer consumer) { + return (Consumer) consumer; + } + + /** + * Returns a {@link Predicate} with identical behavior to the specified {@link Predicate} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param predicate a predicate + * @return a functionally equivalent predicate + */ + @SuppressWarnings("unchecked") + public static Predicate cast(Predicate predicate) { + return (Predicate) predicate; + } + + /** + * Returns a {@link Supplier} with identical behavior to the specified {@link Supplier} but with relaxed return type. + * @param the return type + * @param the relaxed return type + * @param supplier a supplier + * @return a functionally equivalent supplier + */ + @SuppressWarnings("unchecked") + public static Supplier cast(Supplier supplier) { + return (Supplier) supplier; + } + + /** + * Returns a {@link Function} with identical behavior to the specified {@link Function} but with restricted parameter type and relaxed return type. + * @param the parameter type + * @param the return type + * @param the restricted parameter type + * @param the relaxed return type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static Function cast(Function function) { + return (Function) function; + } + + /** + * Returns a {@link DoubleFunction} with identical behavior to the specified {@link DoubleFunction} but with relaxed return type. + * @param the return type + * @param the relaxed return type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static DoubleFunction cast(DoubleFunction function) { + return (DoubleFunction) function; + } + + /** + * Returns a {@link IntFunction} with identical behavior to the specified {@link IntFunction} but with relaxed return type. + * @param the return type + * @param the relaxed return type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static IntFunction cast(IntFunction function) { + return (IntFunction) function; + } + + /** + * Returns a {@link LongFunction} with identical behavior to the specified {@link LongFunction} but with relaxed return type. + * @param the return type + * @param the relaxed return type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static LongFunction cast(LongFunction function) { + return (LongFunction) function; + } + + /** + * Returns a {@link ToDoubleFunction} with identical behavior to the specified {@link ToDoubleFunction} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ToDoubleFunction cast(ToDoubleFunction function) { + return (ToDoubleFunction) function; + } + + /** + * Returns a {@link ToIntFunction} with identical behavior to the specified {@link ToIntFunction} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ToIntFunction cast(ToIntFunction function) { + return (ToIntFunction) function; + } + + /** + * Returns a {@link ToLongFunction} with identical behavior to the specified {@link ToLongFunction} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ToLongFunction cast(ToLongFunction function) { + return (ToLongFunction) function; + } + + /** + * Returns a {@link BiConsumer} with identical behavior to the specified {@link BiConsumer} but with restricted parameter types. + * @param the first parameter type + * @param the second parameter type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static BiConsumer cast(BiConsumer consumer) { + return (BiConsumer) consumer; + } + + /** + * Returns a {@link ObjDoubleConsumer} with identical behavior to the specified {@link ObjDoubleConsumer} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ObjDoubleConsumer cast(ObjDoubleConsumer consumer) { + return (ObjDoubleConsumer) consumer; + } + + /** + * Returns a {@link ObjIntConsumer} with identical behavior to the specified {@link ObjIntConsumer} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ObjIntConsumer cast(ObjIntConsumer consumer) { + return (ObjIntConsumer) consumer; + } + + /** + * Returns a {@link ObjLongConsumer} with identical behavior to the specified {@link ObjLongConsumer} but with restricted parameter type. + * @param the parameter type + * @param the restricted parameter type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ObjLongConsumer cast(ObjLongConsumer consumer) { + return (ObjLongConsumer) consumer; + } + + /** + * Returns a {@link BiPredicate} with identical behavior to the specified {@link BiPredicate} but with restricted parameter types. + * @param the first parameter type + * @param the second parameter type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param predicate a predicate + * @return a functionally equivalent predicate + */ + @SuppressWarnings("unchecked") + public static BiPredicate cast(BiPredicate predicate) { + return (BiPredicate) predicate; + } + + /** + * Returns a {@link BiFunction} with identical behavior to the specified {@link BiFunction} but with restricted parameter types and relaxed return type. + * @param the first parameter type + * @param the second parameter type + * @param the return type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param the relaxed return type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static BiFunction cast(BiFunction function) { + return (BiFunction) function; + } + + /** + * Returns a {@link ToDoubleBiFunction} with identical behavior to the specified {@link ToDoubleBiFunction} but with restricted parameter types. + * @param the first parameter type + * @param the second parameter type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ToDoubleBiFunction cast(ToDoubleBiFunction function) { + return (ToDoubleBiFunction) function; + } + + /** + * Returns a {@link ToIntBiFunction} with identical behavior to the specified {@link ToIntBiFunction} but with restricted parameter types. + * @param the first parameter type + * @param the second parameter type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ToIntBiFunction cast(ToIntBiFunction function) { + return (ToIntBiFunction) function; + } + + /** + * Returns a {@link ToLongBiFunction} with identical behavior to the specified {@link ToLongBiFunction} but with restricted parameter types. + * @param the first parameter type + * @param the second parameter type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ToLongBiFunction cast(ToLongBiFunction function) { + return (ToLongBiFunction) function; + } + + /** + * Returns a {@link ExceptionConsumer} with identical behavior to the specified {@link ExceptionConsumer} but with restricted parameter type and relaxed exception type. + * @param the parameter type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed exception type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ExceptionConsumer cast(ExceptionConsumer consumer) { + return (ExceptionConsumer) consumer; + } + + /** + * Returns a {@link ExceptionPredicate} with identical behavior to the specified {@link ExceptionPredicate} but with restricted parameter type and relaxed exception type. + * @param the parameter type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed exception type + * @param predicate a predicate + * @return a functionally equivalent predicate + */ + @SuppressWarnings("unchecked") + public static ExceptionPredicate cast(ExceptionPredicate consumer) { + return (ExceptionPredicate) consumer; + } + + /** + * Returns a {@link ExceptionSupplier} with identical behavior to the specified {@link ExceptionSupplier} but with relaxed return type and relaxed exception type. + * @param the return type + * @param the exception type + * @param the relaxed return type + * @param the relaxed exception type + * @param supplier a supplier + * @return a functionally equivalent supplier + */ + @SuppressWarnings("unchecked") + public static ExceptionSupplier cast(ExceptionSupplier supplier) { + return (ExceptionSupplier) supplier; + } + + /** + * Returns a {@link ExceptionFunction} with identical behavior to the specified {@link ExceptionFunction} but with restricted parameter type, relaxed return type, and relaxed exception type. + * @param the parameter type + * @param the return type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed return type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionFunction cast(ExceptionFunction function) { + return (ExceptionFunction) function; + } + + /** + * Returns a {@link ExceptionIntFunction} with identical behavior to the specified {@link ExceptionFunction} but with relaxed return type and relaxed exception type. + * @param the return type + * @param the exception type + * @param the relaxed return type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionIntFunction cast(ExceptionIntFunction function) { + return (ExceptionIntFunction) function; + } + + /** + * Returns a {@link ExceptionLongFunction} with identical behavior to the specified {@link ExceptionLongFunction} but with relaxed return type and relaxed exception type. + * @param the return type + * @param the exception type + * @param the relaxed return type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionLongFunction cast(ExceptionLongFunction function) { + return (ExceptionLongFunction) function; + } + + /** + * Returns a {@link ExceptionToIntFunction} with identical behavior to the specified {@link ExceptionToIntFunction} but with restricted parameter type and relaxed exception type. + * @param the parameter type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionToIntFunction cast(ExceptionToIntFunction function) { + return (ExceptionToIntFunction) function; + } + + /** + * Returns a {@link ExceptionToLongFunction} with identical behavior to the specified {@link ExceptionToLongFunction} but with restricted parameter type and relaxed exception type. + * @param the parameter type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionToLongFunction cast(ExceptionToLongFunction function) { + return (ExceptionToLongFunction) function; + } + + /** + * Returns a {@link ExceptionBiConsumer} with identical behavior to the specified {@link ExceptionBiConsumer} but with restricted parameter types and relaxed exception type. + * @param the first parameter type + * @param the second parameter type + * @param the exception type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param the relaxed exception type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ExceptionBiConsumer cast(ExceptionBiConsumer consumer) { + return (ExceptionBiConsumer) consumer; + } + + /** + * Returns a {@link ExceptionObjIntConsumer} with identical behavior to the specified {@link ExceptionObjIntConsumer} but with restricted parameter type and relaxed exception type. + * @param the parameter type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed exception type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ExceptionObjIntConsumer cast(ExceptionObjIntConsumer consumer) { + return (ExceptionObjIntConsumer) consumer; + } + + /** + * Returns a {@link ExceptionObjLongConsumer} with identical behavior to the specified {@link ExceptionObjLongConsumer} but with restricted parameter type and relaxed exception type. + * @param the parameter type + * @param the exception type + * @param the restricted parameter type + * @param the relaxed exception type + * @param consumer a consumer + * @return a functionally equivalent consumer + */ + @SuppressWarnings("unchecked") + public static ExceptionObjLongConsumer cast(ExceptionObjLongConsumer consumer) { + return (ExceptionObjLongConsumer) consumer; + } + + /** + * Returns a {@link ExceptionBiPredicate} with identical behavior to the specified {@link ExceptionBiPredicate} but with restricted parameter types and relaxed exception type. + * @param the first parameter type + * @param the second parameter type + * @param the exception type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param the relaxed exception type + * @param predicate a predicate + * @return a functionally equivalent predicate + */ + @SuppressWarnings("unchecked") + public static ExceptionBiPredicate cast(ExceptionBiPredicate predicate) { + return (ExceptionBiPredicate) predicate; + } + + /** + * Returns a {@link ExceptionBiFunction} with identical behavior to the specified {@link ExceptionBiFunction} but with restricted parameter types, relaxed return type, and relaxed exception type. + * @param the first parameter type + * @param the second parameter type + * @param the return type + * @param the exception type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param the relaxed return type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionBiFunction cast(ExceptionBiFunction function) { + return (ExceptionBiFunction) function; + } + + /** + * Returns a {@link ExceptionToIntBiFunction} with identical behavior to the specified {@link ExceptionToIntBiFunction} but with restricted parameter types and relaxed exception type. + * @param the first parameter type + * @param the second parameter type + * @param the exception type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionToIntBiFunction cast(ExceptionToIntBiFunction function) { + return (ExceptionToIntBiFunction) function; + } + + /** + * Returns a {@link ExceptionToLongBiFunction} with identical behavior to the specified {@link ExceptionToLongBiFunction} but with restricted parameter types and relaxed exception type. + * @param the first parameter type + * @param the second parameter type + * @param the exception type + * @param the restricted first parameter type + * @param the restricted second parameter type + * @param the relaxed exception type + * @param function a function + * @return a functionally equivalent function + */ + @SuppressWarnings("unchecked") + public static ExceptionToLongBiFunction cast(ExceptionToLongBiFunction function) { + return (ExceptionToLongBiFunction) function; + } + static class RunnableConsumer implements Consumer { static final Consumer INSTANCE = new RunnableConsumer();