forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperationZip.java
More file actions
461 lines (409 loc) · 17.9 KB
/
OperationZip.java
File metadata and controls
461 lines (409 loc) · 17.9 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/**
* 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.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.SerialSubscription;
import rx.subscriptions.Subscriptions;
import rx.util.functions.Func2;
import rx.util.functions.Func3;
import rx.util.functions.Func4;
import rx.util.functions.Func5;
import rx.util.functions.Func6;
import rx.util.functions.Func7;
import rx.util.functions.Func8;
import rx.util.functions.Func9;
import rx.util.functions.FuncN;
import rx.util.functions.Functions;
/**
* 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 OperationZip {
@SuppressWarnings("unchecked")
public static <T1, T2, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, final Func2<? super T1, ? super T2, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, final Func3<? super T1, ? super T2, ? super T3, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, T4, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, final Func4<? super T1, ? super T2, ? super T3, ? super T4, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3, o4), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, T4, T5, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, final Func5<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3, o4, o5), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, T4, T5, T6, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6,
final Func6<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3, o4, o5, o6), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, T4, T5, T6, T7, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7,
final Func7<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3, o4, o5, o6, o7), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, T4, T5, T6, T7, T8, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Observable<? extends T8> o8,
final Func8<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3, o4, o5, o6, o7, o8), Functions.fromFunc(zipFunction));
}
@SuppressWarnings("unchecked")
public static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> OnSubscribeFunc<R> zip(Observable<? extends T1> o1, Observable<? extends T2> o2, Observable<? extends T3> o3, Observable<? extends T4> o4, Observable<? extends T5> o5, Observable<? extends T6> o6, Observable<? extends T7> o7, Observable<? extends T8> o8,
Observable<? extends T9> o9, final Func9<? super T1, ? super T2, ? super T3, ? super T4, ? super T5, ? super T6, ? super T7, ? super T8, ? super T9, ? extends R> zipFunction) {
return zip(Arrays.asList(o1, o2, o3, o4, o5, o6, o7, o8, o9), Functions.fromFunc(zipFunction));
}
public static <R> OnSubscribeFunc<R> zip(Iterable<? extends Observable<?>> ws, final FuncN<? extends R> zipFunction) {
ManyObservables<?, R> a = new ManyObservables<Object, R>(ws, zipFunction);
return a;
}
/**
* Merges the values across multiple sources and applies the selector
* function.
* <p>The resulting sequence terminates if no more pairs can be
* established, i.e., streams of length 1 and 2 zipped will produce
* only 1 item.</p>
* <p>Exception semantics: errors from the source observable are
* propagated as-is.</p>
*
* @param <T>
* the common element type
* @param <U>
* the result element type
*/
private static final class ManyObservables<T, U> implements OnSubscribeFunc<U> {
/** */
protected final Iterable<? extends Observable<? extends T>> sources;
/** */
protected final FuncN<? extends U> selector;
/**
* Constructor.
*
* @param sources
* the sources
* @param selector
* the result selector
*/
public ManyObservables(
Iterable<? extends Observable<? extends T>> sources,
FuncN<? extends U> selector) {
this.sources = sources;
this.selector = selector;
}
@Override
public Subscription onSubscribe(final Observer<? super U> observer) {
final CompositeSubscription composite = new CompositeSubscription();
final ReadWriteLock rwLock = new ReentrantReadWriteLock(true);
final List<ItemObserver<T>> all = new ArrayList<ItemObserver<T>>();
Observer<List<T>> o2 = new Observer<List<T>>() {
boolean done;
@Override
public void onCompleted() {
if (!done) {
done = true;
observer.onCompleted();
}
}
@Override
public void onError(Throwable t) {
if (!done) {
done = true;
observer.onError(t);
}
}
@Override
public void onNext(List<T> value) {
if (!done) {
observer.onNext(selector.call(value.toArray(new Object[value.size()])));
}
}
};
for (Observable<? extends T> o : sources) {
ItemObserver<T> io = new ItemObserver<T>(
rwLock, all, o, o2, composite);
composite.add(io);
all.add(io);
}
for (ItemObserver<T> io : all) {
io.connect();
}
return composite;
}
/**
* The individual line's observer.
*
* @author akarnokd, 2013.01.14.
* @param <T>
* the element type
*/
private static final class ItemObserver<T> implements Observer<T>, Subscription {
/** Reader-writer lock. */
protected final ReadWriteLock rwLock;
/** The queue. */
public final Queue<Object> queue = new LinkedList<Object>();
/** The list of the other observers. */
public final List<ItemObserver<T>> all;
/** The null sentinel value. */
protected static final Object NULL_SENTINEL = new Object();
/** The global cancel. */
protected final Subscription cancel;
/** The subscription to the source. */
protected final SerialSubscription toSource = new SerialSubscription();
/** Indicate completion of this stream. */
protected boolean done;
/** The source. */
protected final Observable<? extends T> source;
/** The observer. */
protected final Observer<? super List<T>> observer;
/**
* Constructor.
*
* @param rwLock
* the reader-writer lock to use
* @param all
* all observers
* @param source
* the source sequence
* @param observer
* the output observer
* @param cancel
* the cancellation handler
*/
public ItemObserver(
ReadWriteLock rwLock,
List<ItemObserver<T>> all,
Observable<? extends T> source,
Observer<? super List<T>> observer,
Subscription cancel) {
this.rwLock = rwLock;
this.all = all;
this.source = source;
this.observer = observer;
this.cancel = cancel;
}
@SuppressWarnings("unchecked")
@Override
public void onNext(T value) {
rwLock.readLock().lock();
try {
if (done) {
return;
}
queue.add(value != null ? value : NULL_SENTINEL);
} finally {
rwLock.readLock().unlock();
}
// run collector
if (rwLock.writeLock().tryLock()) {
boolean cu = false;
try {
while (true) {
List<T> values = new ArrayList<T>(all.size());
for (ItemObserver<T> io : all) {
if (io.queue.isEmpty()) {
if (io.done) {
observer.onCompleted();
cu = true;
return;
}
continue;
}
Object v = io.queue.peek();
if (v == NULL_SENTINEL) {
v = null;
}
values.add((T) v);
}
if (values.size() == all.size()) {
for (ItemObserver<T> io : all) {
io.queue.poll();
}
observer.onNext(values);
} else {
break;
}
}
} finally {
rwLock.writeLock().unlock();
if (cu) {
cancel.unsubscribe();
}
}
}
}
@Override
public void onError(Throwable ex) {
rwLock.writeLock().lock();
try {
if (done) {
return;
}
done = true;
observer.onError(ex);
} finally {
rwLock.writeLock().unlock();
}
cancel.unsubscribe();
unsubscribe();
}
@Override
public void onCompleted() {
rwLock.readLock().lock();
try {
done = true;
} finally {
rwLock.readLock().unlock();
}
if (rwLock.writeLock().tryLock()) {
boolean cu = false;
try {
for (ItemObserver<T> io : all) {
if (io.queue.isEmpty() && io.done) {
observer.onCompleted();
cu = true;
return;
}
}
} finally {
rwLock.writeLock().unlock();
if (cu) {
cancel.unsubscribe();
}
}
}
unsubscribe();
}
/** Connect to the source observable. */
public void connect() {
toSource.set(source.subscribe(this));
}
@Override
public void unsubscribe() {
toSource.unsubscribe();
}
}
}
/**
* Zips an Observable and an iterable sequence and applies
* a function to the pair of values.
*/
public static <T, U, R> OnSubscribeFunc<R> zipIterable(Observable<? extends T> source, Iterable<? extends U> other, Func2<? super T, ? super U, ? extends R> zipFunction) {
return new ZipIterable<T, U, R>(source, other, zipFunction);
}
/**
* Zips an Observable and an iterable sequence and applies
* a function to the pair of values.
*/
private static final class ZipIterable<T, U, R> implements OnSubscribeFunc<R> {
final Observable<? extends T> source;
final Iterable<? extends U> other;
final Func2<? super T, ? super U, ? extends R> zipFunction;
public ZipIterable(Observable<? extends T> source, Iterable<? extends U> other, Func2<? super T, ? super U, ? extends R> zipFunction) {
this.source = source;
this.other = other;
this.zipFunction = zipFunction;
}
@Override
public Subscription onSubscribe(Observer<? super R> t1) {
Iterator<? extends U> it;
boolean first;
try {
it = other.iterator();
first = it.hasNext();
} catch (Throwable t) {
t1.onError(t);
return Subscriptions.empty();
}
if (!first) {
t1.onCompleted();
return Subscriptions.empty();
}
SerialSubscription ssub = new SerialSubscription();
ssub.set(source.subscribe(new SourceObserver<T, U, R>(t1, it, zipFunction, ssub)));
return ssub;
}
/** Observe the source. */
private static final class SourceObserver<T, U, R> implements Observer<T> {
final Observer<? super R> observer;
final Iterator<? extends U> other;
final Func2<? super T, ? super U, ? extends R> zipFunction;
final Subscription cancel;
public SourceObserver(Observer<? super R> observer, Iterator<? extends U> other,
Func2<? super T, ? super U, ? extends R> zipFunction, Subscription cancel) {
this.observer = observer;
this.other = other;
this.zipFunction = zipFunction;
this.cancel = cancel;
}
@Override
public void onNext(T args) {
U u = other.next();
R r;
try {
r = zipFunction.call(args, u);
} catch (Throwable t) {
onError(t);
return;
}
observer.onNext(r);
boolean has;
try {
has = other.hasNext();
} catch (Throwable t) {
onError(t);
return;
}
if (!has) {
onCompleted();
}
}
@Override
public void onError(Throwable e) {
observer.onError(e);
cancel.unsubscribe();
}
@Override
public void onCompleted() {
observer.onCompleted();
cancel.unsubscribe();
}
}
}
}