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