Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableTimer.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.flowable;
15
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.disposables.Disposable;
23 import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
24 import io.reactivex.rxjava3.internal.disposables.*;
25 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
26
27 public final class FlowableTimer extends Flowable<Long> {
28 final Scheduler scheduler;
29 final long delay;
30 final TimeUnit unit;
31 public FlowableTimer(long delay, TimeUnit unit, Scheduler scheduler) {
32 this.delay = delay;
33 this.unit = unit;
34 this.scheduler = scheduler;
35 }
36
37 @Override
38 public void subscribeActual(Subscriber<? super Long> s) {
39 TimerSubscriber ios = new TimerSubscriber(s);
40 s.onSubscribe(ios);
41
42 Disposable d = scheduler.scheduleDirect(ios, delay, unit);
43
44 ios.setResource(d);
45 }
46
47 static final class TimerSubscriber extends AtomicReference<Disposable>
48 implements Subscription, Runnable {
49
50 private static final long serialVersionUID = -2809475196591179431L;
51
52 final Subscriber<? super Long> downstream;
53
54 volatile boolean requested;
55
56 TimerSubscriber(Subscriber<? super Long> downstream) {
57 this.downstream = downstream;
58 }
59
60 @Override
61 public void request(long n) {
62 if (SubscriptionHelper.validate(n)) {
63 requested = true;
64 }
65 }
66
67 @Override
68 public void cancel() {
69 DisposableHelper.dispose(this);
70 }
71
72 @Override
73 public void run() {
74 if (get() != DisposableHelper.DISPOSED) {
75 if (requested) {
76 downstream.onNext(0L);
77 lazySet(EmptyDisposable.INSTANCE);
78 downstream.onComplete();
79 } else {
80 lazySet(EmptyDisposable.INSTANCE);
81 downstream.onError(new MissingBackpressureException("Can't deliver value due to lack of requests"));
82 }
83 }
84 }
85
86 public void setResource(Disposable d) {
87 DisposableHelper.trySet(this, d);
88 }
89 }
90 }