forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFlowableFlatMapStream.java
345 lines (284 loc) · 11.3 KB
/
FlowableFlatMapStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.rxjava3.internal.jdk8;
import java.util.*;
import java.util.concurrent.atomic.*;
import java.util.stream.Stream;
import org.reactivestreams.*;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.functions.*;
import io.reactivex.rxjava3.internal.fuseable.*;
import io.reactivex.rxjava3.internal.queue.SpscArrayQueue;
import io.reactivex.rxjava3.internal.subscriptions.*;
import io.reactivex.rxjava3.internal.util.*;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
/**
* Maps the upstream values onto {@link Stream}s and emits their items in order to the downstream.
*
* @param <T> the upstream element type
* @param <R> the inner {@code Stream} and result element type
* @since 3.0.0
*/
public final class FlowableFlatMapStream<T, R> extends Flowable<R> {
final Flowable<T> source;
final Function<? super T, ? extends Stream<? extends R>> mapper;
final int prefetch;
public FlowableFlatMapStream(Flowable<T> source, Function<? super T, ? extends Stream<? extends R>> mapper, int prefetch) {
this.source = source;
this.mapper = mapper;
this.prefetch = prefetch;
}
@Override
protected void subscribeActual(Subscriber<? super R> s) {
if (source instanceof Supplier) {
Stream<? extends R> stream = null;
try {
@SuppressWarnings("unchecked")
T t = ((Supplier<T>)source).get();
if (t != null) {
stream = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Stream");
}
} catch (Throwable ex) {
EmptySubscription.error(ex, s);
return;
}
if (stream != null) {
FlowableFromStream.subscribeStream(s, stream);
} else {
EmptySubscription.complete(s);
}
} else {
source.subscribe(subscribe(s, mapper, prefetch));
}
}
/**
* Create a {@link Subscriber} with the given parameters.
* @param <T> the upstream value type
* @param <R> the {@link Stream} and output value type
* @param downstream the downstream {@code Subscriber} to wrap
* @param mapper the mapper function
* @param prefetch the number of items to prefetch
* @return the new {@code Subscriber}
*/
public static <T, R> Subscriber<T> subscribe(Subscriber<? super R> downstream, Function<? super T, ? extends Stream<? extends R>> mapper, int prefetch) {
return new FlatMapStreamSubscriber<>(downstream, mapper, prefetch);
}
static final class FlatMapStreamSubscriber<T, R> extends AtomicInteger
implements FlowableSubscriber<T>, Subscription {
private static final long serialVersionUID = -5127032662980523968L;
final Subscriber<? super R> downstream;
final Function<? super T, ? extends Stream<? extends R>> mapper;
final int prefetch;
final AtomicLong requested;
SimpleQueue<T> queue;
Subscription upstream;
Iterator<? extends R> currentIterator;
AutoCloseable currentCloseable;
volatile boolean cancelled;
volatile boolean upstreamDone;
final AtomicThrowable error;
long emitted;
int consumed;
int sourceMode;
FlatMapStreamSubscriber(Subscriber<? super R> downstream, Function<? super T, ? extends Stream<? extends R>> mapper, int prefetch) {
this.downstream = downstream;
this.mapper = mapper;
this.prefetch = prefetch;
this.requested = new AtomicLong();
this.error = new AtomicThrowable();
}
@Override
public void onSubscribe(@NonNull Subscription s) {
if (SubscriptionHelper.validate(this.upstream, s)) {
this.upstream = s;
if (s instanceof QueueSubscription) {
@SuppressWarnings("unchecked")
QueueSubscription<T> qs = (QueueSubscription<T>)s;
int m = qs.requestFusion(QueueFuseable.ANY | QueueFuseable.BOUNDARY);
if (m == QueueFuseable.SYNC) {
sourceMode = m;
queue = qs;
upstreamDone = true;
downstream.onSubscribe(this);
return;
}
else if (m == QueueFuseable.ASYNC) {
sourceMode = m;
queue = qs;
downstream.onSubscribe(this);
s.request(prefetch);
return;
}
}
queue = new SpscArrayQueue<>(prefetch);
downstream.onSubscribe(this);
s.request(prefetch);
}
}
@Override
public void onNext(T t) {
if (sourceMode != QueueFuseable.ASYNC) {
if (!queue.offer(t)) {
upstream.cancel();
onError(new MissingBackpressureException("Queue full?!"));
return;
}
}
drain();
}
@Override
public void onError(Throwable t) {
if (error.compareAndSet(null, t)) {
upstreamDone = true;
drain();
} else {
RxJavaPlugins.onError(t);
}
}
@Override
public void onComplete() {
upstreamDone = true;
drain();
}
@Override
public void request(long n) {
if (SubscriptionHelper.validate(n)) {
BackpressureHelper.add(requested, n);
drain();
}
}
@Override
public void cancel() {
cancelled = true;
upstream.cancel();
drain();
}
void drain() {
if (getAndIncrement() != 0) {
return;
}
int missed = 1;
final Subscriber<? super R> downstream = this.downstream;
final SimpleQueue<T> queue = this.queue;
final AtomicThrowable error = this.error;
Iterator<? extends R> iterator = this.currentIterator;
long requested = this.requested.get();
long emitted = this.emitted;
final int limit = prefetch - (prefetch >> 2);
boolean canRequest = sourceMode != QueueFuseable.SYNC;
for (;;) {
if (cancelled) {
queue.clear();
clearCurrentSuppressCloseError();
} else {
boolean isDone = upstreamDone;
if (error.get() != null) {
downstream.onError(error.get());
cancelled = true;
continue;
}
if (iterator == null) {
T t;
try {
t = queue.poll();
} catch (Throwable ex) {
trySignalError(downstream, ex);
continue;
}
boolean isEmpty = t == null;
if (isDone && isEmpty) {
downstream.onComplete();
cancelled = true;
}
else if (!isEmpty) {
if (canRequest && ++consumed == limit) {
consumed = 0;
upstream.request(limit);
}
Stream<? extends R> stream;
try {
stream = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Stream");
iterator = stream.iterator();
if (iterator.hasNext()) {
currentIterator = iterator;
currentCloseable = stream;
} else {
iterator = null;
}
} catch (Throwable ex) {
trySignalError(downstream, ex);
}
continue;
}
}
if (iterator != null && emitted != requested) {
R item;
try {
item = Objects.requireNonNull(iterator.next(), "The Stream.Iterator returned a null value");
} catch (Throwable ex) {
trySignalError(downstream, ex);
continue;
}
if (!cancelled) {
downstream.onNext(item);
emitted++;
if (!cancelled) {
try {
if (!iterator.hasNext()) {
iterator = null;
clearCurrentRethrowCloseError();
}
} catch (Throwable ex) {
trySignalError(downstream, ex);
}
}
}
continue;
}
}
this.emitted = emitted;
missed = addAndGet(-missed);
if (missed == 0) {
break;
}
requested = this.requested.get();
}
}
void clearCurrentRethrowCloseError() throws Throwable {
currentIterator = null;
AutoCloseable ac = currentCloseable;
currentCloseable = null;
if (ac != null) {
ac.close();
}
}
void clearCurrentSuppressCloseError() {
try {
clearCurrentRethrowCloseError();
} catch (Throwable ex) {
RxJavaPlugins.onError(ex);
}
}
void trySignalError(Subscriber<?> downstream, Throwable ex) {
if (error.compareAndSet(null, ex)) {
upstream.cancel();
cancelled = true;
downstream.onError(ex);
} else {
RxJavaPlugins.onError(ex);
}
}
}
}