WFCOM-71 Port Consumers.close() and Functions.identity() from org.wildfly:wildfly-clustering-common#70
Conversation
…dfly:wildfly-clustering-common
|
Since this module has exception-throwing functions, could you do something like this (off the top of my head): public static <T, E> Consumer<T> exceptionLoggingConsumer(ExceptionConsumer<T, E> consumer) {
return val -> try {
consumer.accept(val);
} catch (Exception e) {
LOGGER.warn(e, "Consumer threw an exception");
}
}And then you can do: // ...
Consumer<AutoCloseable> closingConsumer = exceptionLoggingConsumer(AutoCloseable::close); |
|
Also, this should be kept in rough parity with |
| public static <T extends R, R> Function<T, R> identityFunction() { | ||
| return (Function<T, R>) IDENTITY_FUNCTION; | ||
| } |
There was a problem hiding this comment.
This might be better generalized as something like:
public static <T, R, T2 extends T, R2 super R> Function<T2, R2> loosenBoundsOf(Function<T, R> function) {
return (Function<T2, R2>) function;
}or something like that.
There was a problem hiding this comment.
I think you mean:
public static <T, R extends R2, T2 extends T, R2> Function<T2, R2> loosenBoundsOf(Function<T, R> function) {
return (Function<T2, R2>) function;
}In which case, we should consider a different name. While this method can loosen the bounds of the return type, it can also constrain the bounds of the argument (or both).
e.g.
Function<Number, Number> function = Function.identity();
Function<Integer, Object> result = loosenBoundsOf(function);Maybe just name it "cast"?
Also, this suggests analogous methods for Supplier, Consumer, BiFunction, exception variants, etc.
|
Creating a generalized exceptionLoggingConsumer(...) makes sense, I think, and also suggests analogous methods for ExceptionBiConsumer, ExceptionObjIntConsumer, etc. |
|
Re: smallrye-common; I can do that - as soon as this passes muster. |
Add quiet(...) methods to convert from an ExceptionConsumer -> Consumer, ExceptionBiConsumer -> BiConsumer, etc. Add exception consumer methods for logging and RuntimeException wrapping.
…ion.* types and exception variants.
|
@dmlloyd Well, this got out of hand quickly... Re: closingConsumer(): I've generalized the original closingConsumer(), by generalizing your suggestion a bit further, adding a number of "quite" methods to convert exception-based consumers to standard consumers. Consumer<InputStream> streamCloser = Functions.closingConsumer();is equivalent to: Consumer<InputStream> streamCloser = Functions.quiet(InputStream::close, exceptionLoggingConsumer());If, instead, I wanted close() wrapped as an UncheckedIOException, I can do: Consumer<InputStream> streamCloser = Functions.quiet(InputStream::close, runtimeExceptionThrowingConsumer(UncheckedIOException::new));runtimeExceptionThrowingConsumer(...) can also be used to convert an ExceptionConsumer<T, RuntimeException> to a Consumer: ExceptionConsumer<Throwable, NullPointerException> consumer = Throwable::printStackTrace;
Consumer<Throwable> result = Functions.quiet(consumer, runtimeExceptionThrowingConsumer(Function.identity()));Re: identityFunction() I've created generalized function for restricting parameter/exception types and/or relaxing return types: ExceptionFunction<Number, Number, NumberFormatException> function = v -> v;
ExceptionFunction<Integer, Object, Exception> castFunction = Functions.cast(function);Let me know what you think - or if you have better suggesting for these method names. |
|
I think this looks good and these will be very useful. Thanks! |
https://issues.redhat.com/browse/WFCOM-71