Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/parallel/ParallelFlatMapIterable.java

raw
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.Subscriber;
17
18 import io.reactivex.rxjava3.functions.Function;
19 import io.reactivex.rxjava3.internal.operators.flowable.FlowableFlattenIterable;
20 import io.reactivex.rxjava3.parallel.ParallelFlowable;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 /**
24 * Flattens the generated {@link Iterable}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 ParallelFlatMapIterable<T, R> extends ParallelFlowable<R> {
31
32 final ParallelFlowable<T> source;
33
34 final Function<? super T, ? extends Iterable<? extends R>> mapper;
35
36 final int prefetch;
37
38 public ParallelFlatMapIterable(
39 ParallelFlowable<T> source,
40 Function<? super T, ? extends Iterable<? 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 subscribers = RxJavaPlugins.onSubscribe(this, subscribers);
55
56 if (!validate(subscribers)) {
57 return;
58 }
59
60 int n = subscribers.length;
61
62 @SuppressWarnings("unchecked")
63 final Subscriber<T>[] parents = new Subscriber[n];
64
65 for (int i = 0; i < n; i++) {
66 parents[i] = FlowableFlattenIterable.subscribe(subscribers[i], mapper, prefetch);
67 }
68
69 source.subscribe(parents);
70 }
71 }