Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableFromIterable.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.Iterator;
17 import java.util.Objects;
18
19 import org.reactivestreams.Subscriber;
20
21 import io.reactivex.rxjava3.annotations.Nullable;
22 import io.reactivex.rxjava3.core.Flowable;
23 import io.reactivex.rxjava3.exceptions.Exceptions;
24 import io.reactivex.rxjava3.internal.subscriptions.*;
25 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
26 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
27
28 public final class FlowableFromIterable<T> extends Flowable<T> {
29
30 final Iterable<? extends T> source;
31
32 public FlowableFromIterable(Iterable<? extends T> source) {
33 this.source = source;
34 }
35
36 @Override
37 public void subscribeActual(Subscriber<? super T> s) {
38 Iterator<? extends T> it;
39 try {
40 it = source.iterator();
41 } catch (Throwable e) {
42 Exceptions.throwIfFatal(e);
43 EmptySubscription.error(e, s);
44 return;
45 }
46
47 subscribe(s, it);
48 }
49
50 public static <T> void subscribe(Subscriber<? super T> s, Iterator<? extends T> it) {
51 boolean hasNext;
52 try {
53 hasNext = it.hasNext();
54 } catch (Throwable e) {
55 Exceptions.throwIfFatal(e);
56 EmptySubscription.error(e, s);
57 return;
58 }
59
60 if (!hasNext) {
61 EmptySubscription.complete(s);
62 return;
63 }
64
65 if (s instanceof ConditionalSubscriber) {
66 s.onSubscribe(new IteratorConditionalSubscription<T>(
67 (ConditionalSubscriber<? super T>)s, it));
68 } else {
69 s.onSubscribe(new IteratorSubscription<T>(s, it));
70 }
71 }
72
73 abstract static class BaseRangeSubscription<T> extends BasicQueueSubscription<T> {
74 private static final long serialVersionUID = -2252972430506210021L;
75
76 Iterator<? extends T> iterator;
77
78 volatile boolean cancelled;
79
80 boolean once;
81
82 BaseRangeSubscription(Iterator<? extends T> it) {
83 this.iterator = it;
84 }
85
86 @Override
87 public final int requestFusion(int mode) {
88 return mode & SYNC;
89 }
90
91 @Nullable
92 @Override
93 public final T poll() {
94 if (iterator == null) {
95 return null;
96 }
97 if (!once) {
98 once = true;
99 } else {
100 if (!iterator.hasNext()) {
101 return null;
102 }
103 }
104 return Objects.requireNonNull(iterator.next(), "Iterator.next() returned a null value");
105 }
106
107 @Override
108 public final boolean isEmpty() {
109 Iterator<? extends T> it = this.iterator;
110 if (it != null) {
111 if (!once || it.hasNext()) {
112 return false;
113 }
114 clear();
115 }
116 return true;
117 }
118
119 @Override
120 public final void clear() {
121 iterator = null;
122 }
123
124 @Override
125 public final void request(long n) {
126 if (SubscriptionHelper.validate(n)) {
127 if (BackpressureHelper.add(this, n) == 0L) {
128 if (n == Long.MAX_VALUE) {
129 fastPath();
130 } else {
131 slowPath(n);
132 }
133 }
134 }
135 }
136
137 @Override
138 public final void cancel() {
139 cancelled = true;
140 }
141
142 abstract void fastPath();
143
144 abstract void slowPath(long r);
145 }
146
147 static final class IteratorSubscription<T> extends BaseRangeSubscription<T> {
148
149 private static final long serialVersionUID = -6022804456014692607L;
150
151 final Subscriber<? super T> downstream;
152
153 IteratorSubscription(Subscriber<? super T> actual, Iterator<? extends T> it) {
154 super(it);
155 this.downstream = actual;
156 }
157
158 @Override
159 void fastPath() {
160 Iterator<? extends T> it = this.iterator;
161 Subscriber<? super T> a = downstream;
162 for (;;) {
163 if (cancelled) {
164 return;
165 }
166
167 T t;
168
169 try {
170 t = it.next();
171 } catch (Throwable ex) {
172 Exceptions.throwIfFatal(ex);
173 a.onError(ex);
174 return;
175 }
176
177 if (cancelled) {
178 return;
179 }
180
181 if (t == null) {
182 a.onError(new NullPointerException("Iterator.next() returned a null value"));
183 return;
184 } else {
185 a.onNext(t);
186 }
187
188 if (cancelled) {
189 return;
190 }
191
192 boolean b;
193
194 try {
195 b = it.hasNext();
196 } catch (Throwable ex) {
197 Exceptions.throwIfFatal(ex);
198 a.onError(ex);
199 return;
200 }
201
202 if (!b) {
203 if (!cancelled) {
204 a.onComplete();
205 }
206 return;
207 }
208 }
209 }
210
211 @Override
212 void slowPath(long r) {
213 long e = 0L;
214 Iterator<? extends T> it = this.iterator;
215 Subscriber<? super T> a = downstream;
216
217 for (;;) {
218
219 while (e != r) {
220
221 if (cancelled) {
222 return;
223 }
224
225 T t;
226
227 try {
228 t = it.next();
229 } catch (Throwable ex) {
230 Exceptions.throwIfFatal(ex);
231 a.onError(ex);
232 return;
233 }
234
235 if (cancelled) {
236 return;
237 }
238
239 if (t == null) {
240 a.onError(new NullPointerException("Iterator.next() returned a null value"));
241 return;
242 } else {
243 a.onNext(t);
244 }
245
246 if (cancelled) {
247 return;
248 }
249
250 boolean b;
251
252 try {
253 b = it.hasNext();
254 } catch (Throwable ex) {
255 Exceptions.throwIfFatal(ex);
256 a.onError(ex);
257 return;
258 }
259
260 if (!b) {
261 if (!cancelled) {
262 a.onComplete();
263 }
264 return;
265 }
266
267 e++;
268 }
269
270 r = get();
271 if (e == r) {
272 r = addAndGet(-e);
273 if (r == 0L) {
274 return;
275 }
276 e = 0L;
277 }
278 }
279 }
280
281 }
282
283 static final class IteratorConditionalSubscription<T> extends BaseRangeSubscription<T> {
284
285 private static final long serialVersionUID = -6022804456014692607L;
286
287 final ConditionalSubscriber<? super T> downstream;
288
289 IteratorConditionalSubscription(ConditionalSubscriber<? super T> actual, Iterator<? extends T> it) {
290 super(it);
291 this.downstream = actual;
292 }
293
294 @Override
295 void fastPath() {
296 Iterator<? extends T> it = this.iterator;
297 ConditionalSubscriber<? super T> a = downstream;
298 for (;;) {
299 if (cancelled) {
300 return;
301 }
302
303 T t;
304
305 try {
306 t = it.next();
307 } catch (Throwable ex) {
308 Exceptions.throwIfFatal(ex);
309 a.onError(ex);
310 return;
311 }
312
313 if (cancelled) {
314 return;
315 }
316
317 if (t == null) {
318 a.onError(new NullPointerException("Iterator.next() returned a null value"));
319 return;
320 } else {
321 a.tryOnNext(t);
322 }
323
324 if (cancelled) {
325 return;
326 }
327
328 boolean b;
329
330 try {
331 b = it.hasNext();
332 } catch (Throwable ex) {
333 Exceptions.throwIfFatal(ex);
334 a.onError(ex);
335 return;
336 }
337
338 if (!b) {
339 if (!cancelled) {
340 a.onComplete();
341 }
342 return;
343 }
344 }
345 }
346
347 @Override
348 void slowPath(long r) {
349 long e = 0L;
350 Iterator<? extends T> it = this.iterator;
351 ConditionalSubscriber<? super T> a = downstream;
352
353 for (;;) {
354
355 while (e != r) {
356
357 if (cancelled) {
358 return;
359 }
360
361 T t;
362
363 try {
364 t = it.next();
365 } catch (Throwable ex) {
366 Exceptions.throwIfFatal(ex);
367 a.onError(ex);
368 return;
369 }
370
371 if (cancelled) {
372 return;
373 }
374
375 boolean b;
376 if (t == null) {
377 a.onError(new NullPointerException("Iterator.next() returned a null value"));
378 return;
379 } else {
380 b = a.tryOnNext(t);
381 }
382
383 if (cancelled) {
384 return;
385 }
386
387 boolean hasNext;
388
389 try {
390 hasNext = it.hasNext();
391 } catch (Throwable ex) {
392 Exceptions.throwIfFatal(ex);
393 a.onError(ex);
394 return;
395 }
396
397 if (!hasNext) {
398 if (!cancelled) {
399 a.onComplete();
400 }
401 return;
402 }
403
404 if (b) {
405 e++;
406 }
407 }
408
409 r = get();
410 if (e == r) {
411 r = addAndGet(-e);
412 if (r == 0L) {
413 return;
414 }
415 e = 0L;
416 }
417 }
418 }
419
420 }
421 }