Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableBufferTimed.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.TimeUnit;
18 import java.util.concurrent.atomic.AtomicReference;
19
20 import org.reactivestreams.*;
21
22 import io.reactivex.rxjava3.core.*;
23 import io.reactivex.rxjava3.core.Scheduler.Worker;
24 import io.reactivex.rxjava3.disposables.Disposable;
25 import io.reactivex.rxjava3.exceptions.Exceptions;
26 import io.reactivex.rxjava3.functions.Supplier;
27 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
28 import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue;
29 import io.reactivex.rxjava3.internal.subscribers.QueueDrainSubscriber;
30 import io.reactivex.rxjava3.internal.subscriptions.*;
31 import io.reactivex.rxjava3.internal.util.QueueDrainHelper;
32 import io.reactivex.rxjava3.subscribers.SerializedSubscriber;
33
34 public final class FlowableBufferTimed<T, U extends Collection<? super T>> extends AbstractFlowableWithUpstream<T, U> {
35
36 final long timespan;
37 final long timeskip;
38 final TimeUnit unit;
39 final Scheduler scheduler;
40 final Supplier<U> bufferSupplier;
41 final int maxSize;
42 final boolean restartTimerOnMaxSize;
43
44 public FlowableBufferTimed(Flowable<T> source, long timespan, long timeskip, TimeUnit unit, Scheduler scheduler, Supplier<U> bufferSupplier, int maxSize,
45 boolean restartTimerOnMaxSize) {
46 super(source);
47 this.timespan = timespan;
48 this.timeskip = timeskip;
49 this.unit = unit;
50 this.scheduler = scheduler;
51 this.bufferSupplier = bufferSupplier;
52 this.maxSize = maxSize;
53 this.restartTimerOnMaxSize = restartTimerOnMaxSize;
54 }
55
56 @Override
57 protected void subscribeActual(Subscriber<? super U> s) {
58 if (timespan == timeskip && maxSize == Integer.MAX_VALUE) {
59 source.subscribe(new BufferExactUnboundedSubscriber<>(
60 new SerializedSubscriber<>(s),
61 bufferSupplier, timespan, unit, scheduler));
62 return;
63 }
64 Scheduler.Worker w = scheduler.createWorker();
65
66 if (timespan == timeskip) {
67 source.subscribe(new BufferExactBoundedSubscriber<>(
68 new SerializedSubscriber<>(s),
69 bufferSupplier,
70 timespan, unit, maxSize, restartTimerOnMaxSize, w
71 ));
72 return;
73 }
74 // Can't use maxSize because what to do if a buffer is full but its
75 // timespan hasn't been elapsed?
76 source.subscribe(new BufferSkipBoundedSubscriber<>(
77 new SerializedSubscriber<>(s),
78 bufferSupplier, timespan, timeskip, unit, w));
79 }
80
81 static final class BufferExactUnboundedSubscriber<T, U extends Collection<? super T>>
82 extends QueueDrainSubscriber<T, U, U> implements Subscription, Runnable, Disposable {
83 final Supplier<U> bufferSupplier;
84 final long timespan;
85 final TimeUnit unit;
86 final Scheduler scheduler;
87
88 Subscription upstream;
89
90 U buffer;
91
92 final AtomicReference<Disposable> timer = new AtomicReference<>();
93
94 BufferExactUnboundedSubscriber(
95 Subscriber<? super U> actual, Supplier<U> bufferSupplier,
96 long timespan, TimeUnit unit, Scheduler scheduler) {
97 super(actual, new MpscLinkedQueue<>());
98 this.bufferSupplier = bufferSupplier;
99 this.timespan = timespan;
100 this.unit = unit;
101 this.scheduler = scheduler;
102 }
103
104 @Override
105 public void onSubscribe(Subscription s) {
106 if (SubscriptionHelper.validate(this.upstream, s)) {
107 this.upstream = s;
108
109 U b;
110
111 try {
112 b = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
113 } catch (Throwable e) {
114 Exceptions.throwIfFatal(e);
115 cancel();
116 EmptySubscription.error(e, downstream);
117 return;
118 }
119
120 buffer = b;
121
122 downstream.onSubscribe(this);
123
124 if (!cancelled) {
125 s.request(Long.MAX_VALUE);
126
127 Disposable d = scheduler.schedulePeriodicallyDirect(this, timespan, timespan, unit);
128 if (!timer.compareAndSet(null, d)) {
129 d.dispose();
130 }
131 }
132 }
133 }
134
135 @Override
136 public void onNext(T t) {
137 synchronized (this) {
138 U b = buffer;
139 if (b != null) {
140 b.add(t);
141 }
142 }
143 }
144
145 @Override
146 public void onError(Throwable t) {
147 DisposableHelper.dispose(timer);
148 synchronized (this) {
149 buffer = null;
150 }
151 downstream.onError(t);
152 }
153
154 @Override
155 public void onComplete() {
156 DisposableHelper.dispose(timer);
157 U b;
158 synchronized (this) {
159 b = buffer;
160 if (b == null) {
161 return;
162 }
163 buffer = null;
164 }
165 queue.offer(b);
166 done = true;
167 if (enter()) {
168 QueueDrainHelper.drainMaxLoop(queue, downstream, false, null, this);
169 }
170 }
171
172 @Override
173 public void request(long n) {
174 requested(n);
175 }
176
177 @Override
178 public void cancel() {
179 cancelled = true;
180 upstream.cancel();
181 DisposableHelper.dispose(timer);
182 }
183
184 @Override
185 public void run() {
186 U next;
187
188 try {
189 next = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
190 } catch (Throwable e) {
191 Exceptions.throwIfFatal(e);
192 cancel();
193 downstream.onError(e);
194 return;
195 }
196
197 U current;
198
199 synchronized (this) {
200 current = buffer;
201 if (current == null) {
202 return;
203 }
204 buffer = next;
205 }
206
207 fastPathEmitMax(current, false, this);
208 }
209
210 @Override
211 public boolean accept(Subscriber<? super U> a, U v) {
212 downstream.onNext(v);
213 return true;
214 }
215
216 @Override
217 public void dispose() {
218 cancel();
219 }
220
221 @Override
222 public boolean isDisposed() {
223 return timer.get() == DisposableHelper.DISPOSED;
224 }
225 }
226
227 static final class BufferSkipBoundedSubscriber<T, U extends Collection<? super T>>
228 extends QueueDrainSubscriber<T, U, U> implements Subscription, Runnable {
229 final Supplier<U> bufferSupplier;
230 final long timespan;
231 final long timeskip;
232 final TimeUnit unit;
233 final Worker w;
234 final List<U> buffers;
235
236 Subscription upstream;
237
238 BufferSkipBoundedSubscriber(Subscriber<? super U> actual,
239 Supplier<U> bufferSupplier, long timespan,
240 long timeskip, TimeUnit unit, Worker w) {
241 super(actual, new MpscLinkedQueue<>());
242 this.bufferSupplier = bufferSupplier;
243 this.timespan = timespan;
244 this.timeskip = timeskip;
245 this.unit = unit;
246 this.w = w;
247 this.buffers = new LinkedList<>();
248 }
249
250 @Override
251 public void onSubscribe(Subscription s) {
252 if (!SubscriptionHelper.validate(this.upstream, s)) {
253 return;
254 }
255 this.upstream = s;
256
257 final U b; // NOPMD
258
259 try {
260 b = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
261 } catch (Throwable e) {
262 Exceptions.throwIfFatal(e);
263 w.dispose();
264 s.cancel();
265 EmptySubscription.error(e, downstream);
266 return;
267 }
268
269 buffers.add(b);
270
271 downstream.onSubscribe(this);
272
273 s.request(Long.MAX_VALUE);
274
275 w.schedulePeriodically(this, timeskip, timeskip, unit);
276
277 w.schedule(new RemoveFromBuffer(b), timespan, unit);
278 }
279
280 @Override
281 public void onNext(T t) {
282 synchronized (this) {
283 for (U b : buffers) {
284 b.add(t);
285 }
286 }
287 }
288
289 @Override
290 public void onError(Throwable t) {
291 done = true;
292 w.dispose();
293 clear();
294 downstream.onError(t);
295 }
296
297 @Override
298 public void onComplete() {
299 List<U> bs;
300 synchronized (this) {
301 bs = new ArrayList<>(buffers);
302 buffers.clear();
303 }
304
305 for (U b : bs) {
306 queue.offer(b);
307 }
308 done = true;
309 if (enter()) {
310 QueueDrainHelper.drainMaxLoop(queue, downstream, false, w, this);
311 }
312 }
313
314 @Override
315 public void request(long n) {
316 requested(n);
317 }
318
319 @Override
320 public void cancel() {
321 cancelled = true;
322 upstream.cancel();
323 w.dispose();
324 clear();
325 }
326
327 void clear() {
328 synchronized (this) {
329 buffers.clear();
330 }
331 }
332
333 @Override
334 public void run() {
335 if (cancelled) {
336 return;
337 }
338 final U b; // NOPMD
339
340 try {
341 b = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
342 } catch (Throwable e) {
343 Exceptions.throwIfFatal(e);
344 cancel();
345 downstream.onError(e);
346 return;
347 }
348
349 synchronized (this) {
350 if (cancelled) {
351 return;
352 }
353 buffers.add(b);
354 }
355
356 w.schedule(new RemoveFromBuffer(b), timespan, unit);
357 }
358
359 @Override
360 public boolean accept(Subscriber<? super U> a, U v) {
361 a.onNext(v);
362 return true;
363 }
364
365 final class RemoveFromBuffer implements Runnable {
366 private final U buffer;
367
368 RemoveFromBuffer(U buffer) {
369 this.buffer = buffer;
370 }
371
372 @Override
373 public void run() {
374 synchronized (BufferSkipBoundedSubscriber.this) {
375 buffers.remove(buffer);
376 }
377
378 fastPathOrderedEmitMax(buffer, false, w);
379 }
380 }
381 }
382
383 static final class BufferExactBoundedSubscriber<T, U extends Collection<? super T>>
384 extends QueueDrainSubscriber<T, U, U> implements Subscription, Runnable, Disposable {
385 final Supplier<U> bufferSupplier;
386 final long timespan;
387 final TimeUnit unit;
388 final int maxSize;
389 final boolean restartTimerOnMaxSize;
390 final Worker w;
391
392 U buffer;
393
394 Disposable timer;
395
396 Subscription upstream;
397
398 long producerIndex;
399
400 long consumerIndex;
401
402 BufferExactBoundedSubscriber(
403 Subscriber<? super U> actual,
404 Supplier<U> bufferSupplier,
405 long timespan, TimeUnit unit, int maxSize,
406 boolean restartOnMaxSize, Worker w) {
407 super(actual, new MpscLinkedQueue<>());
408 this.bufferSupplier = bufferSupplier;
409 this.timespan = timespan;
410 this.unit = unit;
411 this.maxSize = maxSize;
412 this.restartTimerOnMaxSize = restartOnMaxSize;
413 this.w = w;
414 }
415
416 @Override
417 public void onSubscribe(Subscription s) {
418 if (!SubscriptionHelper.validate(this.upstream, s)) {
419 return;
420 }
421 this.upstream = s;
422
423 U b;
424
425 try {
426 b = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
427 } catch (Throwable e) {
428 Exceptions.throwIfFatal(e);
429 w.dispose();
430 s.cancel();
431 EmptySubscription.error(e, downstream);
432 return;
433 }
434
435 buffer = b;
436
437 downstream.onSubscribe(this);
438
439 timer = w.schedulePeriodically(this, timespan, timespan, unit);
440
441 s.request(Long.MAX_VALUE);
442 }
443
444 @Override
445 public void onNext(T t) {
446 U b;
447 synchronized (this) {
448 b = buffer;
449 if (b == null) {
450 return;
451 }
452
453 b.add(t);
454
455 if (b.size() < maxSize) {
456 return;
457 }
458
459 buffer = null;
460 producerIndex++;
461 }
462
463 if (restartTimerOnMaxSize) {
464 timer.dispose();
465 }
466
467 fastPathOrderedEmitMax(b, false, this);
468
469 try {
470 b = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
471 } catch (Throwable e) {
472 Exceptions.throwIfFatal(e);
473 cancel();
474 downstream.onError(e);
475 return;
476 }
477
478 synchronized (this) {
479 buffer = b;
480 consumerIndex++;
481 }
482 if (restartTimerOnMaxSize) {
483 timer = w.schedulePeriodically(this, timespan, timespan, unit);
484 }
485 }
486
487 @Override
488 public void onError(Throwable t) {
489 synchronized (this) {
490 buffer = null;
491 }
492 downstream.onError(t);
493 w.dispose();
494 }
495
496 @Override
497 public void onComplete() {
498 U b;
499 synchronized (this) {
500 b = buffer;
501 buffer = null;
502 }
503
504 if (b != null) {
505 queue.offer(b);
506 done = true;
507 if (enter()) {
508 QueueDrainHelper.drainMaxLoop(queue, downstream, false, this, this);
509 }
510 w.dispose();
511 }
512 }
513
514 @Override
515 public boolean accept(Subscriber<? super U> a, U v) {
516 a.onNext(v);
517 return true;
518 }
519
520 @Override
521 public void request(long n) {
522 requested(n);
523 }
524
525 @Override
526 public void cancel() {
527 if (!cancelled) {
528 cancelled = true;
529 dispose();
530 }
531 }
532
533 @Override
534 public void dispose() {
535 synchronized (this) {
536 buffer = null;
537 }
538 upstream.cancel();
539 w.dispose();
540 }
541
542 @Override
543 public boolean isDisposed() {
544 return w.isDisposed();
545 }
546
547 @Override
548 public void run() {
549 U next;
550
551 try {
552 next = Objects.requireNonNull(bufferSupplier.get(), "The supplied buffer is null");
553 } catch (Throwable e) {
554 Exceptions.throwIfFatal(e);
555 cancel();
556 downstream.onError(e);
557 return;
558 }
559
560 U current;
561
562 synchronized (this) {
563 current = buffer;
564 if (current == null || producerIndex != consumerIndex) {
565 return;
566 }
567 buffer = next;
568 }
569
570 fastPathOrderedEmitMax(current, false, this);
571 }
572 }
573 }