Code example for collect and collectType (#27744)
Co-Authored-By: Helena Edelson <helena@users.noreply.github.com>
This commit is contained in:
parent
edad69b38c
commit
d8c8121c4d
4 changed files with 143 additions and 5 deletions
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
package jdocs.stream.operators;
|
||||
|
||||
import akka.japi.pf.PFBuilder;
|
||||
import akka.stream.Materializer;
|
||||
import akka.stream.javadsl.Flow;
|
||||
|
||||
|
|
@ -210,4 +211,45 @@ class SourceOrFlow {
|
|||
.throttle(1, Duration.ofSeconds(1)); // slow downstream
|
||||
// #conflateWithSeed
|
||||
}
|
||||
|
||||
// #collect-elements
|
||||
static interface Message {}
|
||||
|
||||
static class Ping implements Message {
|
||||
final int id;
|
||||
|
||||
Ping(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
static class Pong {
|
||||
final int id;
|
||||
|
||||
Pong(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
// #collect-elements
|
||||
|
||||
void collectExample() {
|
||||
// #collect
|
||||
Flow<Message, Pong, NotUsed> flow =
|
||||
Flow.of(Message.class)
|
||||
.collect(
|
||||
new PFBuilder<Message, Pong>()
|
||||
.match(Ping.class, p -> p.id != 0, p -> new Pong(p.id))
|
||||
.build());
|
||||
// #collect
|
||||
}
|
||||
|
||||
void collectTypeExample() {
|
||||
// #collectType
|
||||
Flow<Message, Pong, NotUsed> flow =
|
||||
Flow.of(Message.class)
|
||||
.collectType(Ping.class)
|
||||
.filter(p -> p.id != 0)
|
||||
.map(p -> new Pong(p.id));
|
||||
// #collectType
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue