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