|
15 | 15 |
|
16 | 16 | import java.util.*;
|
17 | 17 | import java.util.concurrent.*;
|
| 18 | +import java.util.stream.*; |
18 | 19 |
|
19 | 20 | import org.reactivestreams.Publisher;
|
20 | 21 |
|
21 | 22 | import io.reactivex.rxjava3.annotations.*;
|
| 23 | +import io.reactivex.rxjava3.core.Observable; |
22 | 24 | import io.reactivex.rxjava3.disposables.Disposable;
|
23 | 25 | import io.reactivex.rxjava3.exceptions.Exceptions;
|
24 | 26 | import io.reactivex.rxjava3.functions.*;
|
@@ -2799,6 +2801,7 @@ public final <R> Flowable<R> flatMapPublisher(@NonNull Function<? super T, ? ext
|
2799 | 2801 | * source Single
|
2800 | 2802 | * @return the new Flowable instance
|
2801 | 2803 | * @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
|
| 2804 | + * @see #flattenStreamAsFlowable(Function) |
2802 | 2805 | */
|
2803 | 2806 | @BackpressureSupport(BackpressureKind.FULL)
|
2804 | 2807 | @CheckReturnValue
|
@@ -2826,6 +2829,7 @@ public final <U> Flowable<U> flattenAsFlowable(@NonNull Function<? super T, ? ex
|
2826 | 2829 | * source Single
|
2827 | 2830 | * @return the new Observable instance
|
2828 | 2831 | * @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a>
|
| 2832 | + * @see #flattenStreamAsObservable(Function) |
2829 | 2833 | */
|
2830 | 2834 | @CheckReturnValue
|
2831 | 2835 | @NonNull
|
@@ -4308,4 +4312,87 @@ private static <T> Single<T> toSingle(@NonNull Flowable<T> source) {
|
4308 | 4312 | public final CompletionStage<T> toCompletionStage() {
|
4309 | 4313 | return subscribeWith(new CompletionStageConsumer<>(false, null));
|
4310 | 4314 | }
|
| 4315 | + |
| 4316 | + /** |
| 4317 | + * Maps the upstream succecss value into a Java {@link Stream} and emits its |
| 4318 | + * items to the downstream consumer as a {@link Flowable}. |
| 4319 | + * <img width="640" height="247" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/flattenStreamAsFlowable.s.png" alt=""> |
| 4320 | + * <p> |
| 4321 | + * The operator closes the {@code Stream} upon cancellation and when it terminates. Exceptions raised when |
| 4322 | + * closing a {@code Stream} are routed to the global error handler ({@link RxJavaPlugins#onError(Throwable)}. |
| 4323 | + * If a {@code Stream} should not be closed, turn it into an {@link Iterable} and use {@link #flattenAsFlowable(Function)}: |
| 4324 | + * <pre><code> |
| 4325 | + * source.flattenAsFlowable(item -> createStream(item)::iterator); |
| 4326 | + * </code></pre> |
| 4327 | + * <p> |
| 4328 | + * Primitive streams are not supported and items have to be boxed manually (e.g., via {@link IntStream#boxed()}): |
| 4329 | + * <pre><code> |
| 4330 | + * source.flattenStreamAsFlowable(item -> IntStream.rangeClosed(1, 10).boxed()); |
| 4331 | + * </code></pre> |
| 4332 | + * <p> |
| 4333 | + * {@code Stream} does not support concurrent usage so creating and/or consuming the same instance multiple times |
| 4334 | + * from multiple threads can lead to undefined behavior. |
| 4335 | + * <dl> |
| 4336 | + * <dt><b>Backpressure:</b></dt> |
| 4337 | + * <dd>The operator honors backpressure from downstream and iterates the given {@code Stream} |
| 4338 | + * on demand (i.e., when requested).</dd> |
| 4339 | + * <dt><b>Scheduler:</b></dt> |
| 4340 | + * <dd>{@code flattenStreamAsFlowable} does not operate by default on a particular {@link Scheduler}.</dd> |
| 4341 | + * </dl> |
| 4342 | + * @param <R> the element type of the {@code Stream} and the output {@code Flowable} |
| 4343 | + * @param mapper the function that receives the upstream success item and should |
| 4344 | + * return a {@code Stream} of values to emit. |
| 4345 | + * @return the new Flowable instance |
| 4346 | + * @since 3.0.0 |
| 4347 | + * @see #flattenAsFlowable(Function) |
| 4348 | + * @see #flattenStreamAsObservable(Function) |
| 4349 | + */ |
| 4350 | + @CheckReturnValue |
| 4351 | + @SchedulerSupport(SchedulerSupport.NONE) |
| 4352 | + @BackpressureSupport(BackpressureKind.FULL) |
| 4353 | + @NonNull |
| 4354 | + public final <R> Flowable<R> flattenStreamAsFlowable(@NonNull Function<? super T, ? extends Stream<? extends R>> mapper) { |
| 4355 | + Objects.requireNonNull(mapper, "mapper is null"); |
| 4356 | + return RxJavaPlugins.onAssembly(new SingleFlattenStreamAsFlowable<>(this, mapper)); |
| 4357 | + } |
| 4358 | + |
| 4359 | + /** |
| 4360 | + * Maps the upstream succecss value into a Java {@link Stream} and emits its |
| 4361 | + * items to the downstream consumer as an {@link Observable}. |
| 4362 | + * <p> |
| 4363 | + * <img width="640" height="247" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/flattenStreamAsObservable.s.png" alt=""> |
| 4364 | + * <p> |
| 4365 | + * The operator closes the {@code Stream} upon cancellation and when it terminates. Exceptions raised when |
| 4366 | + * closing a {@code Stream} are routed to the global error handler ({@link RxJavaPlugins#onError(Throwable)}. |
| 4367 | + * If a {@code Stream} should not be closed, turn it into an {@link Iterable} and use {@link #flattenAsFlowable(Function)}: |
| 4368 | + * <pre><code> |
| 4369 | + * source.flattenAsObservable(item -> createStream(item)::iterator); |
| 4370 | + * </code></pre> |
| 4371 | + * <p> |
| 4372 | + * Primitive streams are not supported and items have to be boxed manually (e.g., via {@link IntStream#boxed()}): |
| 4373 | + * <pre><code> |
| 4374 | + * source.flattenStreamAsObservable(item -> IntStream.rangeClosed(1, 10).boxed()); |
| 4375 | + * </code></pre> |
| 4376 | + * <p> |
| 4377 | + * {@code Stream} does not support concurrent usage so creating and/or consuming the same instance multiple times |
| 4378 | + * from multiple threads can lead to undefined behavior. |
| 4379 | + * <dl> |
| 4380 | + * <dt><b>Scheduler:</b></dt> |
| 4381 | + * <dd>{@code flattenStreamAsObservable} does not operate by default on a particular {@link Scheduler}.</dd> |
| 4382 | + * </dl> |
| 4383 | + * @param <R> the element type of the {@code Stream} and the output {@code Observable} |
| 4384 | + * @param mapper the function that receives the upstream success item and should |
| 4385 | + * return a {@code Stream} of values to emit. |
| 4386 | + * @return the new Observable instance |
| 4387 | + * @since 3.0.0 |
| 4388 | + * @see #flattenAsObservable(Function) |
| 4389 | + * @see #flattenStreamAsFlowable(Function) |
| 4390 | + */ |
| 4391 | + @CheckReturnValue |
| 4392 | + @SchedulerSupport(SchedulerSupport.NONE) |
| 4393 | + @NonNull |
| 4394 | + public final <R> Observable<R> flattenStreamAsObservable(@NonNull Function<? super T, ? extends Stream<? extends R>> mapper) { |
| 4395 | + Objects.requireNonNull(mapper, "mapper is null"); |
| 4396 | + return RxJavaPlugins.onAssembly(new SingleFlattenStreamAsObservable<>(this, mapper)); |
| 4397 | + } |
4311 | 4398 | }
|
0 commit comments