Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableRangeLong.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 org.reactivestreams.Subscriber;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.core.Flowable;
20 import io.reactivex.rxjava3.internal.subscriptions.*;
21 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
22 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
23
24 /**
25 * Emits a range of long values.
26 */
27 public final class FlowableRangeLong extends Flowable<Long> {
28 final long start;
29 final long end;
30
31 public FlowableRangeLong(long start, long count) {
32 this.start = start;
33 this.end = start + count;
34 }
35
36 @Override
37 public void subscribeActual(Subscriber<? super Long> s) {
38 if (s instanceof ConditionalSubscriber) {
39 s.onSubscribe(new RangeConditionalSubscription(
40 (ConditionalSubscriber<? super Long>)s, start, end));
41 } else {
42 s.onSubscribe(new RangeSubscription(s, start, end));
43 }
44 }
45
46 abstract static class BaseRangeSubscription extends BasicQueueSubscription<Long> {
47
48 private static final long serialVersionUID = -2252972430506210021L;
49
50 final long end;
51
52 long index;
53
54 volatile boolean cancelled;
55
56 BaseRangeSubscription(long index, long end) {
57 this.index = index;
58 this.end = end;
59 }
60
61 @Override
62 public final int requestFusion(int mode) {
63 return mode & SYNC;
64 }
65
66 @Nullable
67 @Override
68 public final Long poll() {
69 long i = index;
70 if (i == end) {
71 return null;
72 }
73 index = i + 1;
74 return i;
75 }
76
77 @Override
78 public final boolean isEmpty() {
79 return index == end;
80 }
81
82 @Override
83 public final void clear() {
84 index = end;
85 }
86
87 @Override
88 public final void request(long n) {
89 if (SubscriptionHelper.validate(n)) {
90 if (BackpressureHelper.add(this, n) == 0L) {
91 if (n == Long.MAX_VALUE) {
92 fastPath();
93 } else {
94 slowPath(n);
95 }
96 }
97 }
98 }
99
100 @Override
101 public final void cancel() {
102 cancelled = true;
103 }
104
105 abstract void fastPath();
106
107 abstract void slowPath(long r);
108 }
109
110 static final class RangeSubscription extends BaseRangeSubscription {
111
112 private static final long serialVersionUID = 2587302975077663557L;
113
114 final Subscriber<? super Long> downstream;
115
116 RangeSubscription(Subscriber<? super Long> actual, long index, long end) {
117 super(index, end);
118 this.downstream = actual;
119 }
120
121 @Override
122 void fastPath() {
123 long f = end;
124 Subscriber<? super Long> a = downstream;
125
126 for (long i = index; i != f; i++) {
127 if (cancelled) {
128 return;
129 }
130 a.onNext(i);
131 }
132 if (cancelled) {
133 return;
134 }
135 a.onComplete();
136 }
137
138 @Override
139 void slowPath(long r) {
140 long e = 0;
141 long f = end;
142 long i = index;
143 Subscriber<? super Long> a = downstream;
144
145 for (;;) {
146
147 while (e != r && i != f) {
148 if (cancelled) {
149 return;
150 }
151
152 a.onNext(i);
153
154 e++;
155 i++;
156 }
157
158 if (i == f) {
159 if (!cancelled) {
160 a.onComplete();
161 }
162 return;
163 }
164
165 r = get();
166 if (e == r) {
167 index = i;
168 r = addAndGet(-e);
169 if (r == 0L) {
170 return;
171 }
172 e = 0L;
173 }
174 }
175 }
176 }
177
178 static final class RangeConditionalSubscription extends BaseRangeSubscription {
179
180 private static final long serialVersionUID = 2587302975077663557L;
181
182 final ConditionalSubscriber<? super Long> downstream;
183
184 RangeConditionalSubscription(ConditionalSubscriber<? super Long> actual, long index, long end) {
185 super(index, end);
186 this.downstream = actual;
187 }
188
189 @Override
190 void fastPath() {
191 long f = end;
192 ConditionalSubscriber<? super Long> a = downstream;
193
194 for (long i = index; i != f; i++) {
195 if (cancelled) {
196 return;
197 }
198 a.tryOnNext(i);
199 }
200 if (cancelled) {
201 return;
202 }
203 a.onComplete();
204 }
205
206 @Override
207 void slowPath(long r) {
208 long e = 0;
209 long f = end;
210 long i = index;
211 ConditionalSubscriber<? super Long> a = downstream;
212
213 for (;;) {
214
215 while (e != r && i != f) {
216 if (cancelled) {
217 return;
218 }
219
220 if (a.tryOnNext(i)) {
221 e++;
222 }
223
224 i++;
225 }
226
227 if (i == f) {
228 if (!cancelled) {
229 a.onComplete();
230 }
231 return;
232 }
233
234 r = get();
235 if (e == r) {
236 index = i;
237 r = addAndGet(-e);
238 if (r == 0) {
239 return;
240 }
241 e = 0;
242 }
243 }
244 }
245 }
246 }