Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableTimer.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.observable;
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.*;
22
23 public final class ObservableTimer extends Observable<Long> {
24 final Scheduler scheduler;
25 final long delay;
26 final TimeUnit unit;
27 public ObservableTimer(long delay, TimeUnit unit, Scheduler scheduler) {
28 this.delay = delay;
29 this.unit = unit;
30 this.scheduler = scheduler;
31 }
32
33 @Override
34 public void subscribeActual(Observer<? super Long> observer) {
35 TimerObserver ios = new TimerObserver(observer);
36 observer.onSubscribe(ios);
37
38 Disposable d = scheduler.scheduleDirect(ios, delay, unit);
39
40 ios.setResource(d);
41 }
42
43 static final class TimerObserver extends AtomicReference<Disposable>
44 implements Disposable, Runnable {
45
46 private static final long serialVersionUID = -2809475196591179431L;
47
48 final Observer<? super Long> downstream;
49
50 TimerObserver(Observer<? super Long> downstream) {
51 this.downstream = downstream;
52 }
53
54 @Override
55 public void dispose() {
56 DisposableHelper.dispose(this);
57 }
58
59 @Override
60 public boolean isDisposed() {
61 return get() == DisposableHelper.DISPOSED;
62 }
63
64 @Override
65 public void run() {
66 if (!isDisposed()) {
67 downstream.onNext(0L);
68 lazySet(EmptyDisposable.INSTANCE);
69 downstream.onComplete();
70 }
71 }
72
73 public void setResource(Disposable d) {
74 DisposableHelper.trySet(this, d);
75 }
76 }
77 }