Debian Codegraph @

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