Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableBuffer.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.atomic.AtomicBoolean;
18
19 import io.reactivex.rxjava3.core.ObservableSource;
20 import io.reactivex.rxjava3.core.Observer;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.Supplier;
24 import io.reactivex.rxjava3.internal.disposables.*;
25 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
26
27 public final class ObservableBuffer<T, U extends Collection<? super T>> extends AbstractObservableWithUpstream<T, U> {
28 final int count;
29 final int skip;
30 final Supplier<U> bufferSupplier;
31
32 public ObservableBuffer(ObservableSource<T> source, int count, int skip, Supplier<U> bufferSupplier) {
33 super(source);
34 this.count = count;
35 this.skip = skip;
36 this.bufferSupplier = bufferSupplier;
37 }
38
39 @Override
40 protected void subscribeActual(Observer<? super U> t) {
41 if (skip == count) {
42 BufferExactObserver<T, U> bes = new BufferExactObserver<>(t, count, bufferSupplier);
43 if (bes.createBuffer()) {
44 source.subscribe(bes);
45 }
46 } else {
47 source.subscribe(new BufferSkipObserver<>(t, count, skip, bufferSupplier));
48 }
49 }
50
51 static final class BufferExactObserver<T, U extends Collection<? super T>> implements Observer<T>, Disposable {
52 final Observer<? super U> downstream;
53 final int count;
54 final Supplier<U> bufferSupplier;
55 U buffer;
56
57 int size;
58
59 Disposable upstream;
60
61 BufferExactObserver(Observer<? super U> actual, int count, Supplier<U> bufferSupplier) {
62 this.downstream = actual;
63 this.count = count;
64 this.bufferSupplier = bufferSupplier;
65 }
66
67 boolean createBuffer() {
68 U b;
69 try {
70 b = Objects.requireNonNull(bufferSupplier.get(), "Empty buffer supplied");
71 } catch (Throwable t) {
72 Exceptions.throwIfFatal(t);
73 buffer = null;
74 if (upstream == null) {
75 EmptyDisposable.error(t, downstream);
76 } else {
77 upstream.dispose();
78 downstream.onError(t);
79 }
80 return false;
81 }
82
83 buffer = b;
84
85 return true;
86 }
87
88 @Override
89 public void onSubscribe(Disposable d) {
90 if (DisposableHelper.validate(this.upstream, d)) {
91 this.upstream = d;
92 downstream.onSubscribe(this);
93 }
94 }
95
96 @Override
97 public void dispose() {
98 upstream.dispose();
99 }
100
101 @Override
102 public boolean isDisposed() {
103 return upstream.isDisposed();
104 }
105
106 @Override
107 public void onNext(T t) {
108 U b = buffer;
109 if (b != null) {
110 b.add(t);
111
112 if (++size >= count) {
113 downstream.onNext(b);
114
115 size = 0;
116 createBuffer();
117 }
118 }
119 }
120
121 @Override
122 public void onError(Throwable t) {
123 buffer = null;
124 downstream.onError(t);
125 }
126
127 @Override
128 public void onComplete() {
129 U b = buffer;
130 if (b != null) {
131 buffer = null;
132 if (!b.isEmpty()) {
133 downstream.onNext(b);
134 }
135 downstream.onComplete();
136 }
137 }
138 }
139
140 static final class BufferSkipObserver<T, U extends Collection<? super T>>
141 extends AtomicBoolean implements Observer<T>, Disposable {
142
143 private static final long serialVersionUID = -8223395059921494546L;
144 final Observer<? super U> downstream;
145 final int count;
146 final int skip;
147 final Supplier<U> bufferSupplier;
148
149 Disposable upstream;
150
151 final ArrayDeque<U> buffers;
152
153 long index;
154
155 BufferSkipObserver(Observer<? super U> actual, int count, int skip, Supplier<U> bufferSupplier) {
156 this.downstream = actual;
157 this.count = count;
158 this.skip = skip;
159 this.bufferSupplier = bufferSupplier;
160 this.buffers = new ArrayDeque<>();
161 }
162
163 @Override
164 public void onSubscribe(Disposable d) {
165 if (DisposableHelper.validate(this.upstream, d)) {
166 this.upstream = d;
167 downstream.onSubscribe(this);
168 }
169 }
170
171 @Override
172 public void dispose() {
173 upstream.dispose();
174 }
175
176 @Override
177 public boolean isDisposed() {
178 return upstream.isDisposed();
179 }
180
181 @Override
182 public void onNext(T t) {
183 if (index++ % skip == 0) {
184 U b;
185
186 try {
187 b = ExceptionHelper.nullCheck(bufferSupplier.get(), "The bufferSupplier returned a null Collection.");
188 } catch (Throwable e) {
189 Exceptions.throwIfFatal(e);
190 buffers.clear();
191 upstream.dispose();
192 downstream.onError(e);
193 return;
194 }
195
196 buffers.offer(b);
197 }
198
199 Iterator<U> it = buffers.iterator();
200 while (it.hasNext()) {
201 U b = it.next();
202 b.add(t);
203 if (count <= b.size()) {
204 it.remove();
205
206 downstream.onNext(b);
207 }
208 }
209 }
210
211 @Override
212 public void onError(Throwable t) {
213 buffers.clear();
214 downstream.onError(t);
215 }
216
217 @Override
218 public void onComplete() {
219 while (!buffers.isEmpty()) {
220 downstream.onNext(buffers.poll());
221 }
222 downstream.onComplete();
223 }
224 }
225 }