io.reactivex.observable.internal.operators.ObservableBufferBoundarySupplier Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava3-observable Show documentation
Show all versions of rxjava3-observable Show documentation
rxjava3-observable developed by David Karnok
The newest version!
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.observable.internal.operators;
import java.util.Collection;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;
import io.reactivex.common.*;
import io.reactivex.common.exceptions.Exceptions;
import io.reactivex.common.internal.disposables.DisposableHelper;
import io.reactivex.common.internal.functions.ObjectHelper;
import io.reactivex.observable.*;
import io.reactivex.observable.internal.disposables.EmptyDisposable;
import io.reactivex.observable.internal.observers.QueueDrainObserver;
import io.reactivex.observable.internal.queues.MpscLinkedQueue;
import io.reactivex.observable.internal.utils.QueueDrainHelper;
import io.reactivex.observable.observers.*;
public final class ObservableBufferBoundarySupplier, B>
extends AbstractObservableWithUpstream {
final Callable extends ObservableSource> boundarySupplier;
final Callable bufferSupplier;
public ObservableBufferBoundarySupplier(ObservableSource source, Callable extends ObservableSource> boundarySupplier, Callable bufferSupplier) {
super(source);
this.boundarySupplier = boundarySupplier;
this.bufferSupplier = bufferSupplier;
}
@Override
protected void subscribeActual(Observer super U> t) {
source.subscribe(new BufferBoundarySupplierObserver(new SerializedObserver(t), bufferSupplier, boundarySupplier));
}
static final class BufferBoundarySupplierObserver, B>
extends QueueDrainObserver implements Observer, Disposable {
final Callable bufferSupplier;
final Callable extends ObservableSource> boundarySupplier;
Disposable s;
final AtomicReference other = new AtomicReference();
U buffer;
BufferBoundarySupplierObserver(Observer super U> actual, Callable bufferSupplier,
Callable extends ObservableSource> boundarySupplier) {
super(actual, new MpscLinkedQueue());
this.bufferSupplier = bufferSupplier;
this.boundarySupplier = boundarySupplier;
}
@Override
public void onSubscribe(Disposable s) {
if (DisposableHelper.validate(this.s, s)) {
this.s = s;
Observer super U> actual = this.actual;
U b;
try {
b = ObjectHelper.requireNonNull(bufferSupplier.call(), "The buffer supplied is null");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
cancelled = true;
s.dispose();
EmptyDisposable.error(e, actual);
return;
}
buffer = b;
ObservableSource boundary;
try {
boundary = ObjectHelper.requireNonNull(boundarySupplier.call(), "The boundary Observable supplied is null");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
cancelled = true;
s.dispose();
EmptyDisposable.error(ex, actual);
return;
}
BufferBoundaryObserver bs = new BufferBoundaryObserver(this);
other.set(bs);
actual.onSubscribe(this);
if (!cancelled) {
boundary.subscribe(bs);
}
}
}
@Override
public void onNext(T t) {
synchronized (this) {
U b = buffer;
if (b == null) {
return;
}
b.add(t);
}
}
@Override
public void onError(Throwable t) {
dispose();
actual.onError(t);
}
@Override
public void onComplete() {
U b;
synchronized (this) {
b = buffer;
if (b == null) {
return;
}
buffer = null;
}
queue.offer(b);
done = true;
if (enter()) {
QueueDrainHelper.drainLoop(queue, actual, false, this, this);
}
}
@Override
public void dispose() {
if (!cancelled) {
cancelled = true;
s.dispose();
disposeOther();
if (enter()) {
queue.clear();
}
}
}
@Override
public boolean isDisposed() {
return cancelled;
}
void disposeOther() {
DisposableHelper.dispose(other);
}
void next() {
U next;
try {
next = ObjectHelper.requireNonNull(bufferSupplier.call(), "The buffer supplied is null");
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
dispose();
actual.onError(e);
return;
}
ObservableSource boundary;
try {
boundary = ObjectHelper.requireNonNull(boundarySupplier.call(), "The boundary Observable supplied is null");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
cancelled = true;
s.dispose();
actual.onError(ex);
return;
}
BufferBoundaryObserver bs = new BufferBoundaryObserver(this);
Disposable o = other.get();
if (!other.compareAndSet(o, bs)) {
return;
}
U b;
synchronized (this) {
b = buffer;
if (b == null) {
return;
}
buffer = next;
}
boundary.subscribe(bs);
fastPathEmit(b, false, this);
}
@Override
public void accept(Observer super U> a, U v) {
actual.onNext(v);
}
}
static final class BufferBoundaryObserver, B>
extends DisposableObserver {
final BufferBoundarySupplierObserver parent;
boolean once;
BufferBoundaryObserver(BufferBoundarySupplierObserver parent) {
this.parent = parent;
}
@Override
public void onNext(B t) {
if (once) {
return;
}
once = true;
dispose();
parent.next();
}
@Override
public void onError(Throwable t) {
if (once) {
RxJavaCommonPlugins.onError(t);
return;
}
once = true;
parent.onError(t);
}
@Override
public void onComplete() {
if (once) {
return;
}
once = true;
parent.next();
}
}
}