Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleTimer.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 import java.util.concurrent.atomic.AtomicReference;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
22
23 /**
24 * Signals a {@code 0L} after the specified delay.
25 */
26 public final class SingleTimer extends Single<Long> {
27
28 final long delay;
29 final TimeUnit unit;
30 final Scheduler scheduler;
31
32 public SingleTimer(long delay, TimeUnit unit, Scheduler scheduler) {
33 this.delay = delay;
34 this.unit = unit;
35 this.scheduler = scheduler;
36 }
37
38 @Override
39 protected void subscribeActual(final SingleObserver<? super Long> observer) {
40 TimerDisposable parent = new TimerDisposable(observer);
41 observer.onSubscribe(parent);
42 parent.setFuture(scheduler.scheduleDirect(parent, delay, unit));
43 }
44
45 static final class TimerDisposable extends AtomicReference<Disposable> implements Disposable, Runnable {
46
47 private static final long serialVersionUID = 8465401857522493082L;
48 final SingleObserver<? super Long> downstream;
49
50 TimerDisposable(final SingleObserver<? super Long> downstream) {
51 this.downstream = downstream;
52 }
53
54 @Override
55 public void run() {
56 downstream.onSuccess(0L);
57 }
58
59 @Override
60 public void dispose() {
61 DisposableHelper.dispose(this);
62 }
63
64 @Override
65 public boolean isDisposed() {
66 return DisposableHelper.isDisposed(get());
67 }
68
69 void setFuture(Disposable d) {
70 DisposableHelper.replace(this, d);
71 }
72 }
73 }