T
- The type of CompletionStage
result.public final class FutureValue<T> extends Object
CompletionStage
instances inside streams.
In streams, applying operations such as Stream.filter(Predicate)
immediately alter the state of the streams. When using streams of
CompletionStage
, the result is not available when the stream operation is applied. This class is meant to help with that issue as far as
possible. It does so by providing the most essential functionality using asynchronous mapping:
Stream.filter(Predicate)
,
Stream.map(Function)
should be used in combination with filter(Predicate)
.Stream.map(Function)
and map(Function)
.Stream.collect(Collector)
and collect(Collector)
.Stream.forEach(Consumer)
and run(Consumer)
.
Most other operations rely on the internal state of the stream to change. These operations should not be used after the first mapping to
wrap(CompletionStage)
or wrap(CompletableFuture)
. The only stream operations that can safely be used are
Stream.map(Function)
(both for mapping and filtering), Stream.forEach(Consumer)
and Stream.collect(Collector)
. For other
methods, if possible, use Stream.collect(Collector)
as replacement. For instance, the following can be used as replacement for
Stream.reduce(BinaryOperator)
using Collectors.reducing(BinaryOperator)
:
CompletableFuture<Optional<Integer>> result = stream
.map(FutureValue::wrap)
.map(FutureValue.filter(i -> (i & 1) == 0))
.collect(FutureValue.collect(reducing(Integer::sum));
The following is a list of stream operations and their possible Collector
replacements:
Modifier and Type | Method and Description |
---|---|
static <T,A,R> Collector<FutureValue<T>,?,CompletableFuture<R>> |
collect(Collector<T,A,R> collector)
|
static <T> UnaryOperator<FutureValue<T>> |
filter(Predicate<? super T> predicate)
Returns a unary operator that applies filtering to a stream of
FutureValue . |
static <T> Collector<FutureValue<T>,?,CompletableFuture<Optional<T>>> |
findAny()
Returns a
Collector that returns any CompletionStage result. |
static <T> Collector<FutureValue<T>,?,CompletableFuture<Optional<T>>> |
findAny(ExecutorService executor)
Returns a
Collector that returns any CompletionStage result. |
static <T,R> Function<FutureValue<T>,FutureValue<R>> |
flatMap(Function<? super T,? extends CompletionStage<R>> mapper)
Returns a function that transforms one
FutureValue instance into another. |
static <T,R> Function<FutureValue<T>,FutureValue<R>> |
map(Function<? super T,? extends R> mapper)
Returns a function that transforms one
FutureValue instance into another. |
static <T> Consumer<FutureValue<T>> |
run(Consumer<? super T> action)
Returns a consumer that performs an action on
FutureValue instances. |
static <T> FutureValue<T> |
wrap(CompletableFuture<T> future)
Wraps a
CompletableFuture in a FutureValue . |
static <T> FutureValue<T> |
wrap(CompletionStage<T> future)
Wraps a
CompletionStage in a FutureValue . |
public static <T> FutureValue<T> wrap(CompletionStage<T> future)
CompletionStage
in a FutureValue
. This is usually used in Stream.map(Function)
to start using this class.T
- The type of CompletionStage
result.future
- The future to wrap.FutureValue
wrapping the given CompletionStage
.NullPointerException
- If the given CompletionStage
is null
.public static <T> FutureValue<T> wrap(CompletableFuture<T> future)
CompletableFuture
in a FutureValue
. This is usually used in Stream.map(Function)
to start using this class.T
- The type of CompletableFuture
result.future
- The future to wrap.FutureValue
wrapping the given CompletableFuture
.NullPointerException
- If the given CompletableFuture
is null
.public static <T> UnaryOperator<FutureValue<T>> filter(Predicate<? super T> predicate)
FutureValue
. However, because the actual filtering is done
asynchronously, this must be applied using Stream.map(Function)
and not using Stream.filter(Predicate)
.T
- The type of CompletionStage
result.predicate
- The predicate to apply.FutureValue
.NullPointerException
- If the given predicate is null
.public static <T,R> Function<FutureValue<T>,FutureValue<R>> map(Function<? super T,? extends R> mapper)
FutureValue
instance into another. This is usually used in a call to
Stream.map(Function)
to transform CompletionStage
results.T
- The type of the input to the function.R
- The type of the result of the function.mapper
- The function to apply to each CompletionStage
result.FutureValue
instance into anotherNullPointerException
- If the given function is null
.public static <T,R> Function<FutureValue<T>,FutureValue<R>> flatMap(Function<? super T,? extends CompletionStage<R>> mapper)
FutureValue
instance into another. This is usually used in a call to
Stream.map(Function)
to transform CompletionStage
results. Unlike map(Function)
, the result of the function must be
a CompletionStage
, e.g. a CompletableFuture
.T
- The type of the input to the function.R
- The type of the returned CompletionStage
result.mapper
- The function to apply to each CompletionStage
result.FutureValue
instance into anotherNullPointerException
- If the given function is null
.public static <T,A,R> Collector<FutureValue<T>,?,CompletableFuture<R>> collect(Collector<T,A,R> collector)
Collector
that accumulates FutureValue
instances into a CompletableFuture
.
This method is similar to AdditionalCollectors.completableFutures(Collector)
. That method can be used if no filtering or mapping is
needed on the CompletionStage
results.T
- The result type of the CompletionStage
instances.A
- The intermediate accumulation type of the Collector
.R
- The result type of the collected CompletableFuture
.collector
- The collector for the CompletionStage
results.Collector
that collects FutureValue
instances.NullPointerException
- If the given Collector
is null
.public static <T> Consumer<FutureValue<T>> run(Consumer<? super T> action)
FutureValue
instances. This is usually used in a call to Stream.forEach(Consumer)
or Stream.forEachOrdered(Consumer)
.
Although this method can be used in a call to Stream.peek(Consumer)
, the result is unpredictable due to the asynchronous nature of
CompletionStage
s. The same goes for Stream.forEachOrdered(Consumer)
.
T
- The type of CompletionStage
result.action
- The action to perform for each CompletionStage
result. Note that the action is called asynchronously.FutureValue
instances asynchronously.NullPointerException
- If the given action is null
.public static <T> Collector<FutureValue<T>,?,CompletableFuture<Optional<T>>> findAny()
Collector
that returns any CompletionStage
result.
To prevent waiting for more CompletionStage
s to finish than necessary, this method will create another CompletableFuture
using the ForkJoinPool.commonPool()
. This will return as value the first available result, or throw the first available error.T
- The type of CompletionStage
result.Collector
that returns any CompletionStage
result.public static <T> Collector<FutureValue<T>,?,CompletableFuture<Optional<T>>> findAny(ExecutorService executor)
Collector
that returns any CompletionStage
result.
To prevent waiting for more CompletionStage
s to finish than necessary, this method will create another CompletableFuture
.
This will return as value the first available result, or throw the first available error.T
- The type of CompletionStage
result.executor
- The executor service to use for creating the new CompletableFuture
.Collector
that returns any CompletionStage
result.NullPointerException
- If the given executor service is null
.Copyright © 2021–2022. All rights reserved.