hu.akarnokd.rxjava2.internal.operators.OperatorSampleTimed Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava2-backport Show documentation
Show all versions of rxjava2-backport Show documentation
rxjava2-backport developed by David Karnok
/**
* Copyright 2015 David Karnok and Netflix, Inc.
*
* 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 hu.akarnokd.rxjava2.internal.operators;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;
import org.reactivestreams.*;
import hu.akarnokd.rxjava2.Observable.Operator;
import hu.akarnokd.rxjava2.Scheduler;
import hu.akarnokd.rxjava2.disposables.Disposable;
import hu.akarnokd.rxjava2.internal.subscriptions.SubscriptionHelper;
import hu.akarnokd.rxjava2.internal.util.BackpressureHelper;
import hu.akarnokd.rxjava2.subscribers.SerializedSubscriber;
public final class OperatorSampleTimed implements Operator {
final long period;
final TimeUnit unit;
final Scheduler scheduler;
public OperatorSampleTimed(long period, TimeUnit unit, Scheduler scheduler) {
this.period = period;
this.unit = unit;
this.scheduler = scheduler;
}
@Override
public Subscriber super T> apply(Subscriber super T> t) {
SerializedSubscriber serial = new SerializedSubscriber(t);
return new SampleTimedSubscriber(serial, period, unit, scheduler);
}
static final class SampleTimedSubscriber extends AtomicReference implements Subscriber, Subscription, Runnable {
/** */
private static final long serialVersionUID = -3517602651313910099L;
final Subscriber super T> actual;
final long period;
final TimeUnit unit;
final Scheduler scheduler;
volatile long requested;
@SuppressWarnings("rawtypes")
static final AtomicLongFieldUpdater REQUESTED =
AtomicLongFieldUpdater.newUpdater(SampleTimedSubscriber.class, "requested");
volatile Disposable timer;
@SuppressWarnings("rawtypes")
static final AtomicReferenceFieldUpdater TIMER =
AtomicReferenceFieldUpdater.newUpdater(SampleTimedSubscriber.class, Disposable.class, "timer");
static final Disposable DISPOSED = new Disposable() {
@Override
public void dispose() { }
};
Subscription s;
public SampleTimedSubscriber(Subscriber super T> actual, long period, TimeUnit unit, Scheduler scheduler) {
this.actual = actual;
this.period = period;
this.unit = unit;
this.scheduler = scheduler;
}
@Override
public void onSubscribe(Subscription s) {
if (SubscriptionHelper.validateSubscription(this.s, s)) {
return;
}
this.s = s;
actual.onSubscribe(this);
if (timer == null) {
Disposable d = scheduler.schedulePeriodicallyDirect(this, period, period, unit);
if (!TIMER.compareAndSet(this, null, d)) {
d.dispose();
return;
}
s.request(Long.MAX_VALUE);
}
}
@Override
public void onNext(T t) {
lazySet(t);
}
@Override
public void onError(Throwable t) {
cancelTimer();
actual.onError(t);
}
@Override
public void onComplete() {
cancelTimer();
actual.onComplete();
}
void cancelTimer() {
Disposable d = timer;
if (d != DISPOSED) {
d = TIMER.getAndSet(this, DISPOSED);
if (d != DISPOSED && d != null) {
d.dispose();
}
}
}
@Override
public void request(long n) {
if (SubscriptionHelper.validateRequest(n)) {
return;
}
BackpressureHelper.add(REQUESTED, this, n);
}
@Override
public void cancel() {
cancelTimer();
s.cancel();
}
@Override
public void run() {
T value = getAndSet(null);
if (value != null) {
long r = requested;
if (r != 0L) {
actual.onNext(value);
if (r != Long.MAX_VALUE) {
REQUESTED.decrementAndGet(this);
}
} else {
cancel();
actual.onError(new IllegalStateException("Couldn't emit value due to lack of requests!"));
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy