Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableFromIterable.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.observable;
15
16 import java.util.Iterator;
17 import java.util.Objects;
18
19 import io.reactivex.rxjava3.annotations.Nullable;
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.internal.disposables.EmptyDisposable;
23 import io.reactivex.rxjava3.internal.observers.BasicQueueDisposable;
24
25 public final class ObservableFromIterable<T> extends Observable<T> {
26 final Iterable<? extends T> source;
27 public ObservableFromIterable(Iterable<? extends T> source) {
28 this.source = source;
29 }
30
31 @Override
32 public void subscribeActual(Observer<? super T> observer) {
33 Iterator<? extends T> it;
34 try {
35 it = source.iterator();
36 } catch (Throwable e) {
37 Exceptions.throwIfFatal(e);
38 EmptyDisposable.error(e, observer);
39 return;
40 }
41 boolean hasNext;
42 try {
43 hasNext = it.hasNext();
44 } catch (Throwable e) {
45 Exceptions.throwIfFatal(e);
46 EmptyDisposable.error(e, observer);
47 return;
48 }
49 if (!hasNext) {
50 EmptyDisposable.complete(observer);
51 return;
52 }
53
54 FromIterableDisposable<T> d = new FromIterableDisposable<>(observer, it);
55 observer.onSubscribe(d);
56
57 if (!d.fusionMode) {
58 d.run();
59 }
60 }
61
62 static final class FromIterableDisposable<T> extends BasicQueueDisposable<T> {
63
64 final Observer<? super T> downstream;
65
66 final Iterator<? extends T> it;
67
68 volatile boolean disposed;
69
70 boolean fusionMode;
71
72 boolean done;
73
74 boolean checkNext;
75
76 FromIterableDisposable(Observer<? super T> actual, Iterator<? extends T> it) {
77 this.downstream = actual;
78 this.it = it;
79 }
80
81 void run() {
82 boolean hasNext;
83
84 do {
85 if (isDisposed()) {
86 return;
87 }
88 T v;
89
90 try {
91 v = Objects.requireNonNull(it.next(), "The iterator returned a null value");
92 } catch (Throwable e) {
93 Exceptions.throwIfFatal(e);
94 downstream.onError(e);
95 return;
96 }
97
98 downstream.onNext(v);
99
100 if (isDisposed()) {
101 return;
102 }
103 try {
104 hasNext = it.hasNext();
105 } catch (Throwable e) {
106 Exceptions.throwIfFatal(e);
107 downstream.onError(e);
108 return;
109 }
110 } while (hasNext);
111
112 if (!isDisposed()) {
113 downstream.onComplete();
114 }
115 }
116
117 @Override
118 public int requestFusion(int mode) {
119 if ((mode & SYNC) != 0) {
120 fusionMode = true;
121 return SYNC;
122 }
123 return NONE;
124 }
125
126 @Nullable
127 @Override
128 public T poll() {
129 if (done) {
130 return null;
131 }
132 if (checkNext) {
133 if (!it.hasNext()) {
134 done = true;
135 return null;
136 }
137 } else {
138 checkNext = true;
139 }
140
141 return Objects.requireNonNull(it.next(), "The iterator returned a null value");
142 }
143
144 @Override
145 public boolean isEmpty() {
146 return done;
147 }
148
149 @Override
150 public void clear() {
151 done = true;
152 }
153
154 @Override
155 public void dispose() {
156 disposed = true;
157 }
158
159 @Override
160 public boolean isDisposed() {
161 return disposed;
162 }
163 }
164 }