RFD: Consumer.empty()

Eirik Bjørsnøs eirbjo at gmail.com
Mon Feb 20 18:24:55 UTC 2023


Hi,

So I found myself writing some code which sets up a j.u.s.Stream processing
pipeline where the stream can optionally be traced/logged via a peeking
Consumer.

The default should be not to trace, in which case I initially set the
Consumer to null. But then I need to check that trace != null when setting
up the stream. This felt awkward..

Since related code was using Function.identity() as a "no mapping
configured" default, I went looking for Consumer.empty(), only to learn
that no such thing exists.

I can of course just do () -> {}, but I think Consumer.empty() conveys the
purpose a bit more clearly..

Is there some thought behind this not existing? Would it be worth adding?


/**
 * Returns a consumer which performs no operation on its input argument
 *
 * @param <T> the type of the input argument
 * @return a Consumer which performs no operation on its input argument
 */
static <T> Consumer<T> empty() {
    return t -> {};
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/core-libs-dev/attachments/20230220/5ec07846/attachment.htm>


More information about the core-libs-dev mailing list