Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/jdk8/ParallelMapTryOptional.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.jdk8;
15
16 import java.util.*;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.exceptions.*;
21 import io.reactivex.rxjava3.functions.*;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
24 import io.reactivex.rxjava3.parallel.*;
25 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
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 * @param <T> the input value type
31 * @param <R> the output value type
32 * @since 3.0.0
33 */
34 public final class ParallelMapTryOptional<T, R> extends ParallelFlowable<R> {
35
36 final ParallelFlowable<T> source;
37
38 final Function<? super T, Optional<? extends R>> mapper;
39
40 final BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler;
41
42 public ParallelMapTryOptional(
43 ParallelFlowable<T> source,
44 Function<? super T, Optional<? extends R>> mapper,
45 BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler) {
46 this.source = source;
47 this.mapper = mapper;
48 this.errorHandler = errorHandler;
49 }
50
51 @Override
52 public void subscribe(Subscriber<? super R>[] subscribers) {
53 if (!validate(subscribers)) {
54 return;
55 }
56
57 int n = subscribers.length;
58 @SuppressWarnings("unchecked")
59 Subscriber<? super T>[] parents = new Subscriber[n];
60
61 for (int i = 0; i < n; i++) {
62 Subscriber<? super R> a = subscribers[i];
63 if (a instanceof ConditionalSubscriber) {
64 parents[i] = new ParallelMapTryConditionalSubscriber<>((ConditionalSubscriber<? super R>)a, mapper, errorHandler);
65 } else {
66 parents[i] = new ParallelMapTrySubscriber<>(a, mapper, errorHandler);
67 }
68 }
69
70 source.subscribe(parents);
71 }
72
73 @Override
74 public int parallelism() {
75 return source.parallelism();
76 }
77
78 static final class ParallelMapTrySubscriber<T, R> implements ConditionalSubscriber<T>, Subscription {
79
80 final Subscriber<? super R> downstream;
81
82 final Function<? super T, Optional<? extends R>> mapper;
83
84 final BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler;
85
86 Subscription upstream;
87
88 boolean done;
89
90 ParallelMapTrySubscriber(Subscriber<? super R> actual,
91 Function<? super T, Optional<? 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 Optional<? extends R> v;
133
134 try {
135 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional");
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 if (v.isPresent()) {
167 downstream.onNext(v.get());
168 return true;
169 }
170 return false;
171 }
172 }
173
174 @Override
175 public void onError(Throwable t) {
176 if (done) {
177 RxJavaPlugins.onError(t);
178 return;
179 }
180 done = true;
181 downstream.onError(t);
182 }
183
184 @Override
185 public void onComplete() {
186 if (done) {
187 return;
188 }
189 done = true;
190 downstream.onComplete();
191 }
192
193 }
194 static final class ParallelMapTryConditionalSubscriber<T, R> implements ConditionalSubscriber<T>, Subscription {
195
196 final ConditionalSubscriber<? super R> downstream;
197
198 final Function<? super T, Optional<? extends R>> mapper;
199
200 final BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler;
201
202 Subscription upstream;
203
204 boolean done;
205
206 ParallelMapTryConditionalSubscriber(ConditionalSubscriber<? super R> actual,
207 Function<? super T, Optional<? extends R>> mapper,
208 BiFunction<? super Long, ? super Throwable, ParallelFailureHandling> errorHandler) {
209 this.downstream = actual;
210 this.mapper = mapper;
211 this.errorHandler = errorHandler;
212 }
213
214 @Override
215 public void request(long n) {
216 upstream.request(n);
217 }
218
219 @Override
220 public void cancel() {
221 upstream.cancel();
222 }
223
224 @Override
225 public void onSubscribe(Subscription s) {
226 if (SubscriptionHelper.validate(this.upstream, s)) {
227 this.upstream = s;
228
229 downstream.onSubscribe(this);
230 }
231 }
232
233 @Override
234 public void onNext(T t) {
235 if (!tryOnNext(t) && !done) {
236 upstream.request(1);
237 }
238 }
239
240 @Override
241 public boolean tryOnNext(T t) {
242 if (done) {
243 return false;
244 }
245 long retries = 0;
246
247 for (;;) {
248 Optional<? extends R> v;
249
250 try {
251 v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional");
252 } catch (Throwable ex) {
253 Exceptions.throwIfFatal(ex);
254
255 ParallelFailureHandling h;
256
257 try {
258 h = Objects.requireNonNull(errorHandler.apply(++retries, ex), "The errorHandler returned a null ParallelFailureHandling");
259 } catch (Throwable exc) {
260 Exceptions.throwIfFatal(exc);
261 cancel();
262 onError(new CompositeException(ex, exc));
263 return false;
264 }
265
266 switch (h) {
267 case RETRY:
268 continue;
269 case SKIP:
270 return false;
271 case STOP:
272 cancel();
273 onComplete();
274 return false;
275 default:
276 cancel();
277 onError(ex);
278 return false;
279 }
280 }
281
282 return v.isPresent() && downstream.tryOnNext(v.get());
283 }
284 }
285
286 @Override
287 public void onError(Throwable t) {
288 if (done) {
289 RxJavaPlugins.onError(t);
290 return;
291 }
292 done = true;
293 downstream.onError(t);
294 }
295
296 @Override
297 public void onComplete() {
298 if (done) {
299 return;
300 }
301 done = true;
302 downstream.onComplete();
303 }
304
305 }
306 }