-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathCompletable.java
1372 lines (1259 loc) · 59.7 KB
/
Completable.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 2016 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 io.reactivex;
import java.util.concurrent.*;
import org.reactivestreams.*;
import io.reactivex.annotations.SchedulerSupport;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.*;
import io.reactivex.internal.functions.*;
import io.reactivex.internal.operators.completable.*;
import io.reactivex.internal.subscribers.completable.*;
import io.reactivex.plugins.RxJavaPlugins;
import io.reactivex.schedulers.Schedulers;
/**
* Represents a deferred computation without any value but only indication for completion or exception.
*
* The class follows a similar event pattern as Reactive-Streams: onSubscribe (onError|onComplete)?
*/
public abstract class Completable implements CompletableSource {
/** Single instance of a complete Completable. */
static final Completable COMPLETE = new CompletableEmpty();
/** Single instance of a never Completable. */
static final Completable NEVER = new CompletableNever();
/**
* Wraps the given CompletableConsumable into a Completable
* if not already Completable.
* @param source the source to wrap
* @return the source or its wrapper Completable
* @throws NullPointerException if source is null
*/
public static Completable wrap(CompletableSource source) {
Objects.requireNonNull(source, "source is null");
if (source instanceof Completable) {
return (Completable)source;
}
return new CompletableWrapper(source);
}
/**
* Returns a Completable which terminates as soon as one of the source Completables
* terminates (normally or with an error) and cancels all other Completables.
* @param sources the array of source Completables
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable amb(final CompletableSource... sources) {
Objects.requireNonNull(sources, "sources is null");
if (sources.length == 0) {
return complete();
}
if (sources.length == 1) {
return wrap(sources[0]);
}
return new CompletableAmbArray(sources);
}
/**
* Returns a Completable which terminates as soon as one of the source Completables
* terminates (normally or with an error) and cancels all other Completables.
* @param sources the array of source Completables
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable amb(final Iterable<? extends CompletableSource> sources) {
Objects.requireNonNull(sources, "sources is null");
return new CompletableAmbIterable(sources);
}
/**
* Returns a Completable instance that completes immediately when subscribed to.
* @return a Completable instance that completes immediately
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable complete() {
return COMPLETE;
}
/**
* Returns a Completable which completes only when all sources complete, one after another.
* @param sources the sources to concatenate
* @return the Completable instance which completes only when all sources complete
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable concat(CompletableSource... sources) {
Objects.requireNonNull(sources, "sources is null");
if (sources.length == 0) {
return complete();
} else
if (sources.length == 1) {
return wrap(sources[0]);
}
return new CompletableConcatArray(sources);
}
/**
* Returns a Completable which completes only when all sources complete, one after another.
* @param sources the sources to concatenate
* @return the Completable instance which completes only when all sources complete
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable concat(Iterable<? extends CompletableSource> sources) {
Objects.requireNonNull(sources, "sources is null");
return new CompletableConcatIterable(sources);
}
/**
* Returns a Completable which completes only when all sources complete, one after another.
* @param sources the sources to concatenate
* @return the Completable instance which completes only when all sources complete
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable concat(Publisher<? extends CompletableSource> sources) {
return concat(sources, 2);
}
/**
* Returns a Completable which completes only when all sources complete, one after another.
* @param sources the sources to concatenate
* @param prefetch the number of sources to prefetch from the sources
* @return the Completable instance which completes only when all sources complete
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable concat(Publisher<? extends CompletableSource> sources, int prefetch) {
Objects.requireNonNull(sources, "sources is null");
if (prefetch < 1) {
throw new IllegalArgumentException("prefetch > 0 required but it was " + prefetch);
}
return new CompletableConcat(sources, prefetch);
}
/**
* Constructs a Completable instance by wrapping the given onSubscribe callback.
* @param onSubscribe the callback which will receive the CompletableSubscriber instances
* when the Completable is subscribed to.
* @return the created Completable instance
* @throws NullPointerException if onSubscribe is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable create(CompletableSource onSubscribe) {
Objects.requireNonNull(onSubscribe, "onSubscribe is null");
if (onSubscribe instanceof Completable) {
throw new IllegalArgumentException("Use of create(Completable)!");
}
try {
// TODO plugin wrapping onSubscribe
return RxJavaPlugins.onAssembly(new CompletableWrapper(onSubscribe));
} catch (NullPointerException ex) { // NOPMD
throw ex;
} catch (Throwable ex) {
RxJavaPlugins.onError(ex);
throw toNpe(ex);
}
}
/**
* Defers the subscription to a Completable instance returned by a supplier.
* @param completableSupplier the supplier that returns the Completable that will be subscribed to.
* @return the Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable defer(final Callable<? extends CompletableSource> completableSupplier) {
Objects.requireNonNull(completableSupplier, "completableSupplier");
return new CompletableDefer(completableSupplier);
}
/**
* Creates a Completable which calls the given error supplier for each subscriber
* and emits its returned Throwable.
* <p>
* If the errorSupplier returns null, the child CompletableSubscribers will receive a
* NullPointerException.
* @param errorSupplier the error supplier, not null
* @return the new Completable instance
* @throws NullPointerException if errorSupplier is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable error(final Callable<? extends Throwable> errorSupplier) {
Objects.requireNonNull(errorSupplier, "errorSupplier is null");
return new CompletableErrorSupplier(errorSupplier);
}
/**
* Creates a Completable instance that emits the given Throwable exception to subscribers.
* @param error the Throwable instance to emit, not null
* @return the new Completable instance
* @throws NullPointerException if error is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable error(final Throwable error) {
Objects.requireNonNull(error, "error is null");
return new CompletableError(error);
}
/**
* Returns a Completable which when subscribed, executes the callable function, ignores its
* normal result and emits onError or onCompleted only.
* @param callable the callable instance to execute for each subscriber
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable fromCallable(final Callable<?> callable) {
Objects.requireNonNull(callable, "callable is null");
return new CompletableFromCallable(callable);
}
/**
* Returns a Completable instance that subscribes to the given flowable, ignores all values and
* emits only the terminal event.
*
* @param <T> the type of the flowable
* @param flowable the Flowable instance to subscribe to, not null
* @return the new Completable instance
* @throws NullPointerException if flowable is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Completable fromFlowable(final Publisher<T> flowable) {
Objects.requireNonNull(flowable, "flowable is null");
return new CompletableFromFlowable<T>(flowable);
}
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable fromFuture(final Future<?> future) {
Objects.requireNonNull(future, "future is null");
return fromCallable(new Callable<Object>() {
@Override
public Object call() throws Exception {
future.get();
return null;
}
});
}
/**
* Returns a Completable instance that subscribes to the given Observable, ignores all values and
* emits only the terminal event.
* @param <T> the type of the Observable
* @param observable the Observable instance to subscribe to, not null
* @return the new Completable instance
* @throws NullPointerException if flowable is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Completable fromObservable(final ObservableSource<T> observable) {
Objects.requireNonNull(observable, "observable is null");
return new CompletableFromObservable<T>(observable);
}
/**
* Returns a Completable instance that runs the given Runnable for each subscriber and
* emits either an unchecked exception or simply completes.
* @param run the runnable to run for each subscriber
* @return the new Completable instance
* @throws NullPointerException if run is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable fromRunnable(final Runnable run) {
Objects.requireNonNull(run, "run is null");
return new CompletableFromRunnable(run);
}
/**
* Returns a Completable instance that when subscribed to, subscribes to the Single instance and
* emits a completion event if the single emits onSuccess or forwards any onError events.
* @param <T> the value type of the Single
* @param single the Single instance to subscribe to, not null
* @return the new Completable instance
* @throws NullPointerException if single is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Completable fromSingle(final SingleSource<T> single) {
Objects.requireNonNull(single, "single is null");
return new CompletableFromSingle<T>(single);
}
/**
* Returns a Completable instance that subscribes to all sources at once and
* completes only when all source Completables complete or one of them emits an error.
* @param sources the iterable sequence of sources.
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
public static Completable merge(CompletableSource... sources) {
Objects.requireNonNull(sources, "sources is null");
if (sources.length == 0) {
return complete();
} else
if (sources.length == 1) {
return wrap(sources[0]);
}
return new CompletableMergeArray(sources);
}
/**
* Returns a Completable instance that subscribes to all sources at once and
* completes only when all source Completables complete or one of them emits an error.
* @param sources the iterable sequence of sources.
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public static Completable merge(Iterable<? extends CompletableSource> sources) {
Objects.requireNonNull(sources, "sources is null");
return new CompletableMergeIterable(sources);
}
/**
* Returns a Completable instance that subscribes to all sources at once and
* completes only when all source Completables complete or one of them emits an error.
* @param sources the iterable sequence of sources.
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
public static Completable merge(Publisher<? extends CompletableSource> sources) {
return merge0(sources, Integer.MAX_VALUE, false);
}
/**
* Returns a Completable instance that keeps subscriptions to a limited number of sources at once and
* completes only when all source Completables complete or one of them emits an error.
* @param sources the iterable sequence of sources.
* @param maxConcurrency the maximum number of concurrent subscriptions
* @return the new Completable instance
* @throws NullPointerException if sources is null
* @throws IllegalArgumentException if maxConcurrency is less than 1
*/
public static Completable merge(Publisher<? extends CompletableSource> sources, int maxConcurrency) {
return merge0(sources, maxConcurrency, false);
}
/**
* Returns a Completable instance that keeps subscriptions to a limited number of sources at once and
* completes only when all source Completables terminate in one way or another, combining any exceptions
* thrown by either the sources Observable or the inner Completable instances.
* @param sources the iterable sequence of sources.
* @param maxConcurrency the maximum number of concurrent subscriptions
* @param delayErrors delay all errors from the main source and from the inner Completables?
* @return the new Completable instance
* @throws NullPointerException if sources is null
* @throws IllegalArgumentException if maxConcurrency is less than 1
*/
private static Completable merge0(Publisher<? extends CompletableSource> sources, int maxConcurrency, boolean delayErrors) {
Objects.requireNonNull(sources, "sources is null");
if (maxConcurrency < 1) {
throw new IllegalArgumentException("maxConcurrency > 0 required but it was " + maxConcurrency);
}
return new CompletableMerge(sources, maxConcurrency, delayErrors);
}
/**
* Returns a CompletableConsumable that subscribes to all Completables in the source array and delays
* any error emitted by either the sources observable or any of the inner Completables until all of
* them terminate in a way or another.
* @param sources the array of Completables
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
public static Completable mergeDelayError(CompletableSource... sources) {
Objects.requireNonNull(sources, "sources is null");
return new CompletableMergeDelayErrorArray(sources);
}
/**
* Returns a Completable that subscribes to all Completables in the source sequence and delays
* any error emitted by either the sources observable or any of the inner Completables until all of
* them terminate in a way or another.
* @param sources the sequence of Completables
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
public static Completable mergeDelayError(Iterable<? extends CompletableSource> sources) {
Objects.requireNonNull(sources, "sources is null");
return new CompletableMergeDelayErrorIterable(sources);
}
/**
* Returns a Completable that subscribes to all Completables in the source sequence and delays
* any error emitted by either the sources observable or any of the inner Completables until all of
* them terminate in a way or another.
* @param sources the sequence of Completables
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
public static Completable mergeDelayError(Publisher<? extends CompletableSource> sources) {
return merge0(sources, Integer.MAX_VALUE, true);
}
/**
* Returns a Completable that subscribes to a limited number of inner Completables at once in
* the source sequence and delays any error emitted by either the sources
* observable or any of the inner Completables until all of
* them terminate in a way or another.
* @param sources the sequence of Completables
* @param maxConcurrency the maximum number of concurrent subscriptions to Completables
* @return the new Completable instance
* @throws NullPointerException if sources is null
*/
public static Completable mergeDelayError(Publisher<? extends CompletableSource> sources, int maxConcurrency) {
return merge0(sources, maxConcurrency, true);
}
/**
* Returns a Completable that never calls onError or onComplete.
* @return the singleton instance that never calls onError or onComplete
*/
public static Completable never() {
return NEVER;
}
/**
* Returns a Completable instance that fires its onComplete event after the given delay ellapsed.
* @param delay the delay time
* @param unit the delay unit
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.COMPUTATION)
public static Completable timer(long delay, TimeUnit unit) {
return timer(delay, unit, Schedulers.computation());
}
/**
* Returns a Completable instance that fires its onComplete event after the given delay ellapsed
* by using the supplied scheduler.
* @param delay the delay time
* @param unit the delay unit
* @param scheduler the scheduler where to emit the complete event
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
public static Completable timer(final long delay, final TimeUnit unit, final Scheduler scheduler) {
Objects.requireNonNull(unit, "unit is null");
Objects.requireNonNull(scheduler, "scheduler is null");
return new CompletableTimer(delay, unit, scheduler);
}
/**
* Creates a NullPointerException instance and sets the given Throwable as its initial cause.
* @param ex the Throwable instance to use as cause, not null (not verified)
* @return the created NullPointerException
*/
private static NullPointerException toNpe(Throwable ex) {
NullPointerException npe = new NullPointerException("Actually not, but can't pass out an exception otherwise...");
npe.initCause(ex);
return npe;
}
/**
* Returns a Completable instance which manages a resource along
* with a custom Completable instance while the subscription is active.
* <p>
* This overload performs an eager unsubscription before the terminal event is emitted.
*
* @param <R> the resource type
* @param resourceSupplier the supplier that returns a resource to be managed.
* @param completableFunction the function that given a resource returns a Completable instance that will be subscribed to
* @param disposer the consumer that disposes the resource created by the resource supplier
* @return the new Completable instance
*/
public static <R> Completable using(Callable<R> resourceSupplier,
Function<? super R, ? extends CompletableSource> completableFunction,
Consumer<? super R> disposer) {
return using(resourceSupplier, completableFunction, disposer, true);
}
/**
* Returns a Completable instance which manages a resource along
* with a custom Completable instance while the subscription is active and performs eager or lazy
* resource disposition.
* <p>
* If this overload performs a lazy unsubscription after the terminal event is emitted.
* Exceptions thrown at this time will be delivered to RxJavaPlugins only.
*
* @param <R> the resource type
* @param resourceSupplier the supplier that returns a resource to be managed
* @param completableFunction the function that given a resource returns a non-null
* Completable instance that will be subscribed to
* @param disposer the consumer that disposes the resource created by the resource supplier
* @param eager if true, the resource is disposed before the terminal event is emitted, if false, the
* resource is disposed after the terminal event has been emitted
* @return the new Completable instance
*/
public static <R> Completable using(
final Callable<R> resourceSupplier,
final Function<? super R, ? extends CompletableSource> completableFunction,
final Consumer<? super R> disposer,
final boolean eager) {
Objects.requireNonNull(resourceSupplier, "resourceSupplier is null");
Objects.requireNonNull(completableFunction, "completableFunction is null");
Objects.requireNonNull(disposer, "disposer is null");
return new CompletableUsing<R>(resourceSupplier, completableFunction, disposer, eager);
}
/**
* Returns a Completable that emits the a terminated event of either this Completable
* or the other Completable whichever fires first.
* @param other the other Completable, not null
* @return the new Completable instance
* @throws NullPointerException if other is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable ambWith(CompletableSource other) {
Objects.requireNonNull(other, "other is null");
return amb(this, other);
}
/**
* Subscribes to and awaits the termination of this Completable instance in a blocking manner and
* rethrows any exception emitted.
* @throws RuntimeException wrapping an InterruptedException if the current thread is interrupted
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final void await() {
CompletableAwait.await(this);
}
/**
* Subscribes to and awaits the termination of this Completable instance in a blocking manner
* with a specific timeout and rethrows any exception emitted within the timeout window.
* @param timeout the timeout value
* @param unit the timeout unit
* @return true if the this Completable instance completed normally within the time limit,
* false if the timeout ellapsed before this Completable terminated.
* @throws RuntimeException wrapping an InterruptedException if the current thread is interrupted
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final boolean await(long timeout, TimeUnit unit) {
return CompletableAwait.await(this, timeout, unit);
}
/**
* Calls the given transformer function with this instance and returns the function's resulting
* Completable.
* @param transformer the transformer function, not null
* @return the Completable returned by the function
* @throws NullPointerException if transformer is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable compose(CompletableTransformer transformer) {
return wrap(to(transformer));
}
/**
* Returns an Observable which will subscribe to this Completable and once that is completed then
* will subscribe to the {@code next} Observable. An error event from this Completable will be
* propagated to the downstream subscriber and will result in skipping the subscription of the
* Observable.
*
* @param <T> the value type of the next Observable
* @param next the Observable to subscribe after this Completable is completed, not null
* @return Observable that composes this Completable and next
* @throws NullPointerException if next is null
*/
public final <T> Observable<T> andThen(Observable<T> next) {
Objects.requireNonNull(next, "next is null");
return next.delaySubscription(toObservable());
}
/**
* Returns an Flowable which will subscribe to this Completable and once that is completed then
* will subscribe to the {@code next} Flowable. An error event from this Completable will be
* propagated to the downstream subscriber and will result in skipping the subscription of the
* Observable.
*
* @param <T> the value type of the next Flowable
* @param next the Observable to subscribe after this Completable is completed, not null
* @return Flowable that composes this Completable and next
* @throws NullPointerException if next is null
*/
public final <T> Flowable<T> andThen(Flowable<T> next) {
Objects.requireNonNull(next, "next is null");
return next.delaySubscription(toFlowable());
}
/**
* Returns a Single which will subscribe to this Completable and once that is completed then
* will subscribe to the {@code next} Single. An error event from this Completable will be
* propagated to the downstream subscriber and will result in skipping the subscription of the
* Single.
* <dl>
* <dt><b>Scheduler:</b></dt>
* <dd>{@code andThen} does not operate by default on a particular {@link Scheduler}.</dd>
* </dl>
*
* @param <T> the value type of the next Single
* @param next the Single to subscribe after this Completable is completed, not null
* @return Single that composes this Completable and next
*/
public final <T> Single<T> andThen(Single<T> next) {
Objects.requireNonNull(next, "next is null");
return next.delaySubscription(toObservable());
}
/**
* Returns a Completable that first runs this Completable
* and then the other completable.
* <p>
* This is an alias for {@link #concatWith(CompletableSource)}.
* @param next the other Completable, not null
* @return the new Completable instance
* @throws NullPointerException if other is null
*/
public final Completable andThen(Completable next) {
return concatWith(next);
}
/**
* Concatenates this Completable with another Completable.
* @param other the other Completable, not null
* @return the new Completable which subscribes to this and then the other Completable
* @throws NullPointerException if other is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable concatWith(CompletableSource other) {
Objects.requireNonNull(other, "other is null");
return concat(this, other);
}
/**
* Returns a Completable which delays the emission of the completion event by the given time.
* @param delay the delay time
* @param unit the delay unit
* @return the new Completable instance
* @throws NullPointerException if unit is null
*/
@SchedulerSupport(SchedulerSupport.COMPUTATION)
public final Completable delay(long delay, TimeUnit unit) {
return delay(delay, unit, Schedulers.computation(), false);
}
/**
* Returns a Completable which delays the emission of the completion event by the given time while
* running on the specified scheduler.
* @param delay the delay time
* @param unit the delay unit
* @param scheduler the scheduler to run the delayed completion on
* @return the new Completable instance
* @throws NullPointerException if unit or scheduler is null
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
public final Completable delay(long delay, TimeUnit unit, Scheduler scheduler) {
return delay(delay, unit, scheduler, false);
}
/**
* Returns a Completable which delays the emission of the completion event, and optionally the error as well, by the given time while
* running on the specified scheduler.
* @param delay the delay time
* @param unit the delay unit
* @param scheduler the scheduler to run the delayed completion on
* @param delayError delay the error emission as well?
* @return the new Completable instance
* @throws NullPointerException if unit or scheduler is null
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
public final Completable delay(final long delay, final TimeUnit unit, final Scheduler scheduler, final boolean delayError) {
Objects.requireNonNull(unit, "unit is null");
Objects.requireNonNull(scheduler, "scheduler is null");
return new CompletableDelay(this, delay, unit, scheduler, delayError);
}
/**
* Returns a Completable which calls the given onComplete callback if this Completable completes.
* @param onComplete the callback to call when this emits an onComplete event
* @return the new Completable instance
* @throws NullPointerException if onComplete is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable doOnComplete(Runnable onComplete) {
return doOnLifecycle(Functions.emptyConsumer(), Functions.emptyConsumer(),
onComplete, Functions.EMPTY_RUNNABLE,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE);
}
/**
* Returns a Completable which calls the giveon onDispose callback if the child subscriber cancels
* the subscription.
* @param onDispose the callback to call when the child subscriber cancels the subscription
* @return the new Completable instance
* @throws NullPointerException if onDispose is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable doOnDispose(Runnable onDispose) {
return doOnLifecycle(Functions.emptyConsumer(), Functions.emptyConsumer(),
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE,
Functions.EMPTY_RUNNABLE, onDispose);
}
/**
* Returns a Completable which calls the given onError callback if this Completable emits an error.
* @param onError the error callback
* @return the new Completable instance
* @throws NullPointerException if onError is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable doOnError(Consumer<? super Throwable> onError) {
return doOnLifecycle(Functions.emptyConsumer(), onError,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE);
}
/**
* Returns a Completable instance that calls the various callbacks on the specific
* lifecycle events.
* @param onSubscribe the consumer called when a CompletableSubscriber subscribes.
* @param onError the consumer called when this emits an onError event
* @param onComplete the runnable called just before when this Completable completes normally
* @param onAfterTerminate the runnable called after this Completable completes normally
* @param onDisposed the runnable called when the child cancels the subscription
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
private Completable doOnLifecycle(
final Consumer<? super Disposable> onSubscribe,
final Consumer<? super Throwable> onError,
final Runnable onComplete,
final Runnable onTerminate,
final Runnable onAfterTerminate,
final Runnable onDisposed) {
Objects.requireNonNull(onSubscribe, "onSubscribe is null");
Objects.requireNonNull(onError, "onError is null");
Objects.requireNonNull(onComplete, "onComplete is null");
Objects.requireNonNull(onTerminate, "onTerminate is null");
Objects.requireNonNull(onAfterTerminate, "onAfterTerminate is null");
Objects.requireNonNull(onDisposed, "onDisposed is null");
return new CompletablePeek(this, onSubscribe, onError, onComplete, onTerminate, onAfterTerminate, onDisposed);
}
/**
* Returns a Completable instance that calls the given onSubscribe callback with the disposable
* that child subscribers receive on subscription.
* @param onSubscribe the callback called when a child subscriber subscribes
* @return the new Completable instance
* @throws NullPointerException if onSubscribe is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable doOnSubscribe(Consumer<? super Disposable> onSubscribe) {
return doOnLifecycle(onSubscribe, Functions.emptyConsumer(),
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE);
}
/**
* Returns a Completable instance that calls the given onTerminate callback just before this Completable
* completes normally or with an exception
* @param onTerminate the callback to call just before this Completable terminates
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable doOnTerminate(final Runnable onTerminate) {
return doOnLifecycle(Functions.emptyConsumer(), Functions.emptyConsumer(),
Functions.EMPTY_RUNNABLE, onTerminate,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE);
}
/**
* Returns a Completable instance that calls the given onTerminate callback after this Completable
* completes normally or with an exception
* @param onAfterTerminate the callback to call after this Completable terminates
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable doAfterTerminate(final Runnable onAfterTerminate) {
return doOnLifecycle(Functions.emptyConsumer(), Functions.emptyConsumer(),
onAfterTerminate, Functions.EMPTY_RUNNABLE,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE);
}
/**
* Returns a completable that first runs this Completable
* and then the other completable.
* <p>
* This is an alias for {@link #concatWith(CompletableSource)}.
* @param other the other CompletableConsumable, not null
* @return the new Completable instance
* @throws NullPointerException if other is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable endWith(CompletableSource other) {
return concatWith(other);
}
/**
* Returns an NbpObservable that first runs this Completable instance and
* resumes with the given next Observable.
* @param <T> the type of the NbpObservable
* @param next the next Observable to continue
* @return the new Observable instance
* @throws NullPointerException if next is null
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
public final <T> Observable<T> endWith(ObservableSource<T> next) {
return this.<T>toObservable().endWith(next);
}
/**
* Returns an Observable that first runs this Completable instance and
* resumes with the given next Observable.
* @param <T> the value type of the observable
* @param next the next Observable to continue
* @return the new Observable instance
* @throws NullPointerException if next is null
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
public final <T> Flowable<T> endWith(Publisher<T> next) {
return this.<T>toFlowable().endWith(next);
}
/**
* Returns a Completable instace that calls the given onAfterComplete callback after this
* Completable completes normally.
* @param onAfterComplete the callback to call after this Completable emits an onComplete event.
* @return the new Completable instance
* @throws NullPointerException if onAfterComplete is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
@Deprecated
public final Completable finallyDo(Runnable onAfterComplete) {
return doOnLifecycle(Functions.emptyConsumer(), Functions.emptyConsumer(),
Functions.EMPTY_RUNNABLE, onAfterComplete,
Functions.EMPTY_RUNNABLE, Functions.EMPTY_RUNNABLE);
}
/**
* Subscribes to this Completable instance and blocks until it terminates, then returns null or
* the emitted exception if any.
* @return the throwable if this terminated with an error, null otherwise
* @throws RuntimeException that wraps an InterruptedException if the wait is interrupted
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Throwable get() {
return CompletableAwait.get(this);
}
/**
* Subscribes to this Completable instance and blocks until it terminates or the specified timeout
* ellapses, then returns null for normal termination or the emitted exception if any.
* @param timeout the timeout value
* @param unit the time unit
* @return the throwable if this terminated with an error, null otherwise
* @throws RuntimeException that wraps an InterruptedException if the wait is interrupted or
* TimeoutException if the specified timeout ellapsed before it
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Throwable get(long timeout, TimeUnit unit) {
return CompletableAwait.get(this, timeout, unit);
}
/**
* Lifts a CompletableSubscriber transformation into the chain of Completables.
* @param onLift the lifting function that transforms the child subscriber with a parent subscriber.
* @return the new Completable instance
* @throws NullPointerException if onLift is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable lift(final CompletableOperator onLift) {
Objects.requireNonNull(onLift, "onLift is null");
return new CompletableLift(this, onLift);
}
/**
* Returns a Completable which subscribes to this and the other Completable and completes
* when both of them complete or one emits an error.
* @param other the other Completable instance
* @return the new Completable instance
* @throws NullPointerException if other is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable mergeWith(CompletableSource other) {
Objects.requireNonNull(other, "other is null");
return merge(this, other);
}
/**
* Returns a Completable which emits the terminal events from the thread of the specified scheduler.
* @param scheduler the scheduler to emit terminal events on
* @return the new Completable instance
* @throws NullPointerException if scheduler is null
*/
@SchedulerSupport(SchedulerSupport.CUSTOM)
public final Completable observeOn(final Scheduler scheduler) {
Objects.requireNonNull(scheduler, "scheduler is null");
return new CompletableObserveOn(this, scheduler);
}
/**
* Returns a Completable instance that if this Completable emits an error, it will emit an onComplete
* and swallow the throwable.
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable onErrorComplete() {
return onErrorComplete(Functions.alwaysTrue());
}
/**
* Returns a Completable instance that if this Completable emits an error and the predicate returns
* true, it will emit an onComplete and swallow the throwable.
* @param predicate the predicate to call when an Throwable is emitted which should return true
* if the Throwable should be swallowed and replaced with an onComplete.
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable onErrorComplete(final Predicate<? super Throwable> predicate) {
Objects.requireNonNull(predicate, "predicate is null");
return new CompletableOnErrorComplete(this, predicate);
}
/**
* Returns a Completable instance that when encounters an error from this Completable, calls the
* specified mapper function that returns another Completable instance for it and resumes the
* execution with it.
* @param errorMapper the mapper function that takes the error and should return a Completable as
* continuation.
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable onErrorResumeNext(final Function<? super Throwable, ? extends CompletableSource> errorMapper) {
Objects.requireNonNull(errorMapper, "errorMapper is null");
return new CompletableResumeNext(this, errorMapper);
}
/**
* Returns a Completable that repeatedly subscribes to this Completable until cancelled.
* @return the new Completable instance
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable repeat() {
return fromFlowable(toFlowable().repeat());
}
/**
* Returns a Completable that subscribes repeatedly at most the given times to this Completable.
* @param times the number of times the resubscription should happen
* @return the new Completable instance
* @throws IllegalArgumentException if times is less than zero
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable repeat(long times) {
return fromFlowable(toFlowable().repeat(times));
}
/**
* Returns a Completable that repeatedly subscribes to this Completable so long as the given
* stop supplier returns false.
* @param stop the supplier that should return true to stop resubscribing.
* @return the new Completable instance
* @throws NullPointerException if stop is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
public final Completable repeatUntil(BooleanSupplier stop) {
return fromFlowable(toFlowable().repeatUntil(stop));
}
/**
* Returns a Completable instance that repeats when the Publisher returned by the handler
* emits an item or completes when this Publisher emits a completed event.
* @param handler the function that transforms the stream of values indicating the completion of
* this Completable and returns a Publisher that emits items for repeating or completes to indicate the
* repetition should stop
* @return the new Completable instance
* @throws NullPointerException if stop is null
*/
@SchedulerSupport(SchedulerSupport.NONE)
/*
* FIXME the Observable<Void> type doesn't make sense here because nulls are not allowed
* FIXME add unit test once the type has been fixed
*/
public final Completable repeatWhen(Function<? super Flowable<Object>, ? extends Publisher<Object>> handler) {
return fromFlowable(toFlowable().repeatWhen(handler));
}