/
Observable.java
2635 lines (2407 loc) · 118 KB
/
Observable.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2013 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;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import rx.operators.OperationConcat;
import rx.operators.OperationFilter;
import rx.operators.OperationForEach;
import rx.operators.OperationLast;
import rx.operators.OperationMap;
import rx.operators.OperationMaterialize;
import rx.operators.OperationMerge;
import rx.operators.OperationMergeDelayError;
import rx.operators.OperationOnErrorResumeNextViaFunction;
import rx.operators.OperationOnErrorResumeNextViaObservable;
import rx.operators.OperationOnErrorReturn;
import rx.operators.OperationScan;
import rx.operators.OperationSkip;
import rx.operators.OperationSynchronize;
import rx.operators.OperationTake;
import rx.operators.OperationTakeLast;
import rx.operators.OperationToObservableFuture;
import rx.operators.OperationToObservableIterable;
import rx.operators.OperationToObservableList;
import rx.operators.OperationToObservableSortedList;
import rx.operators.OperationZip;
import rx.plugins.RxJavaErrorHandler;
import rx.plugins.RxJavaPlugins;
import rx.util.AtomicObservableSubscription;
import rx.util.AtomicObserver;
import rx.util.Range;
import rx.util.functions.Action0;
import rx.util.functions.Action1;
import rx.util.functions.Func1;
import rx.util.functions.Func2;
import rx.util.functions.Func3;
import rx.util.functions.Func4;
import rx.util.functions.FuncN;
import rx.util.functions.FunctionLanguageAdaptor;
import rx.util.functions.Functions;
/**
* The Observable interface that implements the Reactive Pattern.
* <p>
* It provides overloaded methods for subscribing as well as delegate methods to the various operators.
* <p>
* The documentation for this interface makes use of marble diagrams. The following legend explains
* these diagrams:
* <p>
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/legend.png">
* <p>
* For more information see the <a href="https://github.com/Netflix/RxJava/wiki/Observable">RxJava Wiki</a>
*
* @param <T>
*/
public class Observable<T> {
private final Func1<Observer<T>, Subscription> onSubscribe;
private final boolean isTrusted;
protected Observable(Func1<Observer<T>, Subscription> onSubscribe) {
this(onSubscribe, false);
}
protected Observable() {
this(null, false);
}
private Observable(Func1<Observer<T>, Subscription> onSubscribe, boolean isTrusted) {
this.onSubscribe = onSubscribe;
this.isTrusted = isTrusted;
}
/**
* an {@link Observer} must call an Observable's <code>subscribe</code> method in order to register itself
* to receive push-based notifications from the Observable. A typical implementation of the
* <code>subscribe</code> method does the following:
* <p>
* It stores a reference to the Observer in a collection object, such as a <code>List<T></code>
* object.
* <p>
* It returns a reference to the {@link Subscription} interface. This enables
* Observers to unsubscribe (that is, to stop receiving notifications) before the Observable has
* finished sending them and has called the Observer's {@link Observer#onCompleted()} method.
* <p>
* At any given time, a particular instance of an <code>Observable<T></code> implementation is
* responsible for accepting all subscriptions and notifying all subscribers. Unless the
* documentation for a particular <code>Observable<T></code> implementation indicates otherwise,
* Observers should make no assumptions about the <code>Observable<T></code> implementation, such
* as the order of notifications that multiple Observers will receive.
* <p>
* For more information see the <a href="https://github.com/Netflix/RxJava/wiki/Observable">RxJava Wiki</a>
*
*
* @param Observer
* @return a {@link Subscription} reference that allows observers
* to stop receiving notifications before the provider has finished sending them
*/
public Subscription subscribe(Observer<T> observer) {
if (onSubscribe == null) {
throw new IllegalStateException("onSubscribe function can not be null.");
// the subscribe function can also be overridden but generally that's not the appropriate approach so I won't mention that in the exception
}
if (isTrusted) {
return onSubscribe.call(observer);
} else {
AtomicObservableSubscription subscription = new AtomicObservableSubscription();
return subscription.wrap(onSubscribe.call(new AtomicObserver<T>(subscription, observer)));
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Subscription subscribe(final Map<String, Object> callbacks) {
// lookup and memoize onNext
Object _onNext = callbacks.get("onNext");
if (_onNext == null) {
throw new RuntimeException("onNext must be implemented");
}
final FuncN onNext = Functions.from(_onNext);
return subscribe(new Observer() {
public void onCompleted() {
Object onComplete = callbacks.get("onCompleted");
if (onComplete != null) {
Functions.from(onComplete).call();
}
}
public void onError(Exception e) {
handleError(e);
Object onError = callbacks.get("onError");
if (onError != null) {
Functions.from(onError).call(e);
}
}
public void onNext(Object args) {
onNext.call(args);
}
});
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Subscription subscribe(final Object o) {
if (o instanceof Observer) {
// in case a dynamic language is not correctly handling the overloaded methods and we receive an Observer just forward to the correct method.
return subscribe((Observer) o);
}
// lookup and memoize onNext
if (o == null) {
throw new RuntimeException("onNext must be implemented");
}
final FuncN onNext = Functions.from(o);
return subscribe(new Observer() {
public void onCompleted() {
// do nothing
}
public void onError(Exception e) {
handleError(e);
// no callback defined
}
public void onNext(Object args) {
onNext.call(args);
}
});
}
public Subscription subscribe(final Action1<T> onNext) {
return subscribe(new Observer<T>() {
public void onCompleted() {
// do nothing
}
public void onError(Exception e) {
handleError(e);
// no callback defined
}
public void onNext(T args) {
if (onNext == null) {
throw new RuntimeException("onNext must be implemented");
}
onNext.call(args);
}
});
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Subscription subscribe(final Object onNext, final Object onError) {
// lookup and memoize onNext
if (onNext == null) {
throw new RuntimeException("onNext must be implemented");
}
final FuncN onNextFunction = Functions.from(onNext);
return subscribe(new Observer() {
public void onCompleted() {
// do nothing
}
public void onError(Exception e) {
handleError(e);
if (onError != null) {
Functions.from(onError).call(e);
}
}
public void onNext(Object args) {
onNextFunction.call(args);
}
});
}
public Subscription subscribe(final Action1<T> onNext, final Action1<Exception> onError) {
return subscribe(new Observer<T>() {
public void onCompleted() {
// do nothing
}
public void onError(Exception e) {
handleError(e);
if (onError != null) {
onError.call(e);
}
}
public void onNext(T args) {
if (onNext == null) {
throw new RuntimeException("onNext must be implemented");
}
onNext.call(args);
}
});
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public Subscription subscribe(final Object onNext, final Object onError, final Object onComplete) {
// lookup and memoize onNext
if (onNext == null) {
throw new RuntimeException("onNext must be implemented");
}
final FuncN onNextFunction = Functions.from(onNext);
return subscribe(new Observer() {
public void onCompleted() {
if (onComplete != null) {
Functions.from(onComplete).call();
}
}
public void onError(Exception e) {
handleError(e);
if (onError != null) {
Functions.from(onError).call(e);
}
}
public void onNext(Object args) {
onNextFunction.call(args);
}
});
}
public Subscription subscribe(final Action1<T> onNext, final Action1<Exception> onError, final Action0 onComplete) {
return subscribe(new Observer<T>() {
public void onCompleted() {
onComplete.call();
}
public void onError(Exception e) {
handleError(e);
if (onError != null) {
onError.call(e);
}
}
public void onNext(T args) {
if (onNext == null) {
throw new RuntimeException("onNext must be implemented");
}
onNext.call(args);
}
});
}
/**
* Allow the {@link RxJavaErrorHandler} to receive the exception from onError.
*
* @param e
*/
private void handleError(Exception e) {
// onError should be rare so we'll only fetch when needed
RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
}
/**
* An Observable that never sends any information to an {@link Observer}.
*
* This Observable is useful primarily for testing purposes.
*
* @param <T>
* the type of item emitted by the Observable
*/
private static class NeverObservable<T> extends Observable<T> {
public NeverObservable() {
super(new Func1<Observer<T>, Subscription>() {
@Override
public Subscription call(Observer<T> t1) {
return new NoOpObservableSubscription();
}
});
}
}
/**
* A {@link Subscription} that does nothing when its unsubscribe method is called.
*/
private static class NoOpObservableSubscription implements Subscription {
public void unsubscribe() {
}
}
/**
* an Observable that calls {@link Observer#onError(Exception)} when the Observer subscribes.
*
* @param <T>
* the type of object returned by the Observable
*/
private static class ThrowObservable<T> extends Observable<T> {
public ThrowObservable(final Exception exception) {
super(new Func1<Observer<T>, Subscription>() {
/**
* Accepts an {@link Observer} and calls its <code>onError</code> method.
*
* @param observer
* an {@link Observer} of this Observable
* @return a reference to the subscription
*/
@Override
public Subscription call(Observer<T> observer) {
observer.onError(exception);
return new NoOpObservableSubscription();
}
});
}
}
/**
* Creates an Observable that will execute the given function when a {@link Observer} subscribes to it.
* <p>
* Write the function you pass to <code>create</code> so that it behaves as an Observable - calling the passed-in
* <code>onNext</code>, <code>onError</code>, and <code>onCompleted</code> methods appropriately.
* <p>
* A well-formed Observable must call either the {@link Observer}'s <code>onCompleted</code> method exactly once or its <code>onError</code> method exactly once.
* <p>
* See <a href="http://go.microsoft.com/fwlink/?LinkID=205219">Rx Design Guidelines (PDF)</a> for detailed information.
*
* @param <T>
* the type emitted by the Observable sequence
* @param func
* a function that accepts an <code>Observer<T></code> and calls its <code>onNext</code>, <code>onError</code>, and <code>onCompleted</code> methods
* as appropriate, and returns a {@link Subscription} to allow canceling the subscription (if applicable)
* @return an Observable that, when an {@link Observer} subscribes to it, will execute the given function
*/
public static <T> Observable<T> create(Func1<Observer<T>, Subscription> func) {
return new Observable<T>(func);
}
/*
* Private version that creates a 'trusted' Observable to allow performance optimizations.
*/
private static <T> Observable<T> _create(Func1<Observer<T>, Subscription> func) {
return new Observable<T>(func, true);
}
/**
* Creates an Observable that will execute the given function when a {@link Observer} subscribes to it.
* <p>
* This method accept {@link Object} to allow different languages to pass in closures using {@link FunctionLanguageAdaptor}.
* <p>
* Write the function you pass to <code>create</code> so that it behaves as an Observable - calling the passed-in
* <code>onNext</code>, <code>onError</code>, and <code>onCompleted</code> methods appropriately.
* <p>
* A well-formed Observable must call either the {@link Observer}'s <code>onCompleted</code> method exactly once or its <code>onError</code> method exactly once.
* <p>
* See <a href="http://go.microsoft.com/fwlink/?LinkID=205219">Rx Design Guidelines (PDF)</a> for detailed information.
*
* @param <T>
* the type emitted by the Observable sequence
* @param func
* a function that accepts an <code>Observer<T></code> and calls its <code>onNext</code>, <code>onError</code>, and <code>onCompleted</code> methods
* as appropriate, and returns a {@link Subscription} to allow canceling the subscription (if applicable)
* @return an Observable that, when an {@link Observer} subscribes to it, will execute the given function
*/
public static <T> Observable<T> create(final Object callback) {
@SuppressWarnings("rawtypes")
final FuncN _f = Functions.from(callback);
return create(new Func1<Observer<T>, Subscription>() {
@Override
public Subscription call(Observer<T> t1) {
return (Subscription) _f.call(t1);
}
});
}
/**
* Returns an Observable that returns no data to the {@link Observer} and immediately invokes its <code>onCompleted</code> method.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/empty.png">
*
* @param <T>
* the type of item emitted by the Observable
* @return an Observable that returns no data to the {@link Observer} and immediately invokes the {@link Observer}'s <code>onCompleted</code> method
*/
public static <T> Observable<T> empty() {
return toObservable(new ArrayList<T>());
}
/**
* Returns an Observable that calls <code>onError</code> when an {@link Observer} subscribes to it.
* <p>
*
* @param exception
* the error to throw
* @param <T>
* the type of object returned by the Observable
* @return an Observable object that calls <code>onError</code> when an {@link Observer} subscribes
*/
public static <T> Observable<T> error(Exception exception) {
return new ThrowObservable<T>(exception);
}
/**
* Filters an Observable by discarding any of its emissions that do not meet some test.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/filter.png">
*
* @param that
* the Observable to filter
* @param predicate
* a function that evaluates the items emitted by the source Observable, returning <code>true</code> if they pass the filter
* @return an Observable that emits only those items in the original Observable that the filter evaluates as true
*/
public static <T> Observable<T> filter(Observable<T> that, Func1<T, Boolean> predicate) {
return _create(OperationFilter.filter(that, predicate));
}
/**
* Filters an Observable by discarding any of its emissions that do not meet some test.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/filter.png">
*
* @param that
* the Observable to filter
* @param predicate
* a function that evaluates the items emitted by the source Observable, returning <code>true</code> if they pass the filter
* @return an Observable that emits only those items in the original Observable that the filter evaluates as true
*/
public static <T> Observable<T> filter(Observable<T> that, final Object function) {
@SuppressWarnings("rawtypes")
final FuncN _f = Functions.from(function);
return filter(that, new Func1<T, Boolean>() {
@Override
public Boolean call(T t1) {
return (Boolean) _f.call(t1);
}
});
}
/**
* Converts an {@link Iterable} sequence to an Observable sequence.
*
* @param iterable
* the source {@link Iterable} sequence
* @param <T>
* the type of items in the {@link Iterable} sequence and the type emitted by the resulting Observable
* @return an Observable that emits each item in the source {@link Iterable} sequence
* @see {@link #toObservable(Iterable)}
*/
public static <T> Observable<T> from(Iterable<T> iterable) {
return toObservable(iterable);
}
/**
* Converts an Array to an Observable sequence.
*
* @param iterable
* the source Array
* @param <T>
* the type of items in the Array, and the type of items emitted by the resulting Observable
* @return an Observable that emits each item in the source Array
* @see {@link #toObservable(Object...)}
*/
public static <T> Observable<T> from(T... items) {
return toObservable(items);
}
/**
* Generates an observable sequence of integral numbers within a specified range.
*
* @param start The value of the first integer in the sequence
* @param count The number of sequential integers to generate.
*
* @return An observable sequence that contains a range of sequential integral numbers.
*
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229460(v=vs.103).aspx">Observable.Range Method (Int32, Int32)</a>
*/
public static Observable<Integer> range(int start, int count) {
return from(Range.createWithCount(start, count));
}
/**
* Returns an Observable that notifies an {@link Observer} of a single value and then completes.
* <p>
* To convert any object into an Observable that emits that object, pass that object into the <code>just</code> method.
* <p>
* This is similar to the {@link toObservable} method, except that <code>toObservable</code> will convert
* an {@link Iterable} object into an Observable that emits each of the items in the {@link Iterable}, one
* at a time, while the <code>just</code> method would convert the {@link Iterable} into an Observable
* that emits the entire {@link Iterable} as a single item.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/just.png">
*
* @param value
* the value to pass to the Observer's <code>onNext</code> method
* @param <T>
* the type of the value
* @return an Observable that notifies an {@link Observer} of a single value and then completes
*/
public static <T> Observable<T> just(T value) {
List<T> list = new ArrayList<T>();
list.add(value);
return toObservable(list);
}
/**
* Takes the last item emitted by a source Observable and returns an Observable that emits only
* that item as its sole emission.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/last.png">
*
* @param that
* the source Observable
* @return an Observable that emits a single item, which is identical to the last item emitted
* by the source Observable
*/
public static <T> Observable<T> last(final Observable<T> that) {
return _create(OperationLast.last(that));
}
/**
* Applies a function of your choosing to every notification emitted by an Observable, and returns
* this transformation as a new Observable sequence.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/map.png">
*
* @param sequence
* the source Observable
* @param func
* a function to apply to each item in the sequence emitted by the source Observable
* @param <T>
* the type of items emitted by the the source Observable
* @param <R>
* the type of items returned by map function
* @return an Observable that is the result of applying the transformation function to each item
* in the sequence emitted by the source Observable
*/
public static <T, R> Observable<R> map(Observable<T> sequence, Func1<T, R> func) {
return _create(OperationMap.map(sequence, func));
}
/**
* Applies a function of your choosing to every notification emitted by an Observable, and returns
* this transformation as a new Observable sequence.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/map.png">
*
* @param sequence
* the source Observable
* @param func
* a function to apply to each item in the sequence emitted by the source Observable
* @param <T>
* the type of items emitted by the the source Observable
* @param <R>
* the type of items returned by map function
* @return an Observable that is the result of applying the transformation function to each item
* in the sequence emitted by the source Observable
*/
public static <T, R> Observable<R> map(Observable<T> sequence, final Object function) {
@SuppressWarnings("rawtypes")
final FuncN _f = Functions.from(function);
return map(sequence, new Func1<T, R>() {
@SuppressWarnings("unchecked")
@Override
public R call(T t1) {
return (R) _f.call(t1);
}
});
}
/**
* Creates a new Observable sequence by applying a function that you supply to each object in the
* original Observable sequence, where that function is itself an Observable that emits objects,
* and then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param sequence
* the source Observable
* @param func
* a function to apply to each item emitted by the source Observable, generating a
* Observable
* @param <T>
* the type emitted by the source Observable
* @param <R>
* the type emitted by the Observables emitted by <code>func</code>
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item emitted by the source Observable and merging the results of
* the Observables obtained from this transformation
*/
public static <T, R> Observable<R> mapMany(Observable<T> sequence, Func1<T, Observable<R>> func) {
return _create(OperationMap.mapMany(sequence, func));
}
/**
* Creates a new Observable sequence by applying a function that you supply to each object in the
* original Observable sequence, where that function is itself an Observable that emits objects,
* and then merges the results of that function applied to every item emitted by the original
* Observable, emitting these merged results as its own sequence.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mapMany.png">
*
* @param sequence
* the source Observable
* @param func
* a function to apply to each item emitted by the source Observable, generating a
* Observable
* @param <T>
* the type emitted by the source Observable
* @param <R>
* the type emitted by the Observables emitted by <code>func</code>
* @return an Observable that emits a sequence that is the result of applying the transformation
* function to each item emitted by the source Observable and merging the results of
* the Observables obtained from this transformation
*/
public static <T, R> Observable<R> mapMany(Observable<T> sequence, final Object function) {
@SuppressWarnings("rawtypes")
final FuncN _f = Functions.from(function);
return mapMany(sequence, new Func1<T, R>() {
@SuppressWarnings("unchecked")
@Override
public R call(T t1) {
return (R) _f.call(t1);
}
});
}
/**
* Materializes the implicit notifications of an observable sequence as explicit notification values.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/materialize.png">
*
* @param source
* An observable sequence of elements to project.
* @return An observable sequence whose elements are the result of materializing the notifications of the given sequence.
* @see http://msdn.microsoft.com/en-us/library/hh229453(v=VS.103).aspx
*/
public static <T> Observable<Notification<T>> materialize(final Observable<T> sequence) {
return _create(OperationMaterialize.materialize(sequence));
}
/**
* Flattens the Observable sequences from a list of Observables into one Observable sequence
* without any transformation. You can combine the output of multiple Observables so that they
* act like a single Observable, by using the <code>merge</code> method.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/merge.png">
*
* @param source
* a list of Observables that emit sequences of items
* @return an Observable that emits a sequence of elements that are the result of flattening the
* output from the <code>source</code> list of Observables
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
*/
public static <T> Observable<T> merge(List<Observable<T>> source) {
return _create(OperationMerge.merge(source));
}
/**
* Flattens the Observable sequences emitted by a sequence of Observables that are emitted by a
* Observable into one Observable sequence without any transformation. You can combine the output
* of multiple Observables so that they act like a single Observable, by using the <code>merge</code> method.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/merge.png">
*
* @param source
* an Observable that emits Observables
* @return an Observable that emits a sequence of elements that are the result of flattening the
* output from the Observables emitted by the <code>source</code> Observable
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
*/
public static <T> Observable<T> merge(Observable<Observable<T>> source) {
return _create(OperationMerge.merge(source));
}
/**
* Flattens the Observable sequences from a series of Observables into one Observable sequence
* without any transformation. You can combine the output of multiple Observables so that they
* act like a single Observable, by using the <code>merge</code> method.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/merge.png">
*
* @param source
* a series of Observables that emit sequences of items
* @return an Observable that emits a sequence of elements that are the result of flattening the
* output from the <code>source</code> Observables
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
*/
public static <T> Observable<T> merge(Observable<T>... source) {
return _create(OperationMerge.merge(source));
}
/**
* Combines the objects emitted by two or more Observables, and emits the result as a single Observable,
* by using the <code>concat</code> method.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/concat.png">
*
* @param source
* a series of Observables that emit sequences of items
* @return an Observable that emits a sequence of elements that are the result of combining the
* output from the <code>source</code> Observables
* @see <a href="http://msdn.microsoft.com/en-us/library/system.reactive.linq.observable.concat(v=vs.103).aspx">MSDN: Observable.Concat Method</a>
*/
public static <T> Observable<T> concat(Observable<T>... source) {
return _create(OperationConcat.concat(source));
}
/**
* Same functionality as <code>merge</code> except that errors received to onError will be held until all sequences have finished (onComplete/onError) before sending the error.
* <p>
* Only the first onError received will be sent.
* <p>
* This enables receiving all successes from merged sequences without one onError from one sequence causing all onNext calls to be prevented.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeDelayError.png">
*
* @param source
* a list of Observables that emit sequences of items
* @return an Observable that emits a sequence of elements that are the result of flattening the
* output from the <code>source</code> list of Observables
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
*/
public static <T> Observable<T> mergeDelayError(List<Observable<T>> source) {
return _create(OperationMergeDelayError.mergeDelayError(source));
}
/**
* Same functionality as <code>merge</code> except that errors received to onError will be held until all sequences have finished (onComplete/onError) before sending the error.
* <p>
* Only the first onError received will be sent.
* <p>
* This enables receiving all successes from merged sequences without one onError from one sequence causing all onNext calls to be prevented.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeDelayError.png">
*
* @param source
* an Observable that emits Observables
* @return an Observable that emits a sequence of elements that are the result of flattening the
* output from the Observables emitted by the <code>source</code> Observable
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
*/
public static <T> Observable<T> mergeDelayError(Observable<Observable<T>> source) {
return _create(OperationMergeDelayError.mergeDelayError(source));
}
/**
* Same functionality as <code>merge</code> except that errors received to onError will be held until all sequences have finished (onComplete/onError) before sending the error.
* <p>
* Only the first onError received will be sent.
* <p>
* This enables receiving all successes from merged sequences without one onError from one sequence causing all onNext calls to be prevented.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/mergeDelayError.png">
*
* @param source
* a series of Observables that emit sequences of items
* @return an Observable that emits a sequence of elements that are the result of flattening the
* output from the <code>source</code> Observables
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229099(v=vs.103).aspx">MSDN: Observable.Merge Method</a>
*/
public static <T> Observable<T> mergeDelayError(Observable<T>... source) {
return _create(OperationMergeDelayError.mergeDelayError(source));
}
/**
* Returns an Observable that never sends any information to an {@link Observer}.
*
* This observable is useful primarily for testing purposes.
*
* @param <T>
* the type of item (not) emitted by the Observable
* @return an Observable that never sends any information to an {@link Observer}
*/
public static <T> Observable<T> never() {
return new NeverObservable<T>();
}
/**
* A {@link Subscription} that does nothing.
*
* //TODO should this be moved to a Subscriptions utility class?
*
* @return
*/
public static Subscription noOpSubscription() {
return new NoOpObservableSubscription();
}
/**
* A {@link Subscription} implemented via a Func
*
* //TODO should this be moved to a Subscriptions utility class?
*
* @return
*/
public static Subscription createSubscription(final Action0 unsubscribe) {
return new Subscription() {
@Override
public void unsubscribe() {
unsubscribe.call();
}
};
}
/**
* A {@link Subscription} implemented via an anonymous function (such as closures from other languages).
*
* //TODO should this be moved to a Subscriptions utility class?
*
* @return
*/
public static Subscription createSubscription(final Object unsubscribe) {
final FuncN<?> f = Functions.from(unsubscribe);
return new Subscription() {
@Override
public void unsubscribe() {
f.call();
}
};
}
/**
* Instruct an Observable to pass control to another Observable (the return value of a function)
* rather than calling <code>onError</code> if it encounters an error.
* <p>
* By default, when an Observable encounters an error that prevents it from emitting the expected item to its Observer,
* the Observable calls its {@link Observer}'s <code>onError</code> function, and then quits without calling any more
* of its {@link Observer}'s closures. The <code>onErrorResumeNext</code> method changes this behavior. If you pass a
* function that emits an Observable (<code>resumeFunction</code>) to an Observable's <code>onErrorResumeNext</code> method,
* if the original Observable encounters an error, instead of calling its {@link Observer}'s <code>onError</code> function, it
* will instead relinquish control to this new Observable, which will call the {@link Observer}'s <code>onNext</code> method if
* it is able to do so. In such a case, because no Observable necessarily invokes <code>onError</code>, the Observer may
* never know that an error happened.
* <p>
* You can use this to prevent errors from propagating or to supply fallback data should errors be encountered.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/onErrorResumeNext.png">
*
* @param that
* the source Observable
* @param resumeFunction
* a function that returns an Observable that will take over if the source Observable
* encounters an error
* @return the source Observable, with its behavior modified as described
*/
public static <T> Observable<T> onErrorResumeNext(final Observable<T> that, final Func1<Exception, Observable<T>> resumeFunction) {
return _create(OperationOnErrorResumeNextViaFunction.onErrorResumeNextViaFunction(that, resumeFunction));
}
/**
* Instruct an Observable to pass control to another Observable (the return value of a function)
* rather than calling <code>onError</code> if it encounters an error.
* <p>
* By default, when an Observable encounters an error that prevents it from emitting the expected item to its Observer,
* the Observable calls its {@link Observer}'s <code>onError</code> function, and then quits without calling any more
* of its {@link Observer}'s closures. The <code>onErrorResumeNext</code> method changes this behavior. If you pass a
* function that emits an Observable (<code>resumeFunction</code>) to an Observable's <code>onErrorResumeNext</code> method,
* if the original Observable encounters an error, instead of calling its {@link Observer}'s <code>onError</code> function, it
* will instead relinquish control to this new Observable, which will call the {@link Observer}'s <code>onNext</code> method if
* it is able to do so. In such a case, because no Observable necessarily invokes <code>onError</code>, the Observer may
* never know that an error happened.
* <p>
* You can use this to prevent errors from propagating or to supply fallback data should errors be encountered.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/onErrorResumeNext.png">
*
* @param that
* the source Observable
* @param resumeFunction
* a function that returns an Observable that will take over if the source Observable
* encounters an error
* @return the source Observable, with its behavior modified as described
*/
public static <T> Observable<T> onErrorResumeNext(final Observable<T> that, final Object resumeFunction) {
@SuppressWarnings("rawtypes")
final FuncN _f = Functions.from(resumeFunction);
return onErrorResumeNext(that, new Func1<Exception, Observable<T>>() {
@SuppressWarnings("unchecked")
@Override
public Observable<T> call(Exception e) {
return (Observable<T>) _f.call(e);
}
});
}
/**
* Instruct an Observable to pass control to another Observable rather than calling <code>onError</code> if it encounters an error.
* <p>
* By default, when an Observable encounters an error that prevents it from emitting the expected item to its Observer,
* the Observable calls its {@link Observer}'s <code>onError</code> function, and then quits without calling any more
* of its {@link Observer}'s closures. The <code>onErrorResumeNext</code> method changes this behavior. If you pass a
* function that emits an Observable (<code>resumeFunction</code>) to an Observable's <code>onErrorResumeNext</code> method,
* if the original Observable encounters an error, instead of calling its {@link Observer}'s <code>onError</code> function, it
* will instead relinquish control to this new Observable, which will call the {@link Observer}'s <code>onNext</code> method if
* it is able to do so. In such a case, because no Observable necessarily invokes <code>onError</code>, the Observer may
* never know that an error happened.
* <p>