Class MessageStreamUtils
MessageStreams.- Since:
- 5.0.0
- Author:
- Allard Buijze
-
Method Summary
Modifier and TypeMethodDescriptionstatic <M extends Message>
CompletableFuture<MessageStream.Entry<M>> asCompletableFuture(MessageStream<M> source) static <M extends Message,R>
CompletableFuture<R> reduce(MessageStream<M> source, R identity, BiFunction<R, MessageStream.Entry<M>, R> accumulator) Returns aCompletableFuturethat completes with the given reduction of messages read from thesource.
-
Method Details
-
reduce
public static <M extends Message,R> CompletableFuture<R> reduce(@Nonnull MessageStream<M> source, @Nonnull R identity, @Nonnull BiFunction<R, MessageStream.Entry<M>, R> accumulator) Returns aCompletableFuturethat completes with the given reduction of messages read from thesource. The reduction is computed by applying the givenaccumulatorfunction on the result of the previous invocation in combination with eachentryreturned by the givensource. The very first invocation of the accumulator function is given theidentity.If the given
sourcecompletes normally without producing any entries, the returnedCompletableFuturecompletes with the givenidentity.If the given
sourcecompletes with an error, whether entries have been produced or not, the returnedCompletableFuturecompletes exceptionally with that error.Multi-threading
The accumulator function is invoked either on the thread calling this method, when entries are immediately available for reading, or on the thread on which entries are reported to be available for reading from the givensource. The accumulator function does not need to be thread-safe.- Type Parameters:
M- The type ofMessageto consume from theMessageStream.R- The type of result expected from the reduction operation.- Parameters:
source- TheMessageStreamto consume messages from.identity- The initial value to use for the accumulation.accumulator- The function to combine the current reduction result with the nextentryfrom theMessageStream.- Returns:
- A
CompletableFuturethat completes with the result of the reduction operation.
-
asCompletableFuture
public static <M extends Message> CompletableFuture<MessageStream.Entry<M>> asCompletableFuture(@Nonnull MessageStream<M> source) Returns aCompletableFuturethat completes with the firstentryfrom the givensource.If the given source has completed without producing any entries, the returned
CompletableFuturewill either complete with anullresult if the source completed normally, or exceptionally if the source completed with an error.Once the first entry is read from the source, it is automatically closed, and any subsequent entries in the
sourceare ignored.
-