Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/parallel/ParallelMap.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.*;
17
18 import io.reactivex.rxjava3.core.FlowableSubscriber;
19 import io.reactivex.rxjava3.exceptions.Exceptions;
20 import io.reactivex.rxjava3.functions.Function;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
23 import io.reactivex.rxjava3.parallel.ParallelFlowable;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 import java.util.Objects;
27
28 /**
29 * Maps each 'rail' of the source ParallelFlowable with a mapper function.
30 *
31 * @param <T> the input value type
32 * @param <R> the output value type
33 */
34 public final class ParallelMap<T, R> extends ParallelFlowable<R> {
35
36 final ParallelFlowable<T> source;
37
38 final Function<? super T, ? extends R> mapper;
39
40 public ParallelMap(ParallelFlowable<T> source, Function<? super T, ? extends R> mapper) {
41 this.source = source;
42 this.mapper = mapper;
43 }
44
45 @Override
46 public void subscribe(Subscriber<? super R>[] subscribers) {
47 subscribers = RxJavaPlugins.onSubscribe(this, subscribers);
48
49 if (!validate(subscribers)) {
50 return;
51 }
52
53 int n = subscribers.length;
54 @SuppressWarnings("unchecked")
55 Subscriber<? super T>[] parents = new Subscriber[n];
56
57 for (int i = 0; i < n; i++) {
58 Subscriber<? super R> a = subscribers[i];
59 if (a instanceof ConditionalSubscriber) {
60 parents[i] = new ParallelMapConditionalSubscriber<T, R>((ConditionalSubscriber<? super R>)a, mapper);
61 } else {
62 parents[i] = new ParallelMapSubscriber<T, R>(a, mapper);
63 }
64 }
65
66 source.subscribe(parents);
67 }
68
69 @Override
70 public int parallelism() {
71 return source.parallelism();
72 }
73
74 static final class ParallelMapSubscriber<T, R> implements FlowableSubscriber<T>, Subscription {
75
76 final Subscriber<? super R> downstream;
77
78 final Function<? super T, ? extends R> mapper;
79
80 Subscription upstream;
81
82 boolean done;
83
84 ParallelMapSubscriber(Subscriber<? super R> actual, Function<? super T, ? extends R> mapper) {
85 this.downstream = actual;
86 this.mapper = mapper;
87 }
88
89 @Override
90 public void request(long n) {
91 upstream.request(n);
92 }
93
94 @Override
95 public void cancel() {
96 upstream.cancel();
97 }
98
99 @Override
100 public void onSubscribe(Subscription s) {
101 if (SubscriptionHelper.validate(this.upstream, s)) {
102 this.upstream = s;
103
104 downstream.onSubscribe(this);
105 }
106 }
107
108 @Override
109 public void onNext(T t) {
110 if (done) {
111 return;
112 }
113 R v;
114
115 try {
116 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value");
117 } catch (Throwable ex) {
118 Exceptions.throwIfFatal(ex);
119 cancel();
120 onError(ex);
121 return;
122 }
123
124 downstream.onNext(v);
125 }
126
127 @Override
128 public void onError(Throwable t) {
129 if (done) {
130 RxJavaPlugins.onError(t);
131 return;
132 }
133 done = true;
134 downstream.onError(t);
135 }
136
137 @Override
138 public void onComplete() {
139 if (done) {
140 return;
141 }
142 done = true;
143 downstream.onComplete();
144 }
145
146 }
147 static final class ParallelMapConditionalSubscriber<T, R> implements ConditionalSubscriber<T>, Subscription {
148
149 final ConditionalSubscriber<? super R> downstream;
150
151 final Function<? super T, ? extends R> mapper;
152
153 Subscription upstream;
154
155 boolean done;
156
157 ParallelMapConditionalSubscriber(ConditionalSubscriber<? super R> actual, Function<? super T, ? extends R> mapper) {
158 this.downstream = actual;
159 this.mapper = mapper;
160 }
161
162 @Override
163 public void request(long n) {
164 upstream.request(n);
165 }
166
167 @Override
168 public void cancel() {
169 upstream.cancel();
170 }
171
172 @Override
173 public void onSubscribe(Subscription s) {
174 if (SubscriptionHelper.validate(this.upstream, s)) {
175 this.upstream = s;
176
177 downstream.onSubscribe(this);
178 }
179 }
180
181 @Override
182 public void onNext(T t) {
183 if (done) {
184 return;
185 }
186 R v;
187
188 try {
189 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value");
190 } catch (Throwable ex) {
191 Exceptions.throwIfFatal(ex);
192 cancel();
193 onError(ex);
194 return;
195 }
196
197 downstream.onNext(v);
198 }
199
200 @Override
201 public boolean tryOnNext(T t) {
202 if (done) {
203 return false;
204 }
205 R v;
206
207 try {
208 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value");
209 } catch (Throwable ex) {
210 Exceptions.throwIfFatal(ex);
211 cancel();
212 onError(ex);
213 return false;
214 }
215
216 return downstream.tryOnNext(v);
217 }
218
219 @Override
220 public void onError(Throwable t) {
221 if (done) {
222 RxJavaPlugins.onError(t);
223 return;
224 }
225 done = true;
226 downstream.onError(t);
227 }
228
229 @Override
230 public void onComplete() {
231 if (done) {
232 return;
233 }
234 done = true;
235 downstream.onComplete();
236 }
237
238 }
239 }