Debian Codegraph @

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