-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBiObservable.java
More file actions
540 lines (495 loc) · 19.1 KB
/
BiObservable.java
File metadata and controls
540 lines (495 loc) · 19.1 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package rx;
import rx.Observable.OnSubscribe;
import rx.functions.Action1;
import rx.functions.Action2;
import rx.functions.Func1;
import rx.functions.Func2;
import rx.functions.Func3;
import rx.internal.operators.bi.OperatorBiToMonoMap;
import rx.internal.operators.bi.OperatorDoOnNext;
import rx.internal.operators.bi.OperatorFlip;
import rx.internal.operators.bi.OperatorGenerate;
import rx.internal.operators.bi.OperatorBiMap;
import rx.internal.operators.bi.OperatorScan;
import rx.internal.operators.bi.OperatorTakeLast;
import rx.operators.BiOperator;
import rx.operators.BiToSingleOperator;
import rx.operators.SingleToBiOperator;
public class BiObservable<T0, T1> {
private BiOnSubscribe<T0, T1> onSubscribeFunc;
/**
* An action used by a {@link BiObservable} to produce data to be consumed by a downstream
* {@link BiSubscriber}.
*
* @param <T0>
* type of first argument
* @param <T1>
* type of second argument
*/
public static interface BiOnSubscribe<T0, T1> extends Action1<BiSubscriber<? super T0, ? super T1>> {
@Override
public void call(BiSubscriber<? super T0, ? super T1> subscriber);
}
/**
* @param onSubscribeFunc
*/
private BiObservable(BiOnSubscribe<T0, T1> onSubscribeFunc) {
this.onSubscribeFunc = onSubscribeFunc;
}
/**
* @param onSubscribe
* @return a {@link BiObservable} wrapping the given {@link BiOnSubscribe} action.
*/
public static <T0, T1> BiObservable<T0, T1> create(BiOnSubscribe<T0, T1> onSubscribe) {
return new BiObservable<T0, T1>(onSubscribe);
}
/**
* @param subscriber
*/
public void subcribe(BiSubscriber<? super T0, ? super T1> subscriber) {
onSubscribeFunc.call(subscriber);
}
/**
* Create a new DyadObservable that defers the subscription of {@code this} with a
* {@link BiSubscriber subscriber} that applies the given operator's effect to values produced
* when subscribed to.
*
* @param biOperator
* a function to adapt the types and semantics of the downstream operator.
* @return a new {@link BiObservable} with a {@link BiOnSubscribe onSubscribeFunc} that
* subscribes to {@code this}.
* @see BiObservable#lift(BiToSingleOperator)
*/
public <R0, R1> BiObservable<R0, R1> lift(final BiOperator<? extends R0, ? extends R1, ? super T0, ? super T1> biOperator) {
return new BiObservable<R0, R1>(new BiOnSubscribe<R0, R1>() {
@Override
public void call(BiSubscriber<? super R0, ? super R1> child) {
onSubscribeFunc.call(biOperator.call(child));
}
});
}
/**
* Create a new {@link Observable} that defers the subscription of {@code this} with a
* {@link BiSubscriber subscriber} that applies the given operator's effect to values produced
* when subscribed to. This overload of {@code lift} converts a DyadObservable to a
* single-valued Observable.
*
* @param operator
* a function to adapt the types and semantics of the downstream operator.
* @return a new {@link BiObservable} with a {@link BiOnSubscribe onSubscribeFunc} that
* subscribes to {@code this}.
*/
public <R> Observable<R> lift(final BiToSingleOperator<? extends R, ? super T0, ? super T1> operator) {
return Observable.create(new OnSubscribe<R>() {
@Override
public void call(Subscriber<? super R> child) {
onSubscribeFunc.call(operator.call(child));
}
});
}
/**
* Create a new {@link Observable} that defers the subscription of {@code obs} with a
* {@link Subscriber subscriber} that applies the given operator's effect to values produced
* when subscribed to. This overload of {@code lift} converts a single-valued Observable to a
* two-valued {@link BiObservable}.
*
* @param obs
* the producer subscribed to.
* @param op
* a function to adapt the types and semantics of the downstream operator to
* {@code obs}.
* @return
*/
public static <R0, R1, T> BiObservable<R0, R1> lift(Observable<? extends T> obs, SingleToBiOperator<R0, R1, T> op) {
return new BiObservable<R0, R1>(new BiOnSubscribe<R0, R1>() {
@Override
public void call(BiSubscriber<? super R0, ? super R1> subscriber) {
obs.unsafeSubscribe(op.call(subscriber));
}
});
}
/**
* Converts an Observable to a BiObservable by applying a function to generate a second value
* based on the values produced by the {@code observable}.
*
* @param observable
* the producer
* @param generatorFunc
* ran once per call made to onNext to produce the paired BiObservable's second
* value
* @return a BiObservable encapsulating the subscription to the given {@code observable}
*/
public static <T0, T1> BiObservable<T0, T1> generate(final Observable<? extends T0> observable, final Func1<? super T0, ? extends T1> generatorFunc) {
return BiObservable.lift(observable, new OperatorGenerate<T0, T1>(generatorFunc));
}
/**
* Creates a BiObservable by zipping two observables. Each value produced by the returned
* BiObservable is the pair of each value emitted by the given observables.
*
* @param ob0
* the first observable
* @param ob1
* the second observable
* @return a BiObservable encapsulating the subscription to both observables
*/
public static <T0, T1> BiObservable<T0, T1> zip(final Observable<? extends T0> ob0, final Observable<? extends T1> ob1) {
return create(new BiOnSubscribe<T0, T1>() {
@Override
public void call(final BiSubscriber<? super T0, ? super T1> child) {
child.add(Observable.zip(ob0, ob1, new Func2<T0, T1, Void>() {
@Override
public Void call(T0 t0, T1 t1) {
child.onNext(t0, t1);
return null;
}
}).subscribe(new Observer<Void>() {
@Override
public void onCompleted() {
child.onComplete();
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onNext(Void t) {
}
}));
}
});
}
/**
* @param ob0
* @param ob1
* @return
*/
public static final <T0, T1> BiObservable<T0, T1> combineLatest(final Observable<? extends T0> ob0, final Observable<? extends T1> ob1) {
return create(new BiOnSubscribe<T0, T1>() {
@Override
public void call(final BiSubscriber<? super T0, ? super T1> child) {
child.add(Observable
.combineLatest(ob0, ob1, new Func2<T0, T1, Void>() {
@Override
public Void call(T0 t0, T1 t1) {
child.onNext(t0, t1);
return null;
}
}).subscribe(new Observer<Void>() {
@Override
public void onCompleted() {
child.onComplete();
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onNext(Void t) {
}
}));
}
});
}
/**
* Creates a BiObservable that emits one pair of elements for each combination of the elements
* emitted by the observables passed as parameters. This produces the Cartesian product of all
* emitted elements from two observables.
*
* @param ob0
* @param ob1
* @return a BiObservable that produces the Cartesian product of ob0 and ob1
*/
public static <T0, T1> BiObservable<T0, T1> product(final Observable<? extends T0> ob0, final Observable<? extends T1> ob1) {
if (ob1 == Observable.empty() || ob0 == Observable.empty())
return BiObservable.empty();
return create(new BiOnSubscribe<T0, T1>() {
@Override
public void call(final BiSubscriber<? super T0, ? super T1> child) {
child.add(ob0.flatMap(new Func1<T0, Observable<Void>>() {
@Override
public Observable<Void> call(final T0 t0) {
return ob1.map(new Func1<T1, Void>() {
@Override
public Void call(T1 t1) {
child.onNext(t0, t1);
return null;
}
});
}
}).subscribe(new Observer<Void>() {
@Override
public void onCompleted() {
child.onComplete();
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onNext(Void t) {
}
}));
}
});
}
public static <T0, T1> BiObservable<T0, T1> empty() {
return BiObservable.create(new BiOnSubscribe<T0, T1>(){
@Override
public void call(BiSubscriber<? super T0, ? super T1> subscriber) {
subscriber.onComplete();
}});
}
/**
* Creates a BiObservable from an observable and a non-deterministic-arity generator function.
* This emits a pair of each generatorFunc's output element with the input it was obtained from.
* Note that if the generatorFunc produces an "empty" observable then no pairs will be emitted
* for that input element.
*
* @param ob0
* @param generatorFunc
* @return a BiObservable that
*/
public static <T0, T1> BiObservable<T0, T1> sparseProduct(final Observable<? extends T0> ob0, final Func1<? super T0, Observable<T1>> generatorFunc) {
return create(new BiOnSubscribe<T0, T1>() {
@Override
public void call(final BiSubscriber<? super T0, ? super T1> subscriber) {
subscriber.add(ob0.flatMap(new Func1<T0, Observable<T1>>() {
@Override
public Observable<T1> call(final T0 t0) {
return generatorFunc.call(t0)
.doOnNext(new Action1<T1>() {
@Override
public void call(T1 t1) {
subscriber.onNext(t0, t1);
}
});
}
}).subscribe(new Observer<T1>() {
@Override
public void onCompleted() {
subscriber.onComplete();
}
@Override
public void onError(Throwable e) {
subscriber.onError(e);
}
@Override
public void onNext(T1 t) {
}
}));
}
});
}
/**
* Attaches an instance as the paired item to each element emitted from the observable.
*
* @param i0
* @param ob1
* @return
*/
public static <T0, T1> BiObservable<T0, T1> attach(T0 i0, Observable<? extends T1> ob1) {
return product(Observable.just(i0), ob1);
}
/**
* Attaches an instance as the paired item to each element emitted from the observable.
*
* @param ob0
* @param i1
* @return
*/
public static <T0, T1> BiObservable<T0, T1> attach(Observable<? extends T0> ob0, T1 i1) {
return product(ob0, Observable.just(i1));
}
/**
* Returns a BiObservable that applies a specified function to each pair of items emitted by
* the source BiObservable and emits the results of these function applications, replacing the
* second item with the results. This overload accepts a Func2 that will receive both items
* emitted by the BiObservable as arguments.
*
* @param func
* the function used to produce the new value.
* @return a BiObservable which transforms the first item emitted using the specified
* function.
*/
public <R> BiObservable<? extends R, ? extends T1> map1(final Func2<? super T0, ? super T1, ? extends R> func) {
return lift(OperatorBiMap.bi1Operator(func));
}
/**
* Returns a DyadObservable that applies a specified function to each pair of items emitted by
* the source DyadObservable and emits the results of these function applications, replacing the
* second item with the results. This overload accepts a Func1 that will receive the first item
* emitted by the DyadObservable as an argument.
*
* @param func
* the function used to produce the new value.
* @return a DyadObservable which transforms the first item emitted using the specified
* function.
*/
public <R> BiObservable<? extends R, ? extends T1> map1(final Func1<? super T0, ? extends R> func) {
return lift(OperatorBiMap.mono1Operator(func));
}
// for TriObservable we'll need many combinations of flatten
// <a,b,c> -> <r,a>,
// <a,b,c> -> <r,b>,
// <a,b,c> -> <r,c>,
// <a,b,c> -> <r>
//
// Quad
// <a,b,c,d> -> <r,a,b>
// <a,b,c,d> -> <r,a,c>
// <a,b,c,d> -> <r,a,d>
// <a,b,c,d> -> <r,b,c>
// <a,b,c,d> -> <r,b,d>
// <a,b,c,d> -> <r,c,d>
// <a,b,c,d> -> <r,a>
// <a,b,c,d> -> <r,b>
// <a,b,c,d> -> <r,c>
// <a,b,c,d> -> <r,d>
// <a,b,c,d> -> <r>
/**
* Returns a BiObservable that applies a specified function to each pair of items emitted by
* the source BiObservable and emits the results of these function applications, replacing the
* emitted values with the result from the specified function.
*
* @param func
* the function used to produce the new value.
* @return an Observable which transforms the pair of items emitted using the specified
* function.
*/
public <R> Observable<R> bimap(final Func2<? super T0, ? super T1, R> func) {
return lift(new OperatorBiToMonoMap<R, T0, T1>(func));
}
/**
* @param action
* @return
*/
public BiObservable<T0, T1> doOnNext(final Action2<? super T0, ? super T1> action) {
return lift(OperatorDoOnNext.biOperator(action));
}
/**
* @param action
* @return
*/
public BiObservable<T0, T1> doOnNext1(final Action1<? super T0> action) {
return lift(OperatorDoOnNext.mono1Operator(action));
}
/**
* @param action
* @return
*/
public BiObservable<T0, T1> doOnNext2(final Action1<? super T1> action) {
return lift(OperatorDoOnNext.mono2Operator(action));
}
/**
* @param seed
* @param func
* @return
*/
public <R> BiObservable<R, T1> scan1(R seed, final Func3<R, ? super T0, ? super T1, R> func) {
return lift(OperatorScan.bi1Operator(seed, func));
}
/**
* @param func
* @return
*/
public BiObservable<T0, T1> scan1(final Func3<T0, ? super T0, ? super T1, T0> func) {
return lift(OperatorScan.bi1Operator(func));
}
/**
* @param seed
* @param func
* @return
*/
public <R> BiObservable<T0, R> scan2(R seed, final Func3<R, ? super T0, ? super T1, R> func) {
return lift(OperatorScan.bi2Operator(seed, func));
}
/**
* @param func
* @return
*/
public BiObservable<T0, T1> scan2(final Func3<T1, ? super T0, ? super T1, T1> func) {
return lift(OperatorScan.bi2Operator(func));
}
/**
* @return
*/
public BiObservable<T0, T1> takeLast() {
return lift(new OperatorTakeLast<T0, T1>());
}
/**
* @param seed
* @param func
* @return
*/
public <R> BiObservable<R, T1> reduce1(R seed, final Func3<R, ? super T0, ? super T1, R> func) {
return scan1(seed, func).takeLast();
}
/**
* @param func
* @return
*/
public BiObservable<T0, T1> reduce1(final Func3<T0, ? super T0, ? super T1, T0> func) {
return scan1(func).takeLast();
}
/**
* @param seed
* @param func
* @return
*/
public <R> BiObservable<T0, R> reduce2(R seed, final Func3<R, ? super T0, ? super T1, R> func) {
return scan2(seed, func).takeLast();
}
/**
* @param func
* @return
*/
public BiObservable<T0, T1> reduce2(final Func3<T1, ? super T0, ? super T1, T1> func) {
return scan2(func).takeLast();
}
/**
* Returns a BiObservable that applies a specified function to each pair of items emitted by
* the source BiObservable and emits the results of these function applications, replacing the
* second item with the results. This overload accepts a Func2 that will receive both items
* emitted by the BiObservable as arguments.
*
* @param func
* the function used to produce the new value.
* @return a BiObservable which transforms the second item emitted using the specified
* function.
*/
public <R> BiObservable<T0, R> map2(Func2<? super T0, ? super T1, ? extends R> func) {
return lift(OperatorBiMap.bi2Operator(func));
}
/**
* Returns a BiObservable that applies a specified function to each pair of items emitted by
* the source BiObservable and emits the results of these function applications, replacing the
* second item with the results. This overload accepts a Func1 that will receive the second item
* emitted by the BiObservable as an argument.
*
* @param func
* the function used to produce the new value.
* @return a BiObservable which transforms the second item emitted using the specified
* function.
*/
public <R> BiObservable<T0, R> map2(final Func1<? super T1, ? extends R> func) {
return lift(OperatorBiMap.mono2Operator(func));
}
/**
* @param func
* @return
*/
public static <T0, T1, R> Func2<T1, T0, R> flip(final Func2<? super T0, ? super T1, ? extends R> func) {
return new Func2<T1, T0, R>() {
@Override
public R call(T1 t1, T0 t0) {
return func.call(t0, t1);
}
};
}
/**
* @return
*/
public BiObservable<? extends T1, ? extends T0> flip() {
return lift(new OperatorFlip<T0, T1>());
}
}