Repository navigation
Expand file tree
/
Copy pathOperatorZip.java
More file actions
259 lines (225 loc) · 9.25 KB
/
Copy pathOperatorZip.java
File metadata and controls
259 lines (225 loc) · 9.25 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
/**
* 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.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;
import rx.Observable;
import rx.Observable.Operator;
import rx.Observer;
import rx.Subscriber;
import rx.exceptions.OnErrorThrowable;
import rx.functions.Func2;
import rx.functions.Func3;
import rx.functions.Func4;
import rx.functions.Func5;
import rx.functions.Func6;
import rx.functions.Func7;
import rx.functions.Func8;
import rx.functions.Func9;
import rx.functions.FuncN;
import rx.functions.Functions;
import rx.subscriptions.CompositeSubscription;
/**
* Returns an Observable that emits the results of a function applied to sets of items emitted, in
* sequence, by two or more other Observables.
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/zip.png">
* <p>
* The zip operation applies this function in strict sequence, so the first item emitted by the new
* Observable will be the result of the function applied to the first item emitted by each zipped
* Observable; the second item emitted by the new Observable will be the result of the function
* applied to the second item emitted by each zipped Observable; and so forth.
* <p>
* The resulting Observable returned from zip will invoke <code>onNext</code> as many times as the
* number of <code>onNext</code> invocations of the source Observable that emits the fewest items.
*/
public final class OperatorZip<R> implements Operator<R, Observable<?>[]> {
/*
* Raw types are used so we can use a single implementation for all arities such as zip(t1, t2) and zip(t1, t2, t3) etc.
* The types will be cast on the edges so usage will be the type-safe but the internals are not.
*/
final FuncN<? extends R> zipFunction;
public OperatorZip(FuncN<? extends R> f) {
this.zipFunction = f;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func2 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func3 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func4 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func5 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func6 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func7 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func8 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public OperatorZip(Func9 f) {
this.zipFunction = Functions.fromFunc(f);
}
@SuppressWarnings("rawtypes")
@Override
public Subscriber<? super Observable[]> call(final Subscriber<? super R> observer) {
return new Subscriber<Observable[]>(observer) {
boolean started = false;
@Override
public void onCompleted() {
if (!started) {
// this means we have not received a valid onNext before termination so we emit the onCompleted
observer.onCompleted();
}
}
@Override
public void onError(Throwable e) {
observer.onError(e);
}
@Override
public void onNext(Observable[] observables) {
if (observables == null || observables.length == 0) {
observer.onCompleted();
} else {
started = true;
new Zip<R>(observables, observer, zipFunction).zip();
}
}
};
}
static final NotificationLite on = NotificationLite.instance();
private static class Zip<R> {
@SuppressWarnings("rawtypes")
final Observable[] os;
final Object[] observers;
final Observer<? super R> observer;
final FuncN<? extends R> zipFunction;
final CompositeSubscription childSubscription = new CompositeSubscription();
@SuppressWarnings("rawtypes")
public Zip(Observable[] os, final Subscriber<? super R> observer, FuncN<? extends R> zipFunction) {
this.os = os;
this.observer = observer;
this.zipFunction = zipFunction;
observers = new Object[os.length];
for (int i = 0; i < os.length; i++) {
InnerObserver io = new InnerObserver();
observers[i] = io;
childSubscription.add(io);
}
observer.add(childSubscription);
}
@SuppressWarnings("unchecked")
public void zip() {
for (int i = 0; i < os.length; i++) {
os[i].unsafeSubscribe((InnerObserver) observers[i]);
}
}
final AtomicLong counter = new AtomicLong(0);
/**
* check if we have values for each and emit if we do
*
* This will only allow one thread at a time to do the work, but ensures via `counter` increment/decrement
* that there is always once who acts on each `tick`. Same concept as used in OperationObserveOn.
*
*/
@SuppressWarnings("unchecked")
void tick() {
if (counter.getAndIncrement() == 0) {
do {
final Object[] vs = new Object[observers.length];
boolean allHaveValues = true;
for (int i = 0; i < observers.length; i++) {
Object n = ((InnerObserver) observers[i]).items.peek();
if (n == null) {
allHaveValues = false;
continue;
}
switch (on.kind(n)) {
case OnNext:
vs[i] = on.getValue(n);
break;
case OnCompleted:
observer.onCompleted();
// we need to unsubscribe from all children since children are
// independently subscribed
childSubscription.unsubscribe();
return;
}
}
if (allHaveValues) {
try {
// all have something so emit
observer.onNext(zipFunction.call(vs));
} catch (Throwable e) {
observer.onError(OnErrorThrowable.addValueAsLastCause(e, vs));
return;
}
// now remove them
for (int i = 0; i < observers.length; i++) {
((InnerObserver) observers[i]).items.poll();
// eagerly check if the next item on this queue is an onComplete
if (on.isCompleted(((InnerObserver) observers[i]).items.peek())) {
// it is an onComplete so shut down
observer.onCompleted();
// we need to unsubscribe from all children since children are independently subscribed
childSubscription.unsubscribe();
return;
}
}
}
} while (counter.decrementAndGet() > 0);
}
}
// used to observe each Observable we are zipping together
// it collects all items in an internal queue
@SuppressWarnings("rawtypes")
final class InnerObserver extends Subscriber {
// Concurrent* since we need to read it from across threads
final ConcurrentLinkedQueue items = new ConcurrentLinkedQueue();
@SuppressWarnings("unchecked")
@Override
public void onCompleted() {
items.add(on.completed());
tick();
}
@Override
public void onError(Throwable e) {
// emit error and shut down
observer.onError(e);
}
@SuppressWarnings("unchecked")
@Override
public void onNext(Object t) {
items.add(on.next(t));
tick();
}
};
}
}