Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port quiet(...) and cast(...) methods from wildfly-common #221

Merged
merged 2 commits into from
May 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Port quiet(...) consumer methods from wildfly-common.
pferraro committed May 2, 2023

Verified

This commit was signed with the committer’s verified signature.
pferraro Paul Ferraro
commit a33bdec61b88f72434ccc38365c5461e095445f6
4 changes: 4 additions & 0 deletions function/pom.xml
Original file line number Diff line number Diff line change
@@ -17,6 +17,10 @@
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-constraint</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
</dependencies>

<build>
146 changes: 146 additions & 0 deletions function/src/main/java/io/smallrye/common/function/Functions.java
Original file line number Diff line number Diff line change
@@ -4,14 +4,21 @@
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;

import io.smallrye.common.constraint.Assert;

/**
* 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 +317,145 @@ public static <T, U, E extends Exception> ExceptionBiConsumer<T, U, E> discardin
return DiscardingBiConsumer.INSTANCE;
}

private static final Consumer<AutoCloseable> 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 <T extends AutoCloseable> Consumer<T> closingConsumer() {
return (Consumer<T>) CLOSING_CONSUMER;
}

private static final Consumer<Exception> 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 <E> the exception type
* @return an exception consumer
*/
@SuppressWarnings("unchecked")
public static <E extends Exception> Consumer<E> exceptionLoggingConsumer() {
return (Consumer<E>) EXCEPTION_LOGGER;
}

/**
* Returns a consumer that wraps and throws its exception parameter as a {@link RuntimeException}.
*
* @param <E> the exception type
* @param wrapper a runtime exception wrapper
* @return an exception consumer
*/
public static <E extends Exception, RE extends RuntimeException> Consumer<E> runtimeExceptionThrowingConsumer(Function<E, RE> 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 <T> the parameter type of the consumer
* @param <E> the exception type
* @param consumer an exception consumer
* @param handler an exception handler
* @return a standard consumer
*/
public static <T, E extends Exception> Consumer<T> quiet(ExceptionConsumer<T, E> consumer, Consumer<E> 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 <T> the first parameter type of the consumer
* @param <U> the second parameter type of the consumer
* @param <E> the exception type
* @param consumer a binary exception consumer
* @param handler an exception handler
* @return a standard binary consumer
*/
public static <T, U, E extends Exception> BiConsumer<T, U> quiet(ExceptionBiConsumer<T, U, E> consumer, Consumer<E> 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 <T> the first parameter type of the consumer
* @param <E> the exception type
* @param consumer an object/int exception consumer
* @param handler an exception handler
* @return a standard object/int consumer
*/
public static <T, E extends Exception> ObjIntConsumer<T> quiet(ExceptionObjIntConsumer<T, E> consumer, Consumer<E> 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 <T> the first parameter type of the consumer
* @param <E> the exception type
* @param consumer an object/long exception consumer
* @param handler an exception handler
* @return a standard object/long consumer
*/
public static <T, E extends Exception> ObjLongConsumer<T> quiet(ExceptionObjLongConsumer<T, E> consumer, Consumer<E> 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);
}
}
};
}

static class RunnableConsumer implements Consumer<Runnable> {
static final Consumer<Runnable> INSTANCE = new RunnableConsumer();