forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTask.java
More file actions
1950 lines (1834 loc) · 92.1 KB
/
Task.java
File metadata and controls
1950 lines (1834 loc) · 92.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
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 2015 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 java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import rx.Observable.OnSubscribe;
import rx.Observable.Operator;
import rx.annotations.Experimental;
import rx.exceptions.Exceptions;
import rx.exceptions.OnErrorNotImplementedException;
import rx.functions.Action1;
import rx.functions.Func1;
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.internal.operators.OnSubscribeToObservableFuture;
import rx.internal.operators.OperatorMap;
import rx.internal.operators.OperatorObserveOn;
import rx.internal.operators.OperatorOnErrorReturn;
import rx.internal.operators.OperatorSubscribeOn;
import rx.internal.operators.OperatorTimeout;
import rx.internal.operators.OperatorZip;
import rx.observables.BlockingObservable;
import rx.observers.SafeSubscriber;
import rx.plugins.RxJavaObservableExecutionHook;
import rx.plugins.RxJavaPlugins;
import rx.schedulers.Schedulers;
import rx.subscriptions.Subscriptions;
/**
* The Task class that implements the Reactive Pattern for scalars (single values). See {@link Observable} for a stream or vector of values.
* <p>
* The documentation for this class makes use of marble diagrams. The following legend explains these diagrams:
* <p>
* <img width="640" height="301" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/legend.png" alt="">
* <p>
* For more information see the <a href="http://reactivex.io/documentation/observable.html">ReactiveX documentation</a>.
*
* @param <T>
* the type of the item emitted by the Task
*/
@Experimental
public class Task<T> {
final OnSubscribe<T> onSubscribe;
/**
* Creates a Task with a Function to execute when it is subscribed to (executed).
* <p>
* <em>Note:</em> Use {@link #create(OnExecute)} to create a Task, instead of this constructor,
* unless you specifically have a need for inheritance.
*
* @param f
* {@link OnExecute} to be executed when {@link #execute(TaskObserver)} or {@link #subscribe(Subscriber)} is called
*/
protected Task(final OnExecute<T> f) {
// bridge between OnSubscribe (which all Operators and Observables use) and OnExecute (for Task)
this.onSubscribe = new OnSubscribe<T>() {
@Override
public void call(final Subscriber<? super T> s) {
f.call(new TaskObserver<T>() {
@Override
public void onSuccess(T value) {
// TODO validate error handling if onNext throws
s.onNext(value);
s.onCompleted();
}
@Override
public void onError(Throwable error) {
// TODO validate error handling if onError throws
s.onError(error);
}
});
}
};
}
private Task(final OnSubscribe<T> f) {
this.onSubscribe = f;
}
private static final RxJavaObservableExecutionHook hook = RxJavaPlugins.getInstance().getObservableExecutionHook();
/**
* Returns a Task that will execute the specified function when a {@link TaskObserver} executes it or {@link Subscriber} subscribes to it.
* <p>
* <img width="640" height="200" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/create.png" alt="">
* <p>
* Write the function you pass to {@code create} so that it behaves as a Task: It should invoke the
* TaskExecutor {@link TaskObserver#onSuccess onSuccess} and {@link TaskObserver#onError onError} methods appropriately.
* <p>
* A well-formed Task must invoke either the TaskExecutor's {@code onSuccess} method exactly once or
* its {@code onError} method exactly once.
* <p>
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code create} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <T>
* the type of the item that this Task emits
* @param f
* a function that accepts an {@code TaskExecutor<T>}, and invokes its {@code onSuccess} or {@code onError} methods as appropriate
* @return a Task that, when a {@link Subscriber} subscribes to it, will execute the specified function
* @see <a href="http://reactivex.io/documentation/operators/create.html">ReactiveX operators documentation: Create</a>
*/
public final static <T> Task<T> create(OnExecute<T> f) {
return new Task<T>(f); // TODO need hook
}
/**
* Invoked when Task.execute is called.
*/
public static interface OnExecute<T> extends Action1<TaskObserver<? super T>> {
// cover for generics insanity
}
// TODO what should be the name of this?
public static interface TaskObserver<T> {
public void onSuccess(T value);
public void onError(Throwable error);
}
public static class Promise<T> {
private final Object lock = new Object();
private TaskObserver<? super T> child;
private T v;
private Throwable error;
private final Task<T> task;
public static <T> Promise<T> create() {
return new Promise();
}
Promise() {
this.task = Task.create(new OnExecute<T>() {
@Override
public void call(TaskObserver<? super T> c) {
T _v = null;
Throwable _t = null;
synchronized (lock) {
child = c;
_v = v;
_t = error;
}
if (_v != null) {
// if a value is already set emit it
c.onSuccess(_v);
} else if (_t != null) {
// if a Throwable is already set emit it
c.onError(_t);
}
}
});
}
public final void onSuccess(T value) {
TaskObserver<? super T> c = null;
synchronized (lock) {
if (child != null) {
c = child;
} else {
v = value;
}
}
// emit outside of lock if we have an observer
if (c != null) {
c.onSuccess(value);
}
}
public final void onError(Throwable t) {
TaskObserver<? super T> c = null;
synchronized (lock) {
if (child != null) {
c = child;
} else {
t = error;
}
}
// emit outside of lock if we have an observer
if (c != null) {
c.onError(t);
}
}
public final Task<T> getTask() {
return task;
}
}
/**
* Lifts a function to the current Task and returns a new Task that when subscribed to will pass
* the values of the current Task through the Operator function.
* <p>
* In other words, this allows chaining TaskExecutors together on a Task for acting on the values within
* the Task.
* <p> {@code
* task.map(...).filter(...).lift(new OperatorA()).lift(new OperatorB(...)).subscribe()
* } <p>
* If the operator you are creating is designed to act on the item emitted by a source
* Task, use {@code lift}. If your operator is designed to transform the source Task as a whole
* (for instance, by applying a particular set of existing RxJava operators to it) use {@link #compose}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code lift} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param lift
* the Operator that implements the Task-operating function to be applied to the source Task
* @return a Task that is the result of applying the lifted Operator to the source Task
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators">RxJava wiki: Implementing Your Own Operators</a>
*/
public final <R> Task<R> lift(final Operator<? extends R, ? super T> lift) {
return new Task<R>(new OnSubscribe<R>() {
@Override
public void call(Subscriber<? super R> o) {
try {
final Subscriber<? super T> st = hook.onLift(lift).call(o);
try {
// new Subscriber created and being subscribed with so 'onStart' it
st.onStart();
onSubscribe.call(st);
} catch (Throwable e) {
// localized capture of errors rather than it skipping all operators
// and ending up in the try/catch of the subscribe method which then
// prevents onErrorResumeNext and other similar approaches to error handling
if (e instanceof OnErrorNotImplementedException) {
throw (OnErrorNotImplementedException) e;
}
st.onError(e);
}
} catch (Throwable e) {
if (e instanceof OnErrorNotImplementedException) {
throw (OnErrorNotImplementedException) e;
}
// if the lift function failed all we can do is pass the error to the final Subscriber
// as we don't have the operator available to us
o.onError(e);
}
}
});
}
/**
* Transform an Observable by applying a particular Transformer function to it.
* <p>
* This method operates on the Observable itself whereas {@link #lift} operates on the Observable's
* Subscribers or Observers.
* <p>
* If the operator you are creating is designed to act on the individual items emitted by a source
* Observable, use {@link #lift}. If your operator is designed to transform the source Observable as a whole
* (for instance, by applying a particular set of existing RxJava operators to it) use {@code compose}.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code compose} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param transformer
* implements the function that transforms the source Observable
* @return the source Observable, transformed by the transformer function
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Implementing-Your-Own-Operators">RxJava wiki: Implementing Your Own Operators</a>
*/
@SuppressWarnings("unchecked")
public <R> Task<R> compose(Transformer<? super T, ? extends R> transformer) {
return ((Transformer<T, R>) transformer).call(this);
}
/**
* Transformer function used by {@link #compose}.
*
* @warn more complete description needed
*/
public static interface Transformer<T, R> extends Func1<Task<T>, Task<R>> {
// cover for generics insanity
}
private static <T> Observable<T> toObservable(Task<T> t) {
// is this sufficient, or do I need to keep the outer Task and subscribe to it?
return Observable.create(t.onSubscribe);
}
/**
* INTERNAL: Used with lift and operators.
*
* Converts the source {@code Task<T>} into an {@code Task<Observable<T>>} that emits the
* source Observable as its single emission.
* <p>
* <img width="640" height="350" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/nest.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code nest} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @return an Observable that emits a single item: the source Observable
* @see <a href="http://reactivex.io/documentation/operators/to.html">ReactiveX operators documentation: To</a>
*/
private final Task<Observable<T>> nest() {
return Task.just(toObservable(this));
}
/* *********************************************************************************************************
* Operators Below Here
* *********************************************************************************************************
*/
/**
* Returns an Observable that emits the items emitted by two Tasks, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* an Task to be concatenated
* @param t2
* an Task to be concatenated
* @return an Observable that emits items emitted by the two source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2) {
return Observable.concat(toObservable(t1), toObservable(t2));
}
/**
* Returns an Observable that emits the items emitted by three Tasks, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @return an Observable that emits items emitted by the three source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3));
}
/**
* Returns an Observable that emits the items emitted by four Observables, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @param t4
* a Task to be concatenated
* @return an Observable that emits items emitted by the four source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4));
}
/**
* Returns an Observable that emits the items emitted by five Observables, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @param t4
* a Task to be concatenated
* @param t5
* a Task to be concatenated
* @return an Observable that emits items emitted by the five source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5));
}
/**
* Returns an Observable that emits the items emitted by six Observables, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @param t4
* a Task to be concatenated
* @param t5
* a Task to be concatenated
* @param t6
* a Task to be concatenated
* @return an Observable that emits items emitted by the six source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6));
}
/**
* Returns an Observable that emits the items emitted by seven Observables, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @param t4
* a Task to be concatenated
* @param t5
* a Task to be concatenated
* @param t6
* a Task to be concatenated
* @param t7
* a Task to be concatenated
* @return an Observable that emits items emitted by the seven source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6, Task<? extends T> t7) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6), toObservable(t7));
}
/**
* Returns an Observable that emits the items emitted by eight Observables, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @param t4
* a Task to be concatenated
* @param t5
* a Task to be concatenated
* @param t6
* a Task to be concatenated
* @param t7
* a Task to be concatenated
* @param t8
* a Task to be concatenated
* @return an Observable that emits items emitted by the eight source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6, Task<? extends T> t7, Task<? extends T> t8) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6), toObservable(t7), toObservable(t8));
}
/**
* Returns an Observable that emits the items emitted by nine Observables, one after the other, without
* interleaving them.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/concat.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code concat} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be concatenated
* @param t2
* a Task to be concatenated
* @param t3
* a Task to be concatenated
* @param t4
* a Task to be concatenated
* @param t5
* a Task to be concatenated
* @param t6
* a Task to be concatenated
* @param t7
* a Task to be concatenated
* @param t8
* a Task to be concatenated
* @param t9
* a Task to be concatenated
* @return an Observable that emits items emitted by the nine source Observables, one after the other,
* without interleaving them
* @see <a href="http://reactivex.io/documentation/operators/concat.html">ReactiveX operators documentation: Concat</a>
*/
public final static <T> Observable<T> concat(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6, Task<? extends T> t7, Task<? extends T> t8, Task<? extends T> t9) {
return Observable.concat(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6), toObservable(t7), toObservable(t8), toObservable(t9));
}
/**
* Returns an Observable that invokes an {@link Observer}'s {@link Observer#onError onError} method when the
* Observer subscribes to it.
* <p>
* <img width="640" height="190" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/error.png" alt="">
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code error} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param exception
* the particular Throwable to pass to {@link Observer#onError onError}
* @param <T>
* the type of the items (ostensibly) emitted by the Observable
* @return an Observable that invokes the {@link Observer}'s {@link Observer#onError onError} method when
* the Observer subscribes to it
* @see <a href="http://reactivex.io/documentation/operators/empty-never-throw.html">ReactiveX operators documentation: Throw</a>
*/
public final static <T> Task<T> error(final Throwable exception) {
return Task.create(new OnExecute<T>() {
@Override
public void call(TaskObserver<? super T> te) {
te.onError(exception);
}
});
}
/**
* Converts a {@link Future} into an Observable.
* <p>
* <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/from.Future.png" alt="">
* <p>
* You can convert any object that supports the {@link Future} interface into an Observable that emits the
* return value of the {@link Future#get} method of that object, by passing the object into the {@code from} method.
* <p>
* <em>Important note:</em> This Observable is blocking; you cannot unsubscribe from it.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code from} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param future
* the source {@link Future}
* @param <T>
* the type of object that the {@link Future} returns, and also the type of item to be emitted by
* the resulting Observable
* @return an Observable that emits the item from the source {@link Future}
* @see <a href="http://reactivex.io/documentation/operators/from.html">ReactiveX operators documentation: From</a>
*/
public final static <T> Task<T> from(Future<? extends T> future) {
return new Task<T>(OnSubscribeToObservableFuture.toObservableFuture(future));
}
/**
* Converts a {@link Future} into an Observable, with a timeout on the Future.
* <p>
* <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/from.Future.png" alt="">
* <p>
* You can convert any object that supports the {@link Future} interface into an Observable that emits the
* return value of the {@link Future#get} method of that object, by passing the object into the {@code from} method.
* <p>
* <em>Important note:</em> This Observable is blocking; you cannot unsubscribe from it.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code from} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param future
* the source {@link Future}
* @param timeout
* the maximum time to wait before calling {@code get}
* @param unit
* the {@link TimeUnit} of the {@code timeout} argument
* @param <T>
* the type of object that the {@link Future} returns, and also the type of item to be emitted by
* the resulting Observable
* @return an Observable that emits the item from the source {@link Future}
* @see <a href="http://reactivex.io/documentation/operators/from.html">ReactiveX operators documentation: From</a>
*/
public final static <T> Task<T> from(Future<? extends T> future, long timeout, TimeUnit unit) {
return new Task<T>(OnSubscribeToObservableFuture.toObservableFuture(future, timeout, unit));
}
/**
* Converts a {@link Future}, operating on a specified {@link Scheduler}, into an Observable.
* <p>
* <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/from.Future.s.png" alt="">
* <p>
* You can convert any object that supports the {@link Future} interface into an Observable that emits the
* return value of the {@link Future#get} method of that object, by passing the object into the {@code from} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>you specify which {@link Scheduler} this operator will use</dd>
* </dl>
*
* @param future
* the source {@link Future}
* @param scheduler
* the {@link Scheduler} to wait for the Future on. Use a Scheduler such as {@link Schedulers#io()} that can block and wait on the Future
* @param <T>
* the type of object that the {@link Future} returns, and also the type of item to be emitted by
* the resulting Observable
* @return an Observable that emits the item from the source {@link Future}
* @see <a href="http://reactivex.io/documentation/operators/from.html">ReactiveX operators documentation: From</a>
*/
public final static <T> Task<T> from(Future<? extends T> future, Scheduler scheduler) {
return new Task<T>(OnSubscribeToObservableFuture.toObservableFuture(future)).subscribeOn(scheduler);
}
/**
* Returns an Observable that emits a single item and then completes.
* <p>
* <img width="640" height="310" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/just.png" alt="">
* <p>
* To convert any object into an Observable that emits that object, pass that object into the {@code just} method.
* <p>
* This is similar to the {@link #from(java.lang.Object[])} method, except that {@code from} will convert
* an {@link Iterable} object into an Observable that emits each of the items in the Iterable, one at a
* time, while the {@code just} method converts an Iterable into an Observable that emits the entire
* Iterable as a single item.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code just} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param value
* the item to emit
* @param <T>
* the type of that item
* @return an Observable that emits {@code value} as a single item and then completes
* @see <a href="http://reactivex.io/documentation/operators/just.html">ReactiveX operators documentation: Just</a>
*/
public final static <T> Task<T> just(final T value) {
// TODO add similar optimization as ScalarSynchronousObservable
return Task.create(new OnExecute<T>() {
@Override
public void call(TaskObserver<? super T> te) {
te.onSuccess(value);
}
});
}
// /**
// * Flattens an Observable that emits Observables into a single Observable that emits the items emitted by
// * those Observables, without any transformation.
// * <p>
// * <img width="640" height="370" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.oo.png" alt="">
// * <p>
// * You can combine the items emitted by multiple Observables so that they appear as a single Observable, by
// * using the {@code merge} method.
// * <dl>
// * <dt><b>Scheduler:</b></dt>
// * <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
// * </dl>
// *
// * @param source
// * an Observable that emits Observables
// * @return an Observable that emits items that are the result of flattening the Observables emitted by the {@code source} Observable
// * @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
// */
// public final static <T> Observable<T> merge(Observable<? extends Task<? extends T>> source) {
// return source.lift(OperatorMerge.<T> instance(false));
// }
/**
* Flattens a Task that emits a Task into a single Task that emits the items emitted by
* the nested Task, without any transformation.
* <p>
* <img width="640" height="370" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.oo.png" alt="">
* <p>
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param source
* a Task that emits a Task
* @return a Task that emits the item that is the result of flattening the Task emitted by the {@code source} Task
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Task<T> merge(final Task<? extends Task<? extends T>> source) {
// // TODO This approach is a hack using Observable.merge
// Task<Observable<? extends T>> m2 = source.map(new Func1<Task<? extends T>, Observable<? extends T>>() {
//
// @Override
// public Observable<? extends T> call(Task<? extends T> t) {
// return toObservable(t);
// }
//
// });
// Observable<T> merged = Observable.merge(toObservable(m2));
// return new Task<T>(merged.onSubscribe);
// TODO this is a quick hack that ignores backpressure etc
// TODO can we do this with lift instead?
return Task.create(new OnExecute<T>() {
@Override
public void call(final TaskObserver<? super T> child) {
source.execute(new TaskObserver<Task<? extends T>>() {
@Override
public void onSuccess(Task<? extends T> t) {
t.execute(child);
}
@Override
public void onError(Throwable error) {
child.onError(error);
}
});
}
});
}
/**
* Flattens two Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2) {
return Observable.merge(toObservable(t1), toObservable(t2));
}
/**
* Flattens three Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @param t3
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3) {
return Observable.merge(toObservable(t1), toObservable(t2), toObservable(t3));
}
/**
* Flattens four Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @param t3
* a Task to be merged
* @param t4
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4) {
return Observable.merge(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4));
}
/**
* Flattens five Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @param t3
* a Task to be merged
* @param t4
* a Task to be merged
* @param t5
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5) {
return Observable.merge(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5));
}
/**
* Flattens six Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @param t3
* a Task to be merged
* @param t4
* a Task to be merged
* @param t5
* a Task to be merged
* @param t6
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6) {
return Observable.merge(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6));
}
/**
* Flattens seven Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @param t3
* a Task to be merged
* @param t4
* a Task to be merged
* @param t5
* a Task to be merged
* @param t6
* a Task to be merged
* @param t7
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6, Task<? extends T> t7) {
return Observable.merge(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6), toObservable(t7));
}
/**
* Flattens eight Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param t1
* a Task to be merged
* @param t2
* a Task to be merged
* @param t3
* a Task to be merged
* @param t4
* a Task to be merged
* @param t5
* a Task to be merged
* @param t6
* a Task to be merged
* @param t7
* a Task to be merged
* @param t8
* a Task to be merged
* @return an Observable that emits all of the items emitted by the source Observables
* @see <a href="http://reactivex.io/documentation/operators/merge.html">ReactiveX operators documentation: Merge</a>
*/
public final static <T> Observable<T> merge(Task<? extends T> t1, Task<? extends T> t2, Task<? extends T> t3, Task<? extends T> t4, Task<? extends T> t5, Task<? extends T> t6, Task<? extends T> t7, Task<? extends T> t8) {
return Observable.merge(toObservable(t1), toObservable(t2), toObservable(t3), toObservable(t4), toObservable(t5), toObservable(t6), toObservable(t7), toObservable(t8));
}
/**
* Flattens nine Observables into a single Observable, without any transformation.
* <p>
* <img width="640" height="380" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/merge.png" alt="">
* <p>
* You can combine items emitted by multiple Observables so that they appear as a single Observable, by
* using the {@code merge} method.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code merge} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*