Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleFlatMapIterableFlowable.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.single;
15
16 import java.util.Iterator;
17 import java.util.Objects;
18 import java.util.concurrent.atomic.AtomicLong;
19
20 import org.reactivestreams.Subscriber;
21
22 import io.reactivex.rxjava3.annotations.Nullable;
23 import io.reactivex.rxjava3.core.*;
24 import io.reactivex.rxjava3.disposables.Disposable;
25 import io.reactivex.rxjava3.exceptions.Exceptions;
26 import io.reactivex.rxjava3.functions.Function;
27 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
28 import io.reactivex.rxjava3.internal.subscriptions.*;
29 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
30
31 /**
32 * Maps a success value into an Iterable and streams it back as a Flowable.
33 *
34 * @param <T> the source value type
35 * @param <R> the element type of the Iterable
36 */
37 public final class SingleFlatMapIterableFlowable<T, R> extends Flowable<R> {
38
39 final SingleSource<T> source;
40
41 final Function<? super T, ? extends Iterable<? extends R>> mapper;
42
43 public SingleFlatMapIterableFlowable(SingleSource<T> source,
44 Function<? super T, ? extends Iterable<? extends R>> mapper) {
45 this.source = source;
46 this.mapper = mapper;
47 }
48
49 @Override
50 protected void subscribeActual(Subscriber<? super R> s) {
51 source.subscribe(new FlatMapIterableObserver<>(s, mapper));
52 }
53
54 static final class FlatMapIterableObserver<T, R>
55 extends BasicIntQueueSubscription<R>
56 implements SingleObserver<T> {
57
58 private static final long serialVersionUID = -8938804753851907758L;
59
60 final Subscriber<? super R> downstream;
61
62 final Function<? super T, ? extends Iterable<? extends R>> mapper;
63
64 final AtomicLong requested;
65
66 Disposable upstream;
67
68 volatile Iterator<? extends R> it;
69
70 volatile boolean cancelled;
71
72 boolean outputFused;
73
74 FlatMapIterableObserver(Subscriber<? super R> actual,
75 Function<? super T, ? extends Iterable<? extends R>> mapper) {
76 this.downstream = actual;
77 this.mapper = mapper;
78 this.requested = new AtomicLong();
79 }
80
81 @Override
82 public void onSubscribe(Disposable d) {
83 if (DisposableHelper.validate(this.upstream, d)) {
84 this.upstream = d;
85
86 downstream.onSubscribe(this);
87 }
88 }
89
90 @Override
91 public void onSuccess(T value) {
92 Iterator<? extends R> iterator;
93 boolean has;
94 try {
95 iterator = mapper.apply(value).iterator();
96
97 has = iterator.hasNext();
98 } catch (Throwable ex) {
99 Exceptions.throwIfFatal(ex);
100 downstream.onError(ex);
101 return;
102 }
103
104 if (!has) {
105 downstream.onComplete();
106 return;
107 }
108
109 this.it = iterator;
110 drain();
111 }
112
113 @Override
114 public void onError(Throwable e) {
115 upstream = DisposableHelper.DISPOSED;
116 downstream.onError(e);
117 }
118
119 @Override
120 public void request(long n) {
121 if (SubscriptionHelper.validate(n)) {
122 BackpressureHelper.add(requested, n);
123 drain();
124 }
125 }
126
127 @Override
128 public void cancel() {
129 cancelled = true;
130 upstream.dispose();
131 upstream = DisposableHelper.DISPOSED;
132 }
133
134 void drain() {
135 if (getAndIncrement() != 0) {
136 return;
137 }
138
139 Subscriber<? super R> a = downstream;
140 Iterator<? extends R> iterator = this.it;
141
142 if (outputFused && iterator != null) {
143 a.onNext(null);
144 a.onComplete();
145 return;
146 }
147
148 int missed = 1;
149
150 for (;;) {
151
152 if (iterator != null) {
153 long r = requested.get();
154 long e = 0L;
155
156 if (r == Long.MAX_VALUE) {
157 fastPath(a, iterator);
158 return;
159 }
160
161 while (e != r) {
162 if (cancelled) {
163 return;
164 }
165
166 R v;
167
168 try {
169 v = Objects.requireNonNull(iterator.next(), "The iterator returned a null value");
170 } catch (Throwable ex) {
171 Exceptions.throwIfFatal(ex);
172 a.onError(ex);
173 return;
174 }
175
176 a.onNext(v);
177
178 if (cancelled) {
179 return;
180 }
181
182 e++;
183
184 boolean b;
185
186 try {
187 b = iterator.hasNext();
188 } catch (Throwable ex) {
189 Exceptions.throwIfFatal(ex);
190 a.onError(ex);
191 return;
192 }
193
194 if (!b) {
195 a.onComplete();
196 return;
197 }
198 }
199
200 if (e != 0L) {
201 BackpressureHelper.produced(requested, e);
202 }
203 }
204
205 missed = addAndGet(-missed);
206 if (missed == 0) {
207 break;
208 }
209
210 if (iterator == null) {
211 iterator = it;
212 }
213 }
214 }
215
216 void fastPath(Subscriber<? super R> a, Iterator<? extends R> iterator) {
217 for (;;) {
218 if (cancelled) {
219 return;
220 }
221
222 R v;
223
224 try {
225 v = iterator.next();
226 } catch (Throwable ex) {
227 Exceptions.throwIfFatal(ex);
228 a.onError(ex);
229 return;
230 }
231
232 a.onNext(v);
233
234 if (cancelled) {
235 return;
236 }
237
238 boolean b;
239
240 try {
241 b = iterator.hasNext();
242 } catch (Throwable ex) {
243 Exceptions.throwIfFatal(ex);
244 a.onError(ex);
245 return;
246 }
247
248 if (!b) {
249 a.onComplete();
250 return;
251 }
252 }
253 }
254
255 @Override
256 public int requestFusion(int mode) {
257 if ((mode & ASYNC) != 0) {
258 outputFused = true;
259 return ASYNC;
260 }
261 return NONE;
262 }
263
264 @Override
265 public void clear() {
266 it = null;
267 }
268
269 @Override
270 public boolean isEmpty() {
271 return it == null;
272 }
273
274 @Nullable
275 @Override
276 public R poll() {
277 Iterator<? extends R> iterator = it;
278
279 if (iterator != null) {
280 R v = Objects.requireNonNull(iterator.next(), "The iterator returned a null value");
281 if (!iterator.hasNext()) {
282 it = null;
283 }
284 return v;
285 }
286 return null;
287 }
288
289 }
290 }