Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/parallel/ParallelMapTry.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.exceptions.*;
19 import io.reactivex.rxjava3.functions.*;
20 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
21 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
22 import io.reactivex.rxjava3.parallel.*;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 import java.util.Objects;
26
27 /**
28 * Maps each 'rail' of the source ParallelFlowable with a mapper function
29 * and handle any failure based on a handler function.
30 * <p>History: 2.0.8 - experimental
31 * @param <T> the input value type
32 * @param <R> the output value type
33 * @since 2.2
34 */
35 public final class ParallelMapTry<T, R> extends ParallelFlowable<R> {
36
37 final ParallelFlowable<T> source;
38
39 final Function<? super T, ? extends R> mapper;
40
41 final BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler;
42
43 public ParallelMapTry(ParallelFlowable<T> source, Function<? super T, ? extends R> mapper,
44 BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler) {
45 this.source = source;
46 this.mapper = mapper;
47 this.errorHandler = errorHandler;
48 }
49
50 @Override
51 public void subscribe(Subscriber<? super R>[] subscribers) {
52 subscribers = RxJavaPlugins.onSubscribe(this, subscribers);
53
54 if (!validate(subscribers)) {
55 return;
56 }
57
58 int n = subscribers.length;
59 @SuppressWarnings("unchecked")
60 Subscriber<? super T>[] parents = new Subscriber[n];
61
62 for (int i = 0; i < n; i++) {
63 Subscriber<? super R> a = subscribers[i];
64 if (a instanceof ConditionalSubscriber) {
65 parents[i] = new ParallelMapTryConditionalSubscriber<T, R>((ConditionalSubscriber<? super R>)a, mapper, errorHandler);
66 } else {
67 parents[i] = new ParallelMapTrySubscriber<T, R>(a, mapper, errorHandler);
68 }
69 }
70
71 source.subscribe(parents);
72 }
73
74 @Override
75 public int parallelism() {
76 return source.parallelism();
77 }
78
79 static final class ParallelMapTrySubscriber<T, R> implements ConditionalSubscriber<T>, Subscription {
80
81 final Subscriber<? super R> downstream;
82
83 final Function<? super T, ? extends R> mapper;
84
85 final BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler;
86
87 Subscription upstream;
88
89 boolean done;
90
91 ParallelMapTrySubscriber(Subscriber<? super R> actual, Function<? super T, ? extends R> mapper,
92 BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler) {
93 this.downstream = actual;
94 this.mapper = mapper;
95 this.errorHandler = errorHandler;
96 }
97
98 @Override
99 public void request(long n) {
100 upstream.request(n);
101 }
102
103 @Override
104 public void cancel() {
105 upstream.cancel();
106 }
107
108 @Override
109 public void onSubscribe(Subscription s) {
110 if (SubscriptionHelper.validate(this.upstream, s)) {
111 this.upstream = s;
112
113 downstream.onSubscribe(this);
114 }
115 }
116
117 @Override
118 public void onNext(T t) {
119 if (!tryOnNext(t) && !done) {
120 upstream.request(1);
121 }
122 }
123
124 @Override
125 public boolean tryOnNext(T t) {
126 if (done) {
127 return false;
128 }
129 long retries = 0;
130
131 for (;;) {
132 R v;
133
134 try {
135 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value");
136 } catch (Throwable ex) {
137 Exceptions.throwIfFatal(ex);
138
139 ParallelFailureHandling h;
140
141 try {
142 h = Objects.requireNonNull(errorHandler.apply(++retries, ex), "The errorHandler returned a null ParallelFailureHandling");
143 } catch (Throwable exc) {
144 Exceptions.throwIfFatal(exc);
145 cancel();
146 onError(new CompositeException(ex, exc));
147 return false;
148 }
149
150 switch (h) {
151 case RETRY:
152 continue;
153 case SKIP:
154 return false;
155 case STOP:
156 cancel();
157 onComplete();
158 return false;
159 default:
160 cancel();
161 onError(ex);
162 return false;
163 }
164 }
165
166 downstream.onNext(v);
167 return true;
168 }
169 }
170
171 @Override
172 public void onError(Throwable t) {
173 if (done) {
174 RxJavaPlugins.onError(t);
175 return;
176 }
177 done = true;
178 downstream.onError(t);
179 }
180
181 @Override
182 public void onComplete() {
183 if (done) {
184 return;
185 }
186 done = true;
187 downstream.onComplete();
188 }
189
190 }
191 static final class ParallelMapTryConditionalSubscriber<T, R> implements ConditionalSubscriber<T>, Subscription {
192
193 final ConditionalSubscriber<? super R> downstream;
194
195 final Function<? super T, ? extends R> mapper;
196
197 final BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler;
198
199 Subscription upstream;
200
201 boolean done;
202
203 ParallelMapTryConditionalSubscriber(ConditionalSubscriber<? super R> actual,
204 Function<? super T, ? extends R> mapper,
205 BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler) {
206 this.downstream = actual;
207 this.mapper = mapper;
208 this.errorHandler = errorHandler;
209 }
210
211 @Override
212 public void request(long n) {
213 upstream.request(n);
214 }
215
216 @Override
217 public void cancel() {
218 upstream.cancel();
219 }
220
221 @Override
222 public void onSubscribe(Subscription s) {
223 if (SubscriptionHelper.validate(this.upstream, s)) {
224 this.upstream = s;
225
226 downstream.onSubscribe(this);
227 }
228 }
229
230 @Override
231 public void onNext(T t) {
232 if (!tryOnNext(t) && !done) {
233 upstream.request(1);
234 }
235 }
236
237 @Override
238 public boolean tryOnNext(T t) {
239 if (done) {
240 return false;
241 }
242 long retries = 0;
243
244 for (;;) {
245 R v;
246
247 try {
248 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value");
249 } catch (Throwable ex) {
250 Exceptions.throwIfFatal(ex);
251
252 ParallelFailureHandling h;
253
254 try {
255 h = Objects.requireNonNull(errorHandler.apply(++retries, ex), "The errorHandler returned a null ParallelFailureHandling");
256 } catch (Throwable exc) {
257 Exceptions.throwIfFatal(exc);
258 cancel();
259 onError(new CompositeException(ex, exc));
260 return false;
261 }
262
263 switch (h) {
264 case RETRY:
265 continue;
266 case SKIP:
267 return false;
268 case STOP:
269 cancel();
270 onComplete();
271 return false;
272 default:
273 cancel();
274 onError(ex);
275 return false;
276 }
277 }
278
279 return downstream.tryOnNext(v);
280 }
281 }
282
283 @Override
284 public void onError(Throwable t) {
285 if (done) {
286 RxJavaPlugins.onError(t);
287 return;
288 }
289 done = true;
290 downstream.onError(t);
291 }
292
293 @Override
294 public void onComplete() {
295 if (done) {
296 return;
297 }
298 done = true;
299 downstream.onComplete();
300 }
301
302 }
303 }