Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeTimer.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.maybe;
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 MaybeTimer extends Maybe<Long> {
27
28 final long delay;
29
30 final TimeUnit unit;
31
32 final Scheduler scheduler;
33
34 public MaybeTimer(long delay, TimeUnit unit, Scheduler scheduler) {
35 this.delay = delay;
36 this.unit = unit;
37 this.scheduler = scheduler;
38 }
39
40 @Override
41 protected void subscribeActual(final MaybeObserver<? super Long> observer) {
42 TimerDisposable parent = new TimerDisposable(observer);
43 observer.onSubscribe(parent);
44 parent.setFuture(scheduler.scheduleDirect(parent, delay, unit));
45 }
46
47 static final class TimerDisposable extends AtomicReference<Disposable> implements Disposable, Runnable {
48
49 private static final long serialVersionUID = 2875964065294031672L;
50 final MaybeObserver<? super Long> downstream;
51
52 TimerDisposable(final MaybeObserver<? super Long> downstream) {
53 this.downstream = downstream;
54 }
55
56 @Override
57 public void run() {
58 downstream.onSuccess(0L);
59 }
60
61 @Override
62 public void dispose() {
63 DisposableHelper.dispose(this);
64 }
65
66 @Override
67 public boolean isDisposed() {
68 return DisposableHelper.isDisposed(get());
69 }
70
71 void setFuture(Disposable d) {
72 DisposableHelper.replace(this, d);
73 }
74 }
75 }