io.reactivex.observable.internal.operators.ObservableWindow 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.ArrayDeque;
import java.util.concurrent.atomic.*;
import io.reactivex.common.Disposable;
import io.reactivex.common.internal.disposables.DisposableHelper;
import io.reactivex.observable.*;
import io.reactivex.observable.subjects.UnicastSubject;
public final class ObservableWindow extends AbstractObservableWithUpstream> {
final long count;
final long skip;
final int capacityHint;
public ObservableWindow(ObservableSource source, long count, long skip, int capacityHint) {
super(source);
this.count = count;
this.skip = skip;
this.capacityHint = capacityHint;
}
@Override
public void subscribeActual(Observer super Observable> t) {
if (count == skip) {
source.subscribe(new WindowExactObserver(t, count, capacityHint));
} else {
source.subscribe(new WindowSkipObserver(t, count, skip, capacityHint));
}
}
static final class WindowExactObserver
extends AtomicInteger
implements Observer, Disposable, Runnable {
private static final long serialVersionUID = -7481782523886138128L;
final Observer super Observable> actual;
final long count;
final int capacityHint;
long size;
Disposable s;
UnicastSubject window;
volatile boolean cancelled;
WindowExactObserver(Observer super Observable> actual, long count, int capacityHint) {
this.actual = actual;
this.count = count;
this.capacityHint = capacityHint;
}
@Override
public void onSubscribe(Disposable s) {
if (DisposableHelper.validate(this.s, s)) {
this.s = s;
actual.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
UnicastSubject w = window;
if (w == null && !cancelled) {
w = UnicastSubject.create(capacityHint, this);
window = w;
actual.onNext(w);
}
if (w != null) {
w.onNext(t);
if (++size >= count) {
size = 0;
window = null;
w.onComplete();
if (cancelled) {
s.dispose();
}
}
}
}
@Override
public void onError(Throwable t) {
UnicastSubject w = window;
if (w != null) {
window = null;
w.onError(t);
}
actual.onError(t);
}
@Override
public void onComplete() {
UnicastSubject w = window;
if (w != null) {
window = null;
w.onComplete();
}
actual.onComplete();
}
@Override
public void dispose() {
cancelled = true;
}
@Override
public boolean isDisposed() {
return cancelled;
}
@Override
public void run() {
if (cancelled) {
s.dispose();
}
}
}
static final class WindowSkipObserver extends AtomicBoolean
implements Observer, Disposable, Runnable {
private static final long serialVersionUID = 3366976432059579510L;
final Observer super Observable> actual;
final long count;
final long skip;
final int capacityHint;
final ArrayDeque> windows;
long index;
volatile boolean cancelled;
/** Counts how many elements were emitted to the very first window in windows. */
long firstEmission;
Disposable s;
final AtomicInteger wip = new AtomicInteger();
WindowSkipObserver(Observer super Observable> actual, long count, long skip, int capacityHint) {
this.actual = actual;
this.count = count;
this.skip = skip;
this.capacityHint = capacityHint;
this.windows = new ArrayDeque>();
}
@Override
public void onSubscribe(Disposable s) {
if (DisposableHelper.validate(this.s, s)) {
this.s = s;
actual.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
final ArrayDeque> ws = windows;
long i = index;
long s = skip;
if (i % s == 0 && !cancelled) {
wip.getAndIncrement();
UnicastSubject w = UnicastSubject.create(capacityHint, this);
ws.offer(w);
actual.onNext(w);
}
long c = firstEmission + 1;
for (UnicastSubject w : ws) {
w.onNext(t);
}
if (c >= count) {
ws.poll().onComplete();
if (ws.isEmpty() && cancelled) {
this.s.dispose();
return;
}
firstEmission = c - s;
} else {
firstEmission = c;
}
index = i + 1;
}
@Override
public void onError(Throwable t) {
final ArrayDeque> ws = windows;
while (!ws.isEmpty()) {
ws.poll().onError(t);
}
actual.onError(t);
}
@Override
public void onComplete() {
final ArrayDeque> ws = windows;
while (!ws.isEmpty()) {
ws.poll().onComplete();
}
actual.onComplete();
}
@Override
public void dispose() {
cancelled = true;
}
@Override
public boolean isDisposed() {
return cancelled;
}
@Override
public void run() {
if (wip.decrementAndGet() == 0) {
if (cancelled) {
s.dispose();
}
}
}
}
}