Formatting java codes with sbt-java-formatter.

This commit is contained in:
hepin1989 2019-01-12 04:00:53 +08:00
parent 27500001ea
commit 998c5a9285
401 changed files with 19750 additions and 17450 deletions

View file

@ -11,9 +11,9 @@ import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;
//#takeLast-operator-example
// #takeLast-operator-example
import akka.japi.Pair;
//#takeLast-operator-example
// #takeLast-operator-example
import java.util.*;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
@ -21,60 +21,68 @@ import java.util.concurrent.TimeoutException;
public class SinkDocExamples {
private final static ActorSystem system = ActorSystem.create("SourceFromExample");
private final static Materializer materializer = ActorMaterializer.create(system);
private static final ActorSystem system = ActorSystem.create("SourceFromExample");
private static final Materializer materializer = ActorMaterializer.create(system);
static void reduceExample() throws InterruptedException, ExecutionException, TimeoutException {
static void reduceExample() throws InterruptedException, ExecutionException, TimeoutException {
//#reduce-operator-example
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> sum = ints.runWith(Sink.reduce((a, b) -> a + b), materializer);
sum.thenAccept(System.out::println);
// 55
//#reduce-operator-example
}
// #reduce-operator-example
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> sum = ints.runWith(Sink.reduce((a, b) -> a + b), materializer);
sum.thenAccept(System.out::println);
// 55
// #reduce-operator-example
}
static void takeLastExample() throws InterruptedException, ExecutionException, TimeoutException {
//#takeLast-operator-example
// pair of (Name, GPA)
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));
static void takeLastExample() throws InterruptedException, ExecutionException, TimeoutException {
// #takeLast-operator-example
// pair of (Name, GPA)
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<Pair, NotUsed> studentSource = Source.from(sortedStudents);
Source<Pair, NotUsed> studentSource = Source.from(sortedStudents);
CompletionStage<List<Pair>> topThree = studentSource.runWith(Sink.takeLast(3), materializer);
CompletionStage<List<Pair>> topThree = studentSource.runWith(Sink.takeLast(3), materializer);
topThree.thenAccept(result -> {
System.out.println("#### Top students ####");
for (int i = result.size() - 1; i >= 0; i--) {
Pair<String, Double> s = result.get(i);
System.out.println("Name: " + s.first() + ", " + "GPA: " + s.second());
}
topThree.thenAccept(
result -> {
System.out.println("#### Top students ####");
for (int i = result.size() - 1; i >= 0; i--) {
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
}
/*
#### Top students ####
Name: Alison, GPA: 4.7
Name: Jerrie, GPA: 4.3
Name: Kendra, GPA: 4.2
*/
// #takeLast-operator-example
}
static void lastExample() throws InterruptedException, ExecutionException, TimeoutException {
//#last-operator-example
Source<Integer, NotUsed> source = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> result = source.runWith(Sink.last(), materializer);
result.thenAccept(System.out::println);
// 10
//#last-operator-example
}
static void lastExample() throws InterruptedException, ExecutionException, TimeoutException {
// #last-operator-example
Source<Integer, NotUsed> source = Source.from(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
CompletionStage<Integer> result = source.runWith(Sink.last(), materializer);
result.thenAccept(System.out::println);
// 10
// #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.lastOption(), materializer);
result.thenAccept(System.out::println);
// Optional.empty
//#lastOption-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.lastOption(), materializer);
result.thenAccept(System.out::println);
// Optional.empty
// #lastOption-operator-example
}
}

View file

@ -4,84 +4,84 @@
package jdocs.stream.operators;
//#imports
//#range-imports
// #imports
// #range-imports
import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Source;
//#range-imports
// #range-imports
//#actor-ref-imports
// #actor-ref-imports
import akka.actor.ActorRef;
import akka.actor.Status.Success;
import akka.stream.OverflowStrategy;
import akka.stream.javadsl.Sink;
//#actor-ref-imports
// #actor-ref-imports
import java.util.Arrays;
//#imports
// #imports
public class SourceDocExamples {
public static void fromExample() {
//#source-from-example
final ActorSystem system = ActorSystem.create("SourceFromExample");
final Materializer materializer = ActorMaterializer.create(system);
public static void fromExample() {
// #source-from-example
final ActorSystem system = ActorSystem.create("SourceFromExample");
final Materializer materializer = ActorMaterializer.create(system);
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(0, 1, 2, 3, 4, 5));
ints.runForeach(System.out::println, materializer);
Source<Integer, NotUsed> ints = Source.from(Arrays.asList(0, 1, 2, 3, 4, 5));
ints.runForeach(System.out::println, materializer);
String text = "Perfection is finally attained not when there is no longer more to add," +
"but when there is no longer anything to take away.";
Source<String, NotUsed> words = Source.from(Arrays.asList(text.split("\\s")));
words.runForeach(System.out::println, materializer);
//#source-from-example
}
String text =
"Perfection is finally attained not when there is no longer more to add,"
+ "but when there is no longer anything to take away.";
Source<String, NotUsed> words = Source.from(Arrays.asList(text.split("\\s")));
words.runForeach(System.out::println, materializer);
// #source-from-example
}
static void rangeExample() {
static void rangeExample() {
final ActorSystem system = ActorSystem.create("Source");
final Materializer materializer = ActorMaterializer.create(system);
final ActorSystem system = ActorSystem.create("Source");
final Materializer materializer = ActorMaterializer.create(system);
//#range
// #range
Source<Integer, NotUsed> source = Source.range(1, 100);
Source<Integer, NotUsed> source = Source.range(1, 100);
//#range
// #range
//#range
Source<Integer, NotUsed> sourceStepFive = Source.range(1, 100, 5);
// #range
Source<Integer, NotUsed> sourceStepFive = Source.range(1, 100, 5);
//#range
// #range
//#range
Source<Integer, NotUsed> sourceStepNegative = Source.range(100, 1, -1);
//#range
// #range
Source<Integer, NotUsed> sourceStepNegative = Source.range(100, 1, -1);
// #range
//#run-range
source.runForeach(i -> System.out.println(i), materializer);
//#run-range
}
// #run-range
source.runForeach(i -> System.out.println(i), materializer);
// #run-range
}
static void actorRef() {
//#actor-ref
static void actorRef() {
// #actor-ref
final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);
final ActorSystem system = ActorSystem.create();
final Materializer materializer = ActorMaterializer.create(system);
int bufferSize = 100;
Source<Object, ActorRef> source = Source.actorRef(bufferSize, OverflowStrategy.dropHead());
int bufferSize = 100;
Source<Object, ActorRef> source = Source.actorRef(bufferSize, OverflowStrategy.dropHead());
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(materializer);
actorRef.tell("hello", ActorRef.noSender());
actorRef.tell("hello", ActorRef.noSender());
// The stream completes successfully with the following message
actorRef.tell(new Success("completes stream"), ActorRef.noSender());
//#actor-ref
}
ActorRef actorRef = source.to(Sink.foreach(System.out::println)).run(materializer);
actorRef.tell("hello", ActorRef.noSender());
actorRef.tell("hello", ActorRef.noSender());
// The stream completes successfully with the following message
actorRef.tell(new Success("completes stream"), ActorRef.noSender());
// #actor-ref
}
}

View file

@ -10,170 +10,171 @@ import akka.stream.javadsl.Flow;
import akka.NotUsed;
import akka.japi.function.Function2;
//#zip
//#zip-with
//#zip-with-index
//#or-else
//#prepend
//#concat
//#interleave
//#merge
//#merge-sorted
// #zip
// #zip-with
// #zip-with-index
// #or-else
// #prepend
// #concat
// #interleave
// #merge
// #merge-sorted
import akka.stream.javadsl.Source;
import akka.stream.javadsl.Sink;
import java.util.Arrays;
//#merge-sorted
//#merge
//#interleave
//#concat
//#prepend
//#or-else
//#zip-with-index
//#zip-with
//#zip
// #merge-sorted
// #merge
// #interleave
// #concat
// #prepend
// #or-else
// #zip-with-index
// #zip-with
// #zip
//#log
// #log
import akka.stream.Attributes;
import akka.stream.javadsl.Source;
//#log
// #log
import java.time.Duration;
import java.util.Arrays;
import java.util.Comparator;
class SourceOrFlow {
private static Materializer materializer = null;
void logExample() {
Flow.of(String.class)
//#log
// #log
.log("myStream")
.addAttributes(Attributes.createLogLevels(
Attributes.logLevelOff(), // onElement
Attributes.logLevelError(), // onFailure
Attributes.logLevelInfo())) // onFinish
//#log
.addAttributes(
Attributes.createLogLevels(
Attributes.logLevelOff(), // onElement
Attributes.logLevelError(), // onFailure
Attributes.logLevelInfo())) // onFinish
// #log
;
}
void zipWithIndexExample() {
Materializer materializer = null;
//#zip-with-index
// #zip-with-index
Source.from(Arrays.asList("apple", "orange", "banana"))
.zipWithIndex()
.runWith(Sink.foreach(System.out::print), materializer);
.zipWithIndex()
.runWith(Sink.foreach(System.out::print), materializer);
// this will print ('apple', 0), ('orange', 1), ('banana', 2)
//#zip-with-index
// #zip-with-index
}
void zipExample() {
//#zip
// #zip
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
Source<String, NotUsed> sourceFirstLetters = Source.from(Arrays.asList("A", "O", "B"));
sourceFruits.zip(sourceFirstLetters).runWith(Sink.foreach(System.out::print), materializer);
// this will print ('apple', 'A'), ('orange', 'O'), ('banana', 'B')
//#zip
// #zip
}
void zipWithExample() {
//#zip-with
// #zip-with
Source<String, NotUsed> sourceCount = Source.from(Arrays.asList("one", "two", "three"));
Source<String, NotUsed> sourceFruits = Source.from(Arrays.asList("apple", "orange", "banana"));
sourceCount.zipWith(
sourceCount
.zipWith(
sourceFruits,
(Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName
).runWith(Sink.foreach(System.out::print), materializer);
(Function2<String, String, String>) (countStr, fruitName) -> countStr + " " + fruitName)
.runWith(Sink.foreach(System.out::print), materializer);
// this will print 'one apple', 'two orange', 'three banana'
//#zip-with
// #zip-with
}
void prependExample() {
//#prepend
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), materializer);
// this will print "Emma", "Emily", "Liam", "William"
// #prepend
Source<String, NotUsed> ladies = Source.from(Arrays.asList("Emma", "Emily"));
Source<String, NotUsed> gentlemen = Source.from(Arrays.asList("Liam", "William"));
gentlemen.prepend(ladies).runWith(Sink.foreach(System.out::print), materializer);
// this will print "Emma", "Emily", "Liam", "William"
//#prepend
// #prepend
}
void concatExample() {
//#concat
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), materializer);
//prints 1, 2, 3, 4, 10, 20, 30, 40
// #concat
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.concat(sourceB).runWith(Sink.foreach(System.out::print), materializer);
// prints 1, 2, 3, 4, 10, 20, 30, 40
//#concat
// #concat
}
void interleaveExample() {
//#interleave
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), materializer);
//prints 1, 2, 10, 20, 3, 4, 30, 40
// #interleave
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.interleave(sourceB, 2).runWith(Sink.foreach(System.out::print), materializer);
// prints 1, 2, 10, 20, 3, 4, 30, 40
//#interleave
// #interleave
}
void mergeExample() {
//#merge
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), materializer);
// merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40
// #merge
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 2, 3, 4));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(10, 20, 30, 40));
sourceA.merge(sourceB).runWith(Sink.foreach(System.out::print), materializer);
// merging is not deterministic, can for example print 1, 2, 3, 4, 10, 20, 30, 40
//#merge
// #merge
}
void mergeSortedExample() {
//#merge-sorted
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 3, 5, 7));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(2, 4, 6, 8));
sourceA.mergeSorted(sourceB, Comparator.<Integer>naturalOrder()).runWith(Sink.foreach(System.out::print), materializer);
//prints 1, 2, 3, 4, 5, 6, 7, 8
// #merge-sorted
Source<Integer, NotUsed> sourceA = Source.from(Arrays.asList(1, 3, 5, 7));
Source<Integer, NotUsed> sourceB = Source.from(Arrays.asList(2, 4, 6, 8));
sourceA
.mergeSorted(sourceB, Comparator.<Integer>naturalOrder())
.runWith(Sink.foreach(System.out::print), materializer);
// prints 1, 2, 3, 4, 5, 6, 7, 8
Source<Integer, NotUsed> sourceC = Source.from(Arrays.asList(20, 1, 1, 1));
sourceA.mergeSorted(sourceC, Comparator.<Integer>naturalOrder()).runWith(Sink.foreach(System.out::print), materializer);
//prints 1, 3, 5, 7, 20, 1, 1, 1
//#merge-sorted
Source<Integer, NotUsed> sourceC = Source.from(Arrays.asList(20, 1, 1, 1));
sourceA
.mergeSorted(sourceC, Comparator.<Integer>naturalOrder())
.runWith(Sink.foreach(System.out::print), materializer);
// prints 1, 3, 5, 7, 20, 1, 1, 1
// #merge-sorted
}
void orElseExample() {
//#or-else
Source<String, NotUsed> source1 = Source.from(Arrays.asList("First source"));
Source<String, NotUsed> source2 = Source.from(Arrays.asList("Second source"));
Source<String, NotUsed> emptySource = Source.empty();
// #or-else
Source<String, NotUsed> source1 = Source.from(Arrays.asList("First source"));
Source<String, NotUsed> source2 = Source.from(Arrays.asList("Second source"));
Source<String, NotUsed> emptySource = Source.empty();
source1.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
// this will print "First source"
source1.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
// this will print "First source"
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
// this will print "Second source"
emptySource.orElse(source2).runWith(Sink.foreach(System.out::print), materializer);
// this will print "Second source"
//#or-else
// #or-else
}
void conflateExample() {
//#conflate
// #conflate
Source.cycle(() -> Arrays.asList(1, 10, 100).iterator())
.throttle(10, Duration.ofSeconds(1)) // fast upstream
.conflate((Integer acc, Integer el) -> acc + el)
.throttle(1, Duration.ofSeconds(1)); // slow downstream
//#conflate
// #conflate
}
void scanExample() {
//#scan
// #scan
Source<Integer, NotUsed> source = Source.range(1, 5);
source.scan(0, (acc, x) -> acc + x).runForeach(System.out::println, materializer);
// 0 (= 0)
@ -182,10 +183,10 @@ class SourceOrFlow {
// 6 (= 0 + 1 + 2 + 3)
// 10 (= 0 + 1 + 2 + 3 + 4)
// 15 (= 0 + 1 + 2 + 3 + 4 + 5)
//#scan
// #scan
}
static //#conflateWithSeed-type
static // #conflateWithSeed-type
class Summed {
private final Integer el;
@ -198,15 +199,15 @@ class SourceOrFlow {
return new Summed(this.el + other.el);
}
}
//#conflateWithSeed-type
// #conflateWithSeed-type
void conflateWithSeedExample() {
//#conflateWithSeed
// #conflateWithSeed
Source.cycle(() -> Arrays.asList(1, 10, 100).iterator())
.throttle(10, Duration.ofSeconds(1)) // fast upstream
.conflateWithSeed(Summed::new, (Summed acc, Integer el) -> acc.sum(new Summed(el)))
.throttle(1, Duration.ofSeconds(1)); // slow downstream
//#conflateWithSeed
// #conflateWithSeed
}
}