Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableBuffer.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.flowable;
15
16 import java.util.*;
17 import java.util.concurrent.atomic.*;
18
19 import org.reactivestreams.*;
20
21 import io.reactivex.rxjava3.core.*;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.*;
24 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
25 import io.reactivex.rxjava3.internal.util.*;
26 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
27
28 public final class FlowableBuffer<T, C extends Collection<? super T>> extends AbstractFlowableWithUpstream<T, C> {
29 final int size;
30
31 final int skip;
32
33 final Supplier<C> bufferSupplier;
34
35 public FlowableBuffer(Flowable<T> source, int size, int skip, Supplier<C> bufferSupplier) {
36 super(source);
37 this.size = size;
38 this.skip = skip;
39 this.bufferSupplier = bufferSupplier;
40 }
41
42 @Override
43 public void subscribeActual(Subscriber<? super C> s) {
44 if (size == skip) {
45 source.subscribe(new PublisherBufferExactSubscriber<>(s, size, bufferSupplier));
46 } else if (skip > size) {
47 source.subscribe(new PublisherBufferSkipSubscriber<>(s, size, skip, bufferSupplier));
48 } else {
49 source.subscribe(new PublisherBufferOverlappingSubscriber<>(s, size, skip, bufferSupplier));
50 }
51 }
52
53 static final class PublisherBufferExactSubscriber<T, C extends Collection<? super T>>
54 implements FlowableSubscriber<T>, Subscription {
55
56 final Subscriber<? super C> downstream;
57
58 final Supplier<C> bufferSupplier;
59
60 final int size;
61
62 C buffer;
63
64 Subscription upstream;
65
66 boolean done;
67
68 int index;
69
70 PublisherBufferExactSubscriber(Subscriber<? super C> actual, int size, Supplier<C> bufferSupplier) {
71 this.downstream = actual;
72 this.size = size;
73 this.bufferSupplier = bufferSupplier;
74 }
75
76 @Override
77 public void request(long n) {
78 if (SubscriptionHelper.validate(n)) {
79 upstream.request(BackpressureHelper.multiplyCap(n, size));
80 }
81 }
82
83 @Override
84 public void cancel() {
85 upstream.cancel();
86 }
87
88 @Override
89 public void onSubscribe(Subscription s) {
90 if (SubscriptionHelper.validate(this.upstream, s)) {
91 this.upstream = s;
92
93 downstream.onSubscribe(this);
94 }
95 }
96
97 @Override
98 public void onNext(T t) {
99 if (done) {
100 return;
101 }
102
103 C b = buffer;
104 if (b == null) {
105
106 try {
107 b = Objects.requireNonNull(bufferSupplier.get(), "The bufferSupplier returned a null buffer");
108 } catch (Throwable e) {
109 Exceptions.throwIfFatal(e);
110 cancel();
111 onError(e);
112 return;
113 }
114
115 buffer = b;
116 }
117
118 b.add(t);
119
120 int i = index + 1;
121 if (i == size) {
122 index = 0;
123 buffer = null;
124 downstream.onNext(b);
125 } else {
126 index = i;
127 }
128 }
129
130 @Override
131 public void onError(Throwable t) {
132 if (done) {
133 RxJavaPlugins.onError(t);
134 return;
135 }
136 buffer = null;
137 done = true;
138 downstream.onError(t);
139 }
140
141 @Override
142 public void onComplete() {
143 if (done) {
144 return;
145 }
146 done = true;
147
148 C b = buffer;
149 buffer = null;
150
151 if (b != null) {
152 downstream.onNext(b);
153 }
154 downstream.onComplete();
155 }
156 }
157
158 static final class PublisherBufferSkipSubscriber<T, C extends Collection<? super T>>
159 extends AtomicInteger
160 implements FlowableSubscriber<T>, Subscription {
161
162 private static final long serialVersionUID = -5616169793639412593L;
163
164 final Subscriber<? super C> downstream;
165
166 final Supplier<C> bufferSupplier;
167
168 final int size;
169
170 final int skip;
171
172 C buffer;
173
174 Subscription upstream;
175
176 boolean done;
177
178 int index;
179
180 PublisherBufferSkipSubscriber(Subscriber<? super C> actual, int size, int skip,
181 Supplier<C> bufferSupplier) {
182 this.downstream = actual;
183 this.size = size;
184 this.skip = skip;
185 this.bufferSupplier = bufferSupplier;
186 }
187
188 @Override
189 public void request(long n) {
190 if (SubscriptionHelper.validate(n)) {
191 if (get() == 0 && compareAndSet(0, 1)) {
192 // n full buffers
193 long u = BackpressureHelper.multiplyCap(n, size);
194 // + (n - 1) gaps
195 long v = BackpressureHelper.multiplyCap(skip - size, n - 1);
196
197 upstream.request(BackpressureHelper.addCap(u, v));
198 } else {
199 // n full buffer + gap
200 upstream.request(BackpressureHelper.multiplyCap(skip, n));
201 }
202 }
203 }
204
205 @Override
206 public void cancel() {
207 upstream.cancel();
208 }
209
210 @Override
211 public void onSubscribe(Subscription s) {
212 if (SubscriptionHelper.validate(this.upstream, s)) {
213 this.upstream = s;
214
215 downstream.onSubscribe(this);
216 }
217 }
218
219 @Override
220 public void onNext(T t) {
221 if (done) {
222 return;
223 }
224
225 C b = buffer;
226
227 int i = index;
228
229 if (i++ == 0) {
230 try {
231 b = Objects.requireNonNull(bufferSupplier.get(), "The bufferSupplier returned a null buffer");
232 } catch (Throwable e) {
233 Exceptions.throwIfFatal(e);
234 cancel();
235
236 onError(e);
237 return;
238 }
239
240 buffer = b;
241 }
242
243 if (b != null) {
244 b.add(t);
245 if (b.size() == size) {
246 buffer = null;
247 downstream.onNext(b);
248 }
249 }
250
251 if (i == skip) {
252 i = 0;
253 }
254 index = i;
255 }
256
257 @Override
258 public void onError(Throwable t) {
259 if (done) {
260 RxJavaPlugins.onError(t);
261 return;
262 }
263
264 done = true;
265 buffer = null;
266
267 downstream.onError(t);
268 }
269
270 @Override
271 public void onComplete() {
272 if (done) {
273 return;
274 }
275
276 done = true;
277 C b = buffer;
278 buffer = null;
279
280 if (b != null) {
281 downstream.onNext(b);
282 }
283
284 downstream.onComplete();
285 }
286 }
287
288 static final class PublisherBufferOverlappingSubscriber<T, C extends Collection<? super T>>
289 extends AtomicLong
290 implements FlowableSubscriber<T>, Subscription, BooleanSupplier {
291
292 private static final long serialVersionUID = -7370244972039324525L;
293
294 final Subscriber<? super C> downstream;
295
296 final Supplier<C> bufferSupplier;
297
298 final int size;
299
300 final int skip;
301
302 final ArrayDeque<C> buffers;
303
304 final AtomicBoolean once;
305
306 Subscription upstream;
307
308 boolean done;
309
310 int index;
311
312 volatile boolean cancelled;
313
314 long produced;
315
316 PublisherBufferOverlappingSubscriber(Subscriber<? super C> actual, int size, int skip,
317 Supplier<C> bufferSupplier) {
318 this.downstream = actual;
319 this.size = size;
320 this.skip = skip;
321 this.bufferSupplier = bufferSupplier;
322 this.once = new AtomicBoolean();
323 this.buffers = new ArrayDeque<>();
324 }
325
326 @Override
327 public boolean getAsBoolean() {
328 return cancelled;
329 }
330
331 @Override
332 public void request(long n) {
333 if (SubscriptionHelper.validate(n)) {
334 if (QueueDrainHelper.postCompleteRequest(n, downstream, buffers, this, this)) {
335 return;
336 }
337
338 if (!once.get() && once.compareAndSet(false, true)) {
339 // (n - 1) skips
340 long u = BackpressureHelper.multiplyCap(skip, n - 1);
341
342 // + 1 full buffer
343 long r = BackpressureHelper.addCap(size, u);
344 upstream.request(r);
345 } else {
346 // n skips
347 long r = BackpressureHelper.multiplyCap(skip, n);
348 upstream.request(r);
349 }
350 }
351 }
352
353 @Override
354 public void cancel() {
355 cancelled = true;
356 upstream.cancel();
357 }
358
359 @Override
360 public void onSubscribe(Subscription s) {
361 if (SubscriptionHelper.validate(this.upstream, s)) {
362 this.upstream = s;
363
364 downstream.onSubscribe(this);
365 }
366 }
367
368 @Override
369 public void onNext(T t) {
370 if (done) {
371 return;
372 }
373
374 ArrayDeque<C> bs = buffers;
375
376 int i = index;
377
378 if (i++ == 0) {
379 C b;
380
381 try {
382 b = Objects.requireNonNull(bufferSupplier.get(), "The bufferSupplier returned a null buffer");
383 } catch (Throwable e) {
384 Exceptions.throwIfFatal(e);
385 cancel();
386 onError(e);
387 return;
388 }
389
390 bs.offer(b);
391 }
392
393 C b = bs.peek();
394
395 if (b.size() + 1 == size) {
396 bs.poll();
397
398 b.add(t);
399
400 produced++;
401
402 downstream.onNext(b);
403 }
404
405 for (C b0 : bs) {
406 b0.add(t);
407 }
408
409 if (i == skip) {
410 i = 0;
411 }
412 index = i;
413 }
414
415 @Override
416 public void onError(Throwable t) {
417 if (done) {
418 RxJavaPlugins.onError(t);
419 return;
420 }
421
422 done = true;
423 buffers.clear();
424
425 downstream.onError(t);
426 }
427
428 @Override
429 public void onComplete() {
430 if (done) {
431 return;
432 }
433
434 done = true;
435
436 long p = produced;
437 if (p != 0L) {
438 BackpressureHelper.produced(this, p);
439 }
440 QueueDrainHelper.postComplete(downstream, buffers, this, this);
441 }
442 }
443 }