diff --git a/function/pom.xml b/function/pom.xml index aa127b6e..349e99b9 100644 --- a/function/pom.xml +++ b/function/pom.xml @@ -17,6 +17,10 @@ io.smallrye.common smallrye-common-constraint + + org.jboss.logging + jboss-logging + diff --git a/function/src/main/java/io/smallrye/common/function/Functions.java b/function/src/main/java/io/smallrye/common/function/Functions.java index e4cd0023..8058eeac 100644 --- a/function/src/main/java/io/smallrye/common/function/Functions.java +++ b/function/src/main/java/io/smallrye/common/function/Functions.java @@ -2,9 +2,25 @@ 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 io.smallrye.common.constraint.Assert; @@ -12,6 +28,9 @@ * A set of utility methods which return common functions. */ public final class Functions { + + private static final Logger LOGGER = Logger.getLogger(Functions.class); + private Functions() { } @@ -310,6 +329,701 @@ public static ExceptionBiConsumer discardin return DiscardingBiConsumer.INSTANCE; } + private static final Consumer CLOSING_CONSUMER = quiet(AutoCloseable::close, exceptionLoggingConsumer()); + + /** + * 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; + } + + 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 + * @param wrapper a runtime exception wrapper + * @return an exception consumer + */ + public static Consumer runtimeExceptionThrowingConsumer( + Function wrapper) { + return new Consumer<>() { + @Override + public void accept(E exception) { + throw wrapper.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 handler an exception handler + * @return a standard consumer + */ + public static Consumer quiet(ExceptionConsumer consumer, Consumer handler) { + return new Consumer<>() { + @SuppressWarnings("unchecked") + @Override + public void accept(T value) { + try { + consumer.accept(value); + } catch (Exception e) { + handler.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 handler an exception handler + * @return a standard binary consumer + */ + public static BiConsumer quiet(ExceptionBiConsumer consumer, + Consumer handler) { + return new BiConsumer<>() { + @SuppressWarnings("unchecked") + @Override + public void accept(T value1, U value2) { + try { + consumer.accept(value1, value2); + } catch (Exception e) { + handler.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 handler an exception handler + * @return a standard object/int consumer + */ + public static ObjIntConsumer quiet(ExceptionObjIntConsumer consumer, + Consumer handler) { + return new ObjIntConsumer<>() { + @SuppressWarnings("unchecked") + @Override + public void accept(T object, int i) { + try { + consumer.accept(object, i); + } catch (Exception e) { + handler.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 handler an exception handler + * @return a standard object/long consumer + */ + public static ObjLongConsumer quiet(ExceptionObjLongConsumer consumer, + Consumer handler) { + return new ObjLongConsumer<>() { + @SuppressWarnings("unchecked") + @Override + public void accept(T object, long i) { + try { + consumer.accept(object, i); + } catch (Exception e) { + handler.accept((E) e); + } + } + }; + } + + /** + * 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();