Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableIntervalRange.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.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.core.Scheduler.Worker;
23 import io.reactivex.rxjava3.disposables.Disposable;
24 import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
25 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
26 import io.reactivex.rxjava3.internal.schedulers.TrampolineScheduler;
27 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
28 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
29
30 public final class FlowableIntervalRange extends Flowable<Long> {
31 final Scheduler scheduler;
32 final long start;
33 final long end;
34 final long initialDelay;
35 final long period;
36 final TimeUnit unit;
37
38 public FlowableIntervalRange(long start, long end, long initialDelay, long period, TimeUnit unit, Scheduler scheduler) {
39 this.initialDelay = initialDelay;
40 this.period = period;
41 this.unit = unit;
42 this.scheduler = scheduler;
43 this.start = start;
44 this.end = end;
45 }
46
47 @Override
48 public void subscribeActual(Subscriber<? super Long> s) {
49 IntervalRangeSubscriber is = new IntervalRangeSubscriber(s, start, end);
50 s.onSubscribe(is);
51
52 Scheduler sch = scheduler;
53
54 if (sch instanceof TrampolineScheduler) {
55 Worker worker = sch.createWorker();
56 is.setResource(worker);
57 worker.schedulePeriodically(is, initialDelay, period, unit);
58 } else {
59 Disposable d = sch.schedulePeriodicallyDirect(is, initialDelay, period, unit);
60 is.setResource(d);
61 }
62 }
63
64 static final class IntervalRangeSubscriber extends AtomicLong
65 implements Subscription, Runnable {
66
67 private static final long serialVersionUID = -2809475196591179431L;
68
69 final Subscriber<? super Long> downstream;
70 final long end;
71
72 long count;
73
74 final AtomicReference<Disposable> resource = new AtomicReference<>();
75
76 IntervalRangeSubscriber(Subscriber<? super Long> actual, long start, long end) {
77 this.downstream = actual;
78 this.count = start;
79 this.end = end;
80 }
81
82 @Override
83 public void request(long n) {
84 if (SubscriptionHelper.validate(n)) {
85 BackpressureHelper.add(this, n);
86 }
87 }
88
89 @Override
90 public void cancel() {
91 DisposableHelper.dispose(resource);
92 }
93
94 @Override
95 public void run() {
96 if (resource.get() != DisposableHelper.DISPOSED) {
97 long r = get();
98
99 if (r != 0L) {
100 long c = count;
101 downstream.onNext(c);
102
103 if (c == end) {
104 if (resource.get() != DisposableHelper.DISPOSED) {
105 downstream.onComplete();
106 }
107 DisposableHelper.dispose(resource);
108 return;
109 }
110
111 count = c + 1;
112
113 if (r != Long.MAX_VALUE) {
114 decrementAndGet();
115 }
116 } else {
117 downstream.onError(new MissingBackpressureException("Can't deliver value " + count + " due to lack of requests"));
118 DisposableHelper.dispose(resource);
119 }
120 }
121 }
122
123 public void setResource(Disposable d) {
124 DisposableHelper.setOnce(resource, d);
125 }
126 }
127 }