forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationJoin.java
More file actions
298 lines (257 loc) · 9.77 KB
/
OperationJoin.java
File metadata and controls
298 lines (257 loc) · 9.77 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
import java.util.HashMap;
import java.util.Map;
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.SerialSubscription;
import rx.util.functions.Func1;
import rx.util.functions.Func2;
/**
* Correlates the elements of two sequences based on overlapping durations.
*/
public class OperationJoin<TLeft, TRight, TLeftDuration, TRightDuration, R> implements OnSubscribeFunc<R> {
final Observable<TLeft> left;
final Observable<TRight> right;
final Func1<TLeft, Observable<TLeftDuration>> leftDurationSelector;
final Func1<TRight, Observable<TRightDuration>> rightDurationSelector;
final Func2<TLeft, TRight, R> resultSelector;
public OperationJoin(
Observable<TLeft> left,
Observable<TRight> right,
Func1<TLeft, Observable<TLeftDuration>> leftDurationSelector,
Func1<TRight, Observable<TRightDuration>> rightDurationSelector,
Func2<TLeft, TRight, R> resultSelector) {
this.left = left;
this.right = right;
this.leftDurationSelector = leftDurationSelector;
this.rightDurationSelector = rightDurationSelector;
this.resultSelector = resultSelector;
}
@Override
public Subscription onSubscribe(Observer<? super R> t1) {
SerialSubscription cancel = new SerialSubscription();
ResultSink result = new ResultSink(t1, cancel);
cancel.setSubscription(result.run());
return cancel;
}
/** Manage the left and right sources. */
class ResultSink {
final Object gate = new Object();
final CompositeSubscription group = new CompositeSubscription();
boolean leftDone;
int leftId;
final Map<Integer, TLeft> leftMap = new HashMap<Integer, TLeft>();
boolean rightDone;
int rightId;
final Map<Integer, TRight> rightMap = new HashMap<Integer, TRight>();
final Observer<? super R> observer;
final Subscription cancel;
public ResultSink(Observer<? super R> observer, Subscription cancel) {
this.observer = observer;
this.cancel = cancel;
}
public Subscription run() {
SerialSubscription leftCancel = new SerialSubscription();
SerialSubscription rightCancel = new SerialSubscription();
group.add(leftCancel);
group.add(rightCancel);
leftCancel.setSubscription(left.subscribe(new LeftObserver(leftCancel)));
rightCancel.setSubscription(right.subscribe(new RightObserver(rightCancel)));
return group;
}
/** Observes the left values. */
class LeftObserver implements Observer<TLeft> {
final Subscription self;
public LeftObserver(Subscription self) {
this.self = self;
}
protected void expire(int id, Subscription resource) {
synchronized (gate) {
if (leftMap.remove(id) != null && leftMap.isEmpty() && leftDone) {
observer.onCompleted();
cancel.unsubscribe();
}
}
group.remove(resource);
}
@Override
public void onNext(TLeft args) {
int id;
synchronized (gate) {
id = leftId++;
leftMap.put(id, args);
}
SerialSubscription md = new SerialSubscription();
group.add(md);
Observable<TLeftDuration> duration;
try {
duration = leftDurationSelector.call(args);
} catch (Throwable t) {
observer.onError(t);
cancel.unsubscribe();
return;
}
md.setSubscription(duration.subscribe(new LeftDurationObserver(id, md)));
synchronized (gate) {
for (TRight r : rightMap.values()) {
R result;
try {
result = resultSelector.call(args, r);
} catch (Throwable t) {
observer.onError(t);
cancel.unsubscribe();
return;
}
observer.onNext(result);
}
}
}
@Override
public void onError(Throwable e) {
synchronized (gate) {
observer.onError(e);
cancel.unsubscribe();
}
}
@Override
public void onCompleted() {
synchronized (gate) {
leftDone = true;
if (rightDone || leftMap.isEmpty()) {
observer.onCompleted();
cancel.unsubscribe();
} else {
self.unsubscribe();
}
}
}
/** Observes the left duration. */
class LeftDurationObserver implements Observer<TLeftDuration> {
final int id;
final Subscription handle;
public LeftDurationObserver(int id, Subscription handle) {
this.id = id;
this.handle = handle;
}
@Override
public void onNext(TLeftDuration args) {
expire(id, handle);
}
@Override
public void onError(Throwable e) {
LeftObserver.this.onError(e);
}
@Override
public void onCompleted() {
expire(id, handle);
}
}
}
/** Observes the right values. */
class RightObserver implements Observer<TRight> {
final Subscription self;
public RightObserver(Subscription self) {
this.self = self;
}
void expire(int id, Subscription resource) {
synchronized (gate) {
if (rightMap.remove(id) != null && rightMap.isEmpty() && rightDone) {
observer.onCompleted();
cancel.unsubscribe();
}
}
group.remove(resource);
}
@Override
public void onNext(TRight args) {
int id = 0;
synchronized (gate) {
id = rightId++;
rightMap.put(id, args);
}
SerialSubscription md = new SerialSubscription();
group.add(md);
Observable<TRightDuration> duration;
try {
duration = rightDurationSelector.call(args);
} catch (Throwable t) {
observer.onError(t);
cancel.unsubscribe();
return;
}
md.setSubscription(duration.subscribe(new RightDurationObserver(id, md)));
synchronized (gate) {
for (TLeft lv : leftMap.values()) {
R result;
try {
result = resultSelector.call(lv, args);
} catch (Throwable t) {
observer.onError(t);
cancel.unsubscribe();
return;
}
observer.onNext(result);
}
}
}
@Override
public void onError(Throwable e) {
synchronized (gate) {
observer.onError(e);
cancel.unsubscribe();
}
}
@Override
public void onCompleted() {
synchronized (gate) {
rightDone = true;
if (leftDone || rightMap.isEmpty()) {
observer.onCompleted();
cancel.unsubscribe();
} else {
self.unsubscribe();
}
}
}
/** Observe the right duration. */
class RightDurationObserver implements Observer<TRightDuration> {
final int id;
final Subscription handle;
public RightDurationObserver(int id, Subscription handle) {
this.id = id;
this.handle = handle;
}
@Override
public void onNext(TRightDuration args) {
expire(id, handle);
}
@Override
public void onError(Throwable e) {
RightObserver.this.onError(e);
}
@Override
public void onCompleted() {
expire(id, handle);
}
}
}
}
}