public abstract class Accumulator<E,A> extends Object
An accumulator is a view over an Akka streams Sink that materialises to a future, that is
focused on the value of that future, rather than the Stream. This means methods such as map
, recover
and so on are provided for the eventually redeemed future value.
In order to be in line with the Java ecosystem, the future implementation that this uses for
the materialised value of the Sink is java.util.concurrent.CompletionStage, and running this
accumulator will yield a CompletionStage. The constructor allows an accumulator to be created
from such a sink. Many methods in the Akka streams API however materialise a
scala.concurrent.Future, hence the fromSink
method is provided to create an accumulator
from a typical Akka streams Sink
.
Modifier and Type | Method and Description |
---|---|
abstract play.api.libs.streams.Accumulator<E,A> |
asScala()
Convert this accumulator to a Scala accumulator.
|
static <E,A> Accumulator<E,A> |
done(A a)
Create a done accumulator with the given value.
|
static <E,A> Accumulator<E,A> |
done(CompletionStage<A> a)
Create a done accumulator with the given future.
|
static <E,A> Accumulator<E,A> |
flatten(CompletionStage<Accumulator<E,A>> stage,
akka.stream.Materializer materializer)
Flatten a completion stage of an accumulator to an accumulator.
|
static <E,A> Accumulator<E,A> |
fromSink(akka.stream.javadsl.Sink<E,CompletionStage<A>> sink)
Create an accumulator from an Akka streams sink.
|
abstract <B> Accumulator<E,B> |
map(Function<? super A,? extends B> f,
Executor executor)
Map the accumulated value.
|
abstract <B> Accumulator<E,B> |
mapFuture(Function<? super A,? extends CompletionStage<B>> f,
Executor executor)
Map the accumulated value with a function that returns a future.
|
abstract Accumulator<E,A> |
recover(Function<? super Throwable,? extends A> f,
Executor executor)
Recover from any errors encountered by the accumulator.
|
abstract Accumulator<E,A> |
recoverWith(Function<? super Throwable,? extends CompletionStage<A>> f,
Executor executor)
Recover from any errors encountered by the accumulator.
|
abstract CompletionStage<A> |
run(E element,
akka.stream.Materializer mat)
Run the accumulator with a single element.
|
abstract CompletionStage<A> |
run(akka.stream.Materializer mat)
Run the accumulator with an empty source.
|
abstract CompletionStage<A> |
run(akka.stream.javadsl.Source<E,?> source,
akka.stream.Materializer mat)
Run the accumulator with the given source.
|
static <E> Accumulator<E,akka.stream.javadsl.Source<E,?>> |
source()
Create an accumulator that forwards the stream fed into it to the source it produces.
|
static <E,A> Accumulator<E,A> |
strict(Function<Optional<E>,CompletionStage<A>> strictHandler,
akka.stream.javadsl.Sink<E,CompletionStage<A>> toSink)
Create a done accumulator with the given future.
|
abstract <D> Accumulator<D,A> |
through(akka.stream.javadsl.Flow<D,E,?> flow)
Pass the stream through the given flow before forwarding it to the accumulator.
|
abstract akka.stream.javadsl.Sink<E,CompletionStage<A>> |
toSink()
Convert this accumulator to a sink.
|
public abstract <B> Accumulator<E,B> map(Function<? super A,? extends B> f, Executor executor)
B
- the mapped value typef
- The function to perform the map with.executor
- The executor to run the function in.public abstract <B> Accumulator<E,B> mapFuture(Function<? super A,? extends CompletionStage<B>> f, Executor executor)
B
- the mapped value typef
- The function to perform the map with.executor
- The executor to run the function in.public abstract Accumulator<E,A> recover(Function<? super Throwable,? extends A> f, Executor executor)
f
- The function to use to recover from errors.executor
- The executor to run the function in.public abstract Accumulator<E,A> recoverWith(Function<? super Throwable,? extends CompletionStage<A>> f, Executor executor)
f
- The function to use to recover from errors.executor
- The executor to run the function in.public abstract <D> Accumulator<D,A> through(akka.stream.javadsl.Flow<D,E,?> flow)
D
- the "In" type for the flow parameter.flow
- The flow to send the stream through first.public abstract CompletionStage<A> run(akka.stream.Materializer mat)
mat
- The flow materializer.public abstract CompletionStage<A> run(akka.stream.javadsl.Source<E,?> source, akka.stream.Materializer mat)
source
- The source to feed into the accumulator.mat
- The flow materializer.public abstract CompletionStage<A> run(E element, akka.stream.Materializer mat)
element
- The element to feed into the accumulator.mat
- The flow materilaizer.public abstract akka.stream.javadsl.Sink<E,CompletionStage<A>> toSink()
public abstract play.api.libs.streams.Accumulator<E,A> asScala()
public static <E,A> Accumulator<E,A> fromSink(akka.stream.javadsl.Sink<E,CompletionStage<A>> sink)
E
- the "in" type of the sink parameter.A
- the materialized result of the accumulator.sink
- The sink.public static <E> Accumulator<E,akka.stream.javadsl.Source<E,?>> source()
This is useful for when you want to send the consumed stream to another API that takes a Source as input.
Extreme care must be taken when using this accumulator - the source *must always* be materialized and consumed. If it isn't, this could lead to resource leaks and deadlocks upstream.
E
- the "in" type of the parameter.public static <E,A> Accumulator<E,A> done(A a)
E
- the "in" type of the parameter.A
- the materialized result of the accumulator.a
- The done value for the accumulator.public static <E,A> Accumulator<E,A> done(CompletionStage<A> a)
E
- the "in" type of the parameter.A
- the materialized result of the accumulator.a
- A future of the done value.public static <E,A> Accumulator<E,A> strict(Function<Optional<E>,CompletionStage<A>> strictHandler, akka.stream.javadsl.Sink<E,CompletionStage<A>> toSink)
E
- the "in" type of the parameter.A
- the materialized result of the accumulator.strictHandler
- the handler to handle the stream if it can be expressed as a single
element.toSink
- The sink representation of this accumulator, in case the stream can't be
expressed as a single element.public static <E,A> Accumulator<E,A> flatten(CompletionStage<Accumulator<E,A>> stage, akka.stream.Materializer materializer)
E
- the "in" type of the parameter.A
- the materialized result of the accumulator.stage
- the CompletionStage (asynchronous) accumulatormaterializer
- the stream materializer