| 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.operators.parallel; |
| 15 |
|
| 16 |
import org.reactivestreams.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.functions.Function; |
| 19 |
import io.reactivex.rxjava3.internal.operators.flowable.FlowableFlatMap; |
| 20 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 21 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 22 |
|
| 23 |
/** |
| 24 |
* Flattens the generated Publishers on each rail. |
| 25 |
* |
| 26 |
* @param <T> the input value type |
| 27 |
* @param <R> the output value type |
| 28 |
*/ |
| 29 |
public final class ParallelFlatMap<T, R> extends ParallelFlowable<R> { |
| 30 |
|
| 31 |
final ParallelFlowable<T> source; |
| 32 |
|
| 33 |
final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 34 |
|
| 35 |
final boolean delayError; |
| 36 |
|
| 37 |
final int maxConcurrency; |
| 38 |
|
| 39 |
final int prefetch; |
| 40 |
|
| 41 |
public ParallelFlatMap( |
| 42 |
ParallelFlowable<T> source, |
| 43 |
Function<? super T, ? extends Publisher<? extends R>> mapper, |
| 44 |
boolean delayError, |
| 45 |
int maxConcurrency, |
| 46 |
int prefetch) { |
| 47 |
this.source = source; |
| 48 |
this.mapper = mapper; |
| 49 |
this.delayError = delayError; |
| 50 |
this.maxConcurrency = maxConcurrency; |
| 51 |
this.prefetch = prefetch; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public int parallelism() { |
| 56 |
return source.parallelism(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void subscribe(Subscriber<? super R>[] subscribers) { |
| 61 |
subscribers = RxJavaPlugins.onSubscribe(this, subscribers); |
| 62 |
|
| 63 |
if (!validate(subscribers)) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
int n = subscribers.length; |
| 68 |
|
| 69 |
@SuppressWarnings("unchecked") |
| 70 |
final Subscriber<T>[] parents = new Subscriber[n]; |
| 71 |
|
| 72 |
for (int i = 0; i < n; i++) { |
| 73 |
parents[i] = FlowableFlatMap.subscribe(subscribers[i], mapper, delayError, maxConcurrency, prefetch); |
| 74 |
} |
| 75 |
|
| 76 |
source.subscribe(parents); |
| 77 |
} |
| 78 |
} |