Supplier
does not provide a andThen
method, so chaining another Function
to the result of a Supplier
is not possible.
Is the only alternative to use a Function<Void, R>
that does not get any parameter?
In other words, if Supplier.andThen()
existed I could write:
Supplier<Exception> cleanedExceptionSupplier = exceptionSupplier.andThen(
e -> clean(e));
Since it does not exist, how can I cleanly implement cleanedExceptionSupplier
?
Instead of:
Supplier<T> supp2 = supp1.andThen(function);
(which, uses a method you've seen doesn't exist)
... you could use:
Supplier<T> supp2 = () -> function.apply(supp1.get());
Just adding my alternate solution as a candidate here
Function<Void, R> supplierAsFunction = v -> returnSomethingOfR();
supplierAsFunction.andThen(function).apply(null);
Applying null
as a parameter is rather ugly but this solution maintains functional style while using only java.util.function
classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With