| 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.*; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.Observable; |
| 20 |
import io.reactivex.rxjava3.core.ObservableSource; |
| 21 |
import io.reactivex.rxjava3.core.Observer; |
| 22 |
import io.reactivex.rxjava3.disposables.*; |
| 23 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 24 |
import io.reactivex.rxjava3.functions.Function; |
| 25 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 26 |
import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue; |
| 27 |
import io.reactivex.rxjava3.internal.util.*; |
| 28 |
import io.reactivex.rxjava3.operators.SimplePlainQueue; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
import io.reactivex.rxjava3.subjects.UnicastSubject; |
| 31 |
|
| 32 |
public final class ObservableWindowBoundarySelector<T, B, V> extends AbstractObservableWithUpstream<T, Observable<T>> { |
| 33 |
final ObservableSource<B> open; |
| 34 |
final Function<? super B, ? extends ObservableSource<V>> closingIndicator; |
| 35 |
final int bufferSize; |
| 36 |
|
| 37 |
public ObservableWindowBoundarySelector( |
| 38 |
ObservableSource<T> source, |
| 39 |
ObservableSource<B> open, Function<? super B, ? extends ObservableSource<V>> closingIndicator, |
| 40 |
int bufferSize) { |
| 41 |
super(source); |
| 42 |
this.open = open; |
| 43 |
this.closingIndicator = closingIndicator; |
| 44 |
this.bufferSize = bufferSize; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void subscribeActual(Observer<? super Observable<T>> t) { |
| 49 |
source.subscribe(new WindowBoundaryMainObserver<>( |
| 50 |
t, open, closingIndicator, bufferSize)); |
| 51 |
} |
| 52 |
|
| 53 |
static final class WindowBoundaryMainObserver<T, B, V> |
| 54 |
extends AtomicInteger |
| 55 |
implements Observer<T>, Disposable, Runnable { |
| 56 |
private static final long serialVersionUID = 8646217640096099753L; |
| 57 |
|
| 58 |
final Observer<? super Observable<T>> downstream; |
| 59 |
final ObservableSource<B> open; |
| 60 |
final Function<? super B, ? extends ObservableSource<V>> closingIndicator; |
| 61 |
final int bufferSize; |
| 62 |
final CompositeDisposable resources; |
| 63 |
|
| 64 |
final WindowStartObserver<B> startObserver; |
| 65 |
|
| 66 |
final List<UnicastSubject<T>> windows; |
| 67 |
|
| 68 |
final SimplePlainQueue<Object> queue; |
| 69 |
|
| 70 |
final AtomicLong windowCount; |
| 71 |
|
| 72 |
final AtomicBoolean downstreamDisposed; |
| 73 |
|
| 74 |
final AtomicLong requested; |
| 75 |
long emitted; |
| 76 |
|
| 77 |
volatile boolean upstreamCanceled; |
| 78 |
|
| 79 |
volatile boolean upstreamDone; |
| 80 |
volatile boolean openDone; |
| 81 |
final AtomicThrowable error; |
| 82 |
|
| 83 |
Disposable upstream; |
| 84 |
|
| 85 |
WindowBoundaryMainObserver(Observer<? super Observable<T>> downstream, |
| 86 |
ObservableSource<B> open, Function<? super B, ? extends ObservableSource<V>> closingIndicator, int bufferSize) { |
| 87 |
this.downstream = downstream; |
| 88 |
this.queue = new MpscLinkedQueue<>(); |
| 89 |
this.open = open; |
| 90 |
this.closingIndicator = closingIndicator; |
| 91 |
this.bufferSize = bufferSize; |
| 92 |
this.resources = new CompositeDisposable(); |
| 93 |
this.windows = new ArrayList<>(); |
| 94 |
this.windowCount = new AtomicLong(1L); |
| 95 |
this.downstreamDisposed = new AtomicBoolean(); |
| 96 |
this.error = new AtomicThrowable(); |
| 97 |
this.startObserver = new WindowStartObserver<>(this); |
| 98 |
this.requested = new AtomicLong(); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void onSubscribe(Disposable d) { |
| 103 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 104 |
this.upstream = d; |
| 105 |
|
| 106 |
downstream.onSubscribe(this); |
| 107 |
|
| 108 |
open.subscribe(startObserver); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onNext(T t) { |
| 114 |
queue.offer(t); |
| 115 |
drain(); |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onError(Throwable t) { |
| 120 |
startObserver.dispose(); |
| 121 |
resources.dispose(); |
| 122 |
if (error.tryAddThrowableOrReport(t)) { |
| 123 |
upstreamDone = true; |
| 124 |
drain(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void onComplete() { |
| 130 |
startObserver.dispose(); |
| 131 |
resources.dispose(); |
| 132 |
upstreamDone = true; |
| 133 |
drain(); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void dispose() { |
| 138 |
if (downstreamDisposed.compareAndSet(false, true)) { |
| 139 |
if (windowCount.decrementAndGet() == 0) { |
| 140 |
upstream.dispose(); |
| 141 |
startObserver.dispose(); |
| 142 |
resources.dispose(); |
| 143 |
error.tryTerminateAndReport(); |
| 144 |
upstreamCanceled = true; |
| 145 |
drain(); |
| 146 |
} else { |
| 147 |
startObserver.dispose(); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public boolean isDisposed() { |
| 154 |
return downstreamDisposed.get(); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void run() { |
| 159 |
if (windowCount.decrementAndGet() == 0) { |
| 160 |
upstream.dispose(); |
| 161 |
startObserver.dispose(); |
| 162 |
resources.dispose(); |
| 163 |
error.tryTerminateAndReport(); |
| 164 |
upstreamCanceled = true; |
| 165 |
drain(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void open(B startValue) { |
| 170 |
queue.offer(new WindowStartItem<>(startValue)); |
| 171 |
drain(); |
| 172 |
} |
| 173 |
|
| 174 |
void openError(Throwable t) { |
| 175 |
upstream.dispose(); |
| 176 |
resources.dispose(); |
| 177 |
if (error.tryAddThrowableOrReport(t)) { |
| 178 |
upstreamDone = true; |
| 179 |
drain(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
void openComplete() { |
| 184 |
openDone = true; |
| 185 |
drain(); |
| 186 |
} |
| 187 |
|
| 188 |
void close(WindowEndObserverIntercept<T, V> what) { |
| 189 |
queue.offer(what); |
| 190 |
drain(); |
| 191 |
} |
| 192 |
|
| 193 |
void closeError(Throwable t) { |
| 194 |
upstream.dispose(); |
| 195 |
startObserver.dispose(); |
| 196 |
resources.dispose(); |
| 197 |
if (error.tryAddThrowableOrReport(t)) { |
| 198 |
upstreamDone = true; |
| 199 |
drain(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void drain() { |
| 204 |
if (getAndIncrement() != 0) { |
| 205 |
return; |
| 206 |
} |
| 207 |
|
| 208 |
int missed = 1; |
| 209 |
final Observer<? super Observable<T>> downstream = this.downstream; |
| 210 |
final SimplePlainQueue<Object> queue = this.queue; |
| 211 |
final List<UnicastSubject<T>> windows = this.windows; |
| 212 |
|
| 213 |
for (;;) { |
| 214 |
if (upstreamCanceled) { |
| 215 |
queue.clear(); |
| 216 |
windows.clear(); |
| 217 |
} else { |
| 218 |
boolean isDone = upstreamDone; |
| 219 |
Object o = queue.poll(); |
| 220 |
boolean isEmpty = o == null; |
| 221 |
|
| 222 |
if (isDone) { |
| 223 |
if (isEmpty || error.get() != null) { |
| 224 |
terminateDownstream(downstream); |
| 225 |
upstreamCanceled = true; |
| 226 |
continue; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if (!isEmpty) { |
| 231 |
if (o instanceof WindowStartItem) { |
| 232 |
if (!downstreamDisposed.get()) { |
| 233 |
@SuppressWarnings("unchecked") |
| 234 |
B startItem = ((WindowStartItem<B>)o).item; |
| 235 |
|
| 236 |
ObservableSource<V> endSource; |
| 237 |
try { |
| 238 |
endSource = Objects.requireNonNull(closingIndicator.apply(startItem), "The closingIndicator returned a null ObservableSource"); |
| 239 |
} catch (Throwable ex) { |
| 240 |
Exceptions.throwIfFatal(ex); |
| 241 |
upstream.dispose(); |
| 242 |
startObserver.dispose(); |
| 243 |
resources.dispose(); |
| 244 |
Exceptions.throwIfFatal(ex); |
| 245 |
error.tryAddThrowableOrReport(ex); |
| 246 |
upstreamDone = true; |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
windowCount.getAndIncrement(); |
| 251 |
UnicastSubject<T> newWindow = UnicastSubject.create(bufferSize, this); |
| 252 |
WindowEndObserverIntercept<T, V> endObserver = new WindowEndObserverIntercept<>(this, newWindow); |
| 253 |
|
| 254 |
downstream.onNext(endObserver); |
| 255 |
|
| 256 |
if (endObserver.tryAbandon()) { |
| 257 |
newWindow.onComplete(); |
| 258 |
} else { |
| 259 |
windows.add(newWindow); |
| 260 |
resources.add(endObserver); |
| 261 |
endSource.subscribe(endObserver); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
else if (o instanceof WindowEndObserverIntercept) { |
| 266 |
@SuppressWarnings("unchecked") |
| 267 |
UnicastSubject<T> w = ((WindowEndObserverIntercept<T, V>)o).window; |
| 268 |
|
| 269 |
windows.remove(w); |
| 270 |
resources.delete((Disposable)o); |
| 271 |
w.onComplete(); |
| 272 |
} else { |
| 273 |
@SuppressWarnings("unchecked") |
| 274 |
T item = (T)o; |
| 275 |
|
| 276 |
for (UnicastSubject<T> w : windows) { |
| 277 |
w.onNext(item); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
continue; |
| 282 |
} |
| 283 |
else if (openDone && windows.size() == 0) { |
| 284 |
upstream.dispose(); |
| 285 |
startObserver.dispose(); |
| 286 |
resources.dispose(); |
| 287 |
terminateDownstream(downstream); |
| 288 |
upstreamCanceled = true; |
| 289 |
continue; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
missed = addAndGet(-missed); |
| 294 |
if (missed == 0) { |
| 295 |
break; |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
void terminateDownstream(Observer<?> downstream) { |
| 301 |
Throwable ex = error.terminate(); |
| 302 |
if (ex == null) { |
| 303 |
for (UnicastSubject<T> w : windows) { |
| 304 |
w.onComplete(); |
| 305 |
} |
| 306 |
downstream.onComplete(); |
| 307 |
} else if (ex != ExceptionHelper.TERMINATED) { |
| 308 |
for (UnicastSubject<T> w : windows) { |
| 309 |
w.onError(ex); |
| 310 |
} |
| 311 |
downstream.onError(ex); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
static final class WindowStartItem<B> { |
| 316 |
|
| 317 |
final B item; |
| 318 |
|
| 319 |
WindowStartItem(B item) { |
| 320 |
this.item = item; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
static final class WindowStartObserver<B> extends AtomicReference<Disposable> |
| 325 |
implements Observer<B> { |
| 326 |
|
| 327 |
private static final long serialVersionUID = -3326496781427702834L; |
| 328 |
|
| 329 |
final WindowBoundaryMainObserver<?, B, ?> parent; |
| 330 |
|
| 331 |
WindowStartObserver(WindowBoundaryMainObserver<?, B, ?> parent) { |
| 332 |
this.parent = parent; |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public void onSubscribe(Disposable d) { |
| 337 |
DisposableHelper.setOnce(this, d); |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public void onNext(B t) { |
| 342 |
parent.open(t); |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public void onError(Throwable t) { |
| 347 |
parent.openError(t); |
| 348 |
} |
| 349 |
|
| 350 |
@Override |
| 351 |
public void onComplete() { |
| 352 |
parent.openComplete(); |
| 353 |
} |
| 354 |
|
| 355 |
void dispose() { |
| 356 |
DisposableHelper.dispose(this); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
static final class WindowEndObserverIntercept<T, V> extends Observable<T> |
| 361 |
implements Observer<V>, Disposable { |
| 362 |
|
| 363 |
final WindowBoundaryMainObserver<T, ?, V> parent; |
| 364 |
|
| 365 |
final UnicastSubject<T> window; |
| 366 |
|
| 367 |
final AtomicReference<Disposable> upstream; |
| 368 |
|
| 369 |
final AtomicBoolean once; |
| 370 |
|
| 371 |
WindowEndObserverIntercept(WindowBoundaryMainObserver<T, ?, V> parent, UnicastSubject<T> window) { |
| 372 |
this.parent = parent; |
| 373 |
this.window = window; |
| 374 |
this.upstream = new AtomicReference<>(); |
| 375 |
this.once = new AtomicBoolean(); |
| 376 |
} |
| 377 |
|
| 378 |
@Override |
| 379 |
public void onSubscribe(Disposable d) { |
| 380 |
DisposableHelper.setOnce(upstream, d); |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
public void onNext(V t) { |
| 385 |
if (DisposableHelper.dispose(upstream)) { |
| 386 |
parent.close(this); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
@Override |
| 391 |
public void onError(Throwable t) { |
| 392 |
if (isDisposed()) { |
| 393 |
RxJavaPlugins.onError(t); |
| 394 |
} else { |
| 395 |
parent.closeError(t); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
@Override |
| 400 |
public void onComplete() { |
| 401 |
parent.close(this); |
| 402 |
} |
| 403 |
|
| 404 |
@Override |
| 405 |
public void dispose() { |
| 406 |
DisposableHelper.dispose(upstream); |
| 407 |
} |
| 408 |
|
| 409 |
@Override |
| 410 |
public boolean isDisposed() { |
| 411 |
return upstream.get() == DisposableHelper.DISPOSED; |
| 412 |
} |
| 413 |
|
| 414 |
@Override |
| 415 |
protected void subscribeActual(Observer<? super T> o) { |
| 416 |
window.subscribe(o); |
| 417 |
once.set(true); |
| 418 |
} |
| 419 |
|
| 420 |
boolean tryAbandon() { |
| 421 |
return !once.get() && once.compareAndSet(false, true); |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
} |