Skip to content

Commit

Permalink
Adds example for lastOption operator akka#25468
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdimousavi1995 committed Oct 15, 2018
1 parent 0405d5f commit 531b645
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
Expand Up @@ -27,3 +27,10 @@ completed with @scala[`None`] @java[an empty `Optional`].

@@@

## Example

Scala
: @@snip [LastSinkSpec.scala](/akka-stream-tests/src/test/scala/akka/stream/scaladsl/LastSinkSpec.scala) { #lastOption-operator-example }

Java
: @@snip [SinkDocExamples.java](/akka-docs/src/test/java/jdocs/stream/operators/SinkDocExamples.java) { #lastOption-operator-example }
Expand Up @@ -6,6 +6,7 @@

import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.japi.Pair;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
Expand Down Expand Up @@ -37,27 +38,27 @@ static void reduceExample() throws InterruptedException, ExecutionException, Tim
static void takeLastExample() throws InterruptedException, ExecutionException, TimeoutException {
//#takeLast-operator-example
// tuple of (Name, GPA)
List<Tuple2> sortedStudents = Arrays.asList(new Tuple2("Benita", 2.1), new Tuple2("Adrian", 3.1),
new Tuple2("Alexis", 4), new Tuple2("Kendra", 4.2), new Tuple2("Jerrie", 4.3), new Tuple2("Alison", 4.7));
List<Pair> sortedStudents = Arrays.asList(new Pair<>("Benita", 2.1), new Pair<>("Adrian", 3.1),
new Pair<>("Alexis", 4), new Pair<>("Kendra", 4.2), new Pair<>("Jerrie", 4.3), new Pair<>("Alison", 4.7));

Source<Tuple2, NotUsed> studentSource = Source.from(sortedStudents);
Source<Pair, NotUsed> studentSource = Source.from(sortedStudents);

CompletionStage<List<Tuple2>> topThree = studentSource.runWith(Sink.takeLast(3), materializer);
CompletionStage<List<Pair>> topThree = studentSource.runWith(Sink.takeLast(3), materializer);

List<Tuple2> result = topThree.toCompletableFuture().get(3, TimeUnit.SECONDS);
List<Pair> result = topThree.toCompletableFuture().get(3, TimeUnit.SECONDS);

System.out.println("#### Top students ####");
for (int i = result.size() - 1; i >= 0; i--) {
Tuple2<String, Double> s = result.get(i);
System.out.println("Name: " + s._1 + ", " + "GPA: " + s._2);
Pair<String, Double> s = result.get(i);
System.out.println("Name: " + s.first() + ", " + "GPA: " + s.second());
}
/*
#### Top students ####
Name: Alison, GPA: 4.7
Name: Jerrie, GPA: 4.3
Name: Kendra, GPA: 4.2
*/
//#takeLast-operator-example
//#takeLast-operator-example
}

static void lastExample() throws InterruptedException, ExecutionException, TimeoutException {
Expand All @@ -70,4 +71,13 @@ static void lastExample() throws InterruptedException, ExecutionException, Timeo
//#last-operator-example
}

static void lastOptionExample() throws InterruptedException, ExecutionException, TimeoutException {
//#lastOption-operator-example
Source<Integer, NotUsed> source = Source.empty();
CompletionStage<Optional<Integer>> result = source.runWith(Sink.<Integer>lastOption(), materializer);
Optional<Integer> optItem = result.toCompletableFuture().get(3, TimeUnit.SECONDS);
System.out.println(optItem);
// Optional.empty
//#lastOption-operator-example
}
}
@@ -1,30 +1,29 @@
/**
* Copyright (C) 2014-2018 Lightbend Inc. <https://www.lightbend.com>
*/
* Copyright (C) 2014-2018 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.stream.scaladsl

import scala.concurrent.Await
import scala.concurrent.duration._

import akka.stream.ActorMaterializer
import akka.stream.ActorMaterializerSettings
import akka.stream.{ActorMaterializer, ActorMaterializerSettings}
import akka.stream.testkit._
import akka.stream.testkit.Utils._
import akka.stream.testkit.scaladsl.StreamTestKit._

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}

class LastSinkSpec extends StreamSpec with ScriptedTest {

val settings = ActorMaterializerSettings(system)

implicit val materializer: ActorMaterializer = ActorMaterializer(settings)
implicit val ec = system.dispatcher

"A Flow with Sink.last" must {

"yield the last value" in {
"yield the last value" in {
//#last-operator-example
val source = Source(1 to 10)
val result = source.runWith(Sink.last)
val result: Future[Int] = source.runWith(Sink.last)
result.map(println)
// 10
//#last-operator-example
Expand Down Expand Up @@ -58,8 +57,14 @@ class LastSinkSpec extends StreamSpec with ScriptedTest {
} should be theSameInstanceAs (ex)
}

"yield None for empty stream" in assertAllStagesStopped {
Await.result(Source.empty[Int].runWith(Sink.lastOption), 1.second) should be(None)
"yield None for empty stream" in {
//#lastOption-operator-example
val source = Source.empty[Int]
val result: Future[Option[Int]] = source.runWith(Sink.lastOption)
result.map(println)
// None
//#lastOption-operator-example
result.futureValue shouldEqual None
}

}
Expand Down
Expand Up @@ -35,7 +35,7 @@ class TakeLastSinkSpec extends StreamSpec {

val sourceOfStudents = Source(students)

val result = sourceOfStudents.runWith(Sink.takeLast(3))
val result: Future[Seq[Student]] = sourceOfStudents.runWith(Sink.takeLast(3))

result.foreach { topThree
println("#### Top students ####")
Expand Down

0 comments on commit 531b645

Please sign in to comment.