Debian Codegraph @

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