Debian Codegraph @

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