-
Notifications
You must be signed in to change notification settings - Fork 214
/
Pipe.java
117 lines (102 loc) · 3.95 KB
/
Pipe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.pekko.controlflow;
import java.util.Collection;
import org.apache.pekko.NotUsed;
import org.apache.pekko.stream.FanOutShape2;
import org.apache.pekko.stream.FlowShape;
import org.apache.pekko.stream.Graph;
import org.apache.pekko.stream.SinkShape;
import org.apache.pekko.stream.javadsl.Flow;
import org.apache.pekko.stream.javadsl.GraphDSL;
/**
* Combinators to join Akka stream processors.
*/
public final class Pipe {
private Pipe() {
throw new AssertionError();
}
/**
* Attach a sink to the output port of a filter.
*
* @param filter the filter.
* @param sink the sink.
* @param <A> type of incoming and unhandled messages.
* @param <B> type of messages that pass through the filter.
* @return joined flow.
*/
public static <A, B> Graph<FlowShape<A, A>, NotUsed> joinFilteredSink(
final Graph<FanOutShape2<A, B, A>, NotUsed> filter,
final Graph<SinkShape<B>, NotUsed> sink) {
return GraphDSL.create(builder -> {
final FanOutShape2<A, B, A> filterShape = builder.add(filter);
final SinkShape<B> sinkShape = builder.add(sink);
builder.from(filterShape.out0()).to(sinkShape);
return FlowShape.of(filterShape.in(), filterShape.out1());
});
}
/**
* Attach a sink to the unhandled port of a filter.
*
* @param filter the filter.
* @param unhandled the sink.
* @param <A> type of incoming messages.
* @param <B> type of messages that pass through the filter.
* @return joined flow.
*/
public static <A, B> Graph<FlowShape<A, B>, NotUsed> joinUnhandledSink(
final Graph<FanOutShape2<A, B, A>, NotUsed> filter,
final Graph<SinkShape<A>, NotUsed> unhandled) {
return GraphDSL.create(builder -> {
final FanOutShape2<A, B, A> filterShape = builder.add(filter);
final SinkShape<A> sinkShape = builder.add(unhandled);
builder.from(filterShape.out1()).to(sinkShape);
return FlowShape.of(filterShape.in(), filterShape.out0());
});
}
/**
* Attach a flow into the output port of a filter.
*
* @param filter the filter.
* @param flow the flow.
* @param <A> type of incoming and unhandled messages.
* @param <B> type of messages that pass through the filter.
* @param <C> output type of the flow.
* @return joined filter.
*/
public static <A, B, C> Graph<FanOutShape2<A, C, A>, NotUsed> joinFilteredFlow(
final Graph<FanOutShape2<A, B, A>, NotUsed> filter,
final Graph<FlowShape<B, C>, NotUsed> flow) {
return GraphDSL.create(builder -> {
final FanOutShape2<A, B, A> filterShape = builder.add(filter);
final FlowShape<B, C> flowShape = builder.add(flow);
builder.from(filterShape.out0()).toInlet(flowShape.in());
return new FanOutShape2<>(filterShape.in(), flowShape.out(), filterShape.out1());
});
}
/**
* Chain a collection of flows one after another.
*
* @param flows collection of flows.
* @param <A> type of messages through the flows.
* @return joined flow.
*/
public static <A> Graph<FlowShape<A, A>, NotUsed> joinFlows(
final Collection<Graph<FlowShape<A, A>, NotUsed>> flows) {
Flow<A, A, NotUsed> overallFlow = Flow.create();
for (Graph<FlowShape<A, A>, NotUsed> flow : flows) {
overallFlow = overallFlow.via(flow);
}
return overallFlow;
}
}