io.reactivex.rxjava3.internal.operators.observable.ObservableWindow Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/*
* 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.rxjava3.internal.operators.observable;
import java.util.ArrayDeque;
import java.util.concurrent.atomic.*;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
import io.reactivex.rxjava3.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> downstream;
final long count;
final int capacityHint;
final AtomicBoolean cancelled;
long size;
Disposable upstream;
UnicastSubject window;
WindowExactObserver(Observer super Observable> actual, long count, int capacityHint) {
this.downstream = actual;
this.count = count;
this.capacityHint = capacityHint;
this.cancelled = new AtomicBoolean();
this.lazySet(1);
}
@Override
public void onSubscribe(Disposable d) {
if (DisposableHelper.validate(this.upstream, d)) {
this.upstream = d;
downstream.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
UnicastSubject w = window;
ObservableWindowSubscribeIntercept intercept = null;
if (w == null && !cancelled.get()) {
getAndIncrement();
w = UnicastSubject.create(capacityHint, this);
window = w;
intercept = new ObservableWindowSubscribeIntercept<>(w);
downstream.onNext(intercept);
}
if (w != null) {
w.onNext(t);
if (++size >= count) {
size = 0;
window = null;
w.onComplete();
}
if (intercept != null && intercept.tryAbandon()) {
window = null;
w.onComplete();
w = null;
}
}
}
@Override
public void onError(Throwable t) {
UnicastSubject w = window;
if (w != null) {
window = null;
w.onError(t);
}
downstream.onError(t);
}
@Override
public void onComplete() {
UnicastSubject w = window;
if (w != null) {
window = null;
w.onComplete();
}
downstream.onComplete();
}
@Override
public void dispose() {
if (cancelled.compareAndSet(false, true)) {
run();
}
}
@Override
public boolean isDisposed() {
return cancelled.get();
}
@Override
public void run() {
if (decrementAndGet() == 0) {
upstream.dispose();
}
}
}
static final class WindowSkipObserver extends AtomicInteger
implements Observer, Disposable, Runnable {
private static final long serialVersionUID = 3366976432059579510L;
final Observer super Observable> downstream;
final long count;
final long skip;
final int capacityHint;
final ArrayDeque> windows;
final AtomicBoolean cancelled;
long index;
/** Counts how many elements were emitted to the very first window in windows. */
long firstEmission;
Disposable upstream;
WindowSkipObserver(Observer super Observable> actual, long count, long skip, int capacityHint) {
this.downstream = actual;
this.count = count;
this.skip = skip;
this.capacityHint = capacityHint;
this.windows = new ArrayDeque<>();
this.cancelled = new AtomicBoolean();
this.lazySet(1);
}
@Override
public void onSubscribe(Disposable d) {
if (DisposableHelper.validate(this.upstream, d)) {
this.upstream = d;
downstream.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
final ArrayDeque> ws = windows;
long i = index;
long s = skip;
ObservableWindowSubscribeIntercept intercept = null;
if (i % s == 0 && !cancelled.get()) {
getAndIncrement();
UnicastSubject w = UnicastSubject.create(capacityHint, this);
intercept = new ObservableWindowSubscribeIntercept<>(w);
ws.offer(w);
downstream.onNext(intercept);
}
long c = firstEmission + 1;
for (UnicastSubject w : ws) {
w.onNext(t);
}
if (c >= count) {
ws.poll().onComplete();
if (ws.isEmpty() && cancelled.get()) {
return;
}
firstEmission = c - s;
} else {
firstEmission = c;
}
index = i + 1;
if (intercept != null && intercept.tryAbandon()) {
intercept.window.onComplete();
}
}
@Override
public void onError(Throwable t) {
final ArrayDeque> ws = windows;
while (!ws.isEmpty()) {
ws.poll().onError(t);
}
downstream.onError(t);
}
@Override
public void onComplete() {
final ArrayDeque> ws = windows;
while (!ws.isEmpty()) {
ws.poll().onComplete();
}
downstream.onComplete();
}
@Override
public void dispose() {
if (cancelled.compareAndSet(false, true)) {
run();
}
}
@Override
public boolean isDisposed() {
return cancelled.get();
}
@Override
public void run() {
if (decrementAndGet() == 0) {
upstream.dispose();
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy