Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableBufferExactBoundary.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.Collection;
17 import java.util.Objects;
18
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.exceptions.Exceptions;
22 import io.reactivex.rxjava3.functions.Supplier;
23 import io.reactivex.rxjava3.internal.disposables.*;
24 import io.reactivex.rxjava3.internal.observers.QueueDrainObserver;
25 import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue;
26 import io.reactivex.rxjava3.internal.util.QueueDrainHelper;
27 import io.reactivex.rxjava3.observers.*;
28
29 public final class ObservableBufferExactBoundary<T, U extends Collection<? super T>, B>
30 extends AbstractObservableWithUpstream<T, U> {
31 final ObservableSource<B> boundary;
32 final Supplier<U> bufferSupplier;
33
34 public ObservableBufferExactBoundary(ObservableSource<T> source, ObservableSource<B> boundary, Supplier<U> bufferSupplier) {
35 super(source);
36 this.boundary = boundary;
37 this.bufferSupplier = bufferSupplier;
38 }
39
40 @Override
41 protected void subscribeActual(Observer<? super U> t) {
42 source.subscribe(new BufferExactBoundaryObserver<>(new SerializedObserver<>(t), bufferSupplier, boundary));
43 }
44
45 static final class BufferExactBoundaryObserver<T, U extends Collection<? super T>, B>
46 extends QueueDrainObserver<T, U, U> implements Observer<T>, Disposable {
47
48 final Supplier<U> bufferSupplier;
49 final ObservableSource<B> boundary;
50
51 Disposable upstream;
52
53 Disposable other;
54
55 U buffer;
56
57 BufferExactBoundaryObserver(Observer<? super U> actual, Supplier<U> bufferSupplier,
58 ObservableSource<B> boundary) {
59 super(actual, new MpscLinkedQueue<>());
60 this.bufferSupplier = bufferSupplier;
61 this.boundary = boundary;
62 }
63
64 @Override
65 public void onSubscribe(Disposable d) {
66 if (DisposableHelper.validate(this.upstream, d)) {
67 this.upstream = d;
68
69 U b;
70
71 try {
72 b = Objects.requireNonNull(bufferSupplier.get(), "The buffer supplied is null");
73 } catch (Throwable e) {
74 Exceptions.throwIfFatal(e);
75 cancelled = true;
76 d.dispose();
77 EmptyDisposable.error(e, downstream);
78 return;
79 }
80
81 buffer = b;
82
83 BufferBoundaryObserver<T, U, B> bs = new BufferBoundaryObserver<>(this);
84 other = bs;
85
86 downstream.onSubscribe(this);
87
88 if (!cancelled) {
89 boundary.subscribe(bs);
90 }
91 }
92 }
93
94 @Override
95 public void onNext(T t) {
96 synchronized (this) {
97 U b = buffer;
98 if (b == null) {
99 return;
100 }
101 b.add(t);
102 }
103 }
104
105 @Override
106 public void onError(Throwable t) {
107 dispose();
108 downstream.onError(t);
109 }
110
111 @Override
112 public void onComplete() {
113 U b;
114 synchronized (this) {
115 b = buffer;
116 if (b == null) {
117 return;
118 }
119 buffer = null;
120 }
121 queue.offer(b);
122 done = true;
123 if (enter()) {
124 QueueDrainHelper.drainLoop(queue, downstream, false, this, this);
125 }
126 }
127
128 @Override
129 public void dispose() {
130 if (!cancelled) {
131 cancelled = true;
132 other.dispose();
133 upstream.dispose();
134
135 if (enter()) {
136 queue.clear();
137 }
138 }
139 }
140
141 @Override
142 public boolean isDisposed() {
143 return cancelled;
144 }
145
146 void next() {
147
148 U next;
149
150 try {
151 next = Objects.requireNonNull(bufferSupplier.get(), "The buffer supplied is null");
152 } catch (Throwable e) {
153 Exceptions.throwIfFatal(e);
154 dispose();
155 downstream.onError(e);
156 return;
157 }
158
159 U b;
160 synchronized (this) {
161 b = buffer;
162 if (b == null) {
163 return;
164 }
165 buffer = next;
166 }
167
168 fastPathEmit(b, false, this);
169 }
170
171 @Override
172 public void accept(Observer<? super U> a, U v) {
173 downstream.onNext(v);
174 }
175
176 }
177
178 static final class BufferBoundaryObserver<T, U extends Collection<? super T>, B>
179 extends DisposableObserver<B> {
180 final BufferExactBoundaryObserver<T, U, B> parent;
181
182 BufferBoundaryObserver(BufferExactBoundaryObserver<T, U, B> parent) {
183 this.parent = parent;
184 }
185
186 @Override
187 public void onNext(B t) {
188 parent.next();
189 }
190
191 @Override
192 public void onError(Throwable t) {
193 parent.onError(t);
194 }
195
196 @Override
197 public void onComplete() {
198 parent.onComplete();
199 }
200 }
201 }