Class MessageStreamUtils
MessageStreams.- Since:
- 5.0.0
- Author:
- Allard Buijze
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final RunnableA reusable no-operationRunnableinstance. -
Method Summary
Modifier and TypeMethodDescriptionstatic <M extends Message,R>
CompletableFuture<@Nullable R> reduce(MessageStream<M> source, @Nullable R identity, BiFunction<@Nullable R, ? super MessageStream.Entry<M>, @Nullable R> accumulator) Returns aCompletableFuturethat completes with the given reduction of messages read from thesource.
-
Field Details
-
NO_OP_CALLBACK
A reusable no-operationRunnableinstance.This constant can be used in contexts where a non-null callback is required, but no specific action needs to be performed. When executed, it performs no operation and immediately returns.
-
-
Method Details
-
reduce
public static <M extends Message,R> CompletableFuture<@Nullable R> reduce(MessageStream<M> source, @Nullable R identity, BiFunction<@Nullable R, ? super MessageStream.Entry<M>, @Nullable 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.
-