Debian Codegraph @

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