Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleDelay.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.operators.single;
15
16 import java.util.concurrent.TimeUnit;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.SequentialDisposable;
21
22 public final class SingleDelay<T> extends Single<T> {
23
24 final SingleSource<? extends T> source;
25 final long time;
26 final TimeUnit unit;
27 final Scheduler scheduler;
28 final boolean delayError;
29
30 public SingleDelay(SingleSource<? extends T> source, long time, TimeUnit unit, Scheduler scheduler, boolean delayError) {
31 this.source = source;
32 this.time = time;
33 this.unit = unit;
34 this.scheduler = scheduler;
35 this.delayError = delayError;
36 }
37
38 @Override
39 protected void subscribeActual(final SingleObserver<? super T> observer) {
40
41 final SequentialDisposable sd = new SequentialDisposable();
42 observer.onSubscribe(sd);
43 source.subscribe(new Delay(sd, observer));
44 }
45
46 final class Delay implements SingleObserver<T> {
47 private final SequentialDisposable sd;
48 final SingleObserver<? super T> downstream;
49
50 Delay(SequentialDisposable sd, SingleObserver<? super T> observer) {
51 this.sd = sd;
52 this.downstream = observer;
53 }
54
55 @Override
56 public void onSubscribe(Disposable d) {
57 sd.replace(d);
58 }
59
60 @Override
61 public void onSuccess(final T value) {
62 sd.replace(scheduler.scheduleDirect(new OnSuccess(value), time, unit));
63 }
64
65 @Override
66 public void onError(final Throwable e) {
67 sd.replace(scheduler.scheduleDirect(new OnError(e), delayError ? time : 0, unit));
68 }
69
70 final class OnSuccess implements Runnable {
71 private final T value;
72
73 OnSuccess(T value) {
74 this.value = value;
75 }
76
77 @Override
78 public void run() {
79 downstream.onSuccess(value);
80 }
81 }
82
83 final class OnError implements Runnable {
84 private final Throwable e;
85
86 OnError(Throwable e) {
87 this.e = e;
88 }
89
90 @Override
91 public void run() {
92 downstream.onError(e);
93 }
94 }
95 }
96 }