doc: Source.maybe, #25468 (#27832)

This commit is contained in:
Patrik Nordwall 2019-10-03 14:09:45 +02:00 committed by GitHub
parent a217d5566e
commit 1a8a438144
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 22 deletions

View file

@ -22,6 +22,11 @@ import akka.stream.javadsl.Sink;
import akka.testkit.TestProbe;
// #actor-ref-imports
// #maybe
import akka.stream.javadsl.RunnableGraph;
import java.util.concurrent.CompletableFuture;
// #maybe
import java.util.Arrays;
import java.util.Optional;
@ -32,9 +37,9 @@ public class SourceDocExamples {
public static final TestKitJunitResource testKit = new TestKitJunitResource(ManualTime.config());
public static void fromExample() {
// #source-from-example
final ActorSystem system = ActorSystem.create("SourceFromExample");
final ActorSystem system = null;
// #source-from-example
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(0, 1, 2, 3, 4, 5));
ints.runForeach(System.out::println, system);
@ -71,9 +76,9 @@ public class SourceDocExamples {
}
static void actorRef() {
// #actor-ref
final ActorSystem system = null;
final ActorSystem system = ActorSystem.create();
// #actor-ref
int bufferSize = 100;
Source<Object, ActorRef> source = Source.actorRef(bufferSize, OverflowStrategy.dropHead());
@ -89,10 +94,9 @@ public class SourceDocExamples {
static void actorRefWithBackpressure() {
final TestProbe probe = null;
final ActorSystem system = null;
// #actorRefWithBackpressure
final ActorSystem system = ActorSystem.create();
Source<Object, ActorRef> source =
Source.actorRefWithBackpressure(
"ack",
@ -112,4 +116,28 @@ public class SourceDocExamples {
actorRef.tell("complete", ActorRef.noSender());
// #actorRefWithBackpressure
}
static void maybeExample() {
final ActorSystem system = null;
// #maybe
Source<Integer, CompletableFuture<Optional<Integer>>> source = Source.<Integer>maybe();
RunnableGraph<CompletableFuture<Optional<Integer>>> runnable =
source.to(Sink.foreach(System.out::println));
CompletableFuture<Optional<Integer>> completable1 = runnable.run(system);
completable1.complete(Optional.of(1)); // prints 1
CompletableFuture<Optional<Integer>> completable2 = runnable.run(system);
completable2.complete(Optional.of(2)); // prints 2
// #maybe
}
static
// #maybe-signature
<Out> Source<Out, CompletableFuture<Optional<Out>>> maybe()
// #maybe-signature
{
return Source.maybe();
}
}