rx.operators.OperationThrottleFirst Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-core Show documentation
Show all versions of rxjava-core Show documentation
rxjava-core developed by Netflix
/**
* Copyright 2014 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 rx.operators;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import rx.Observable;
import rx.Observable.OnSubscribeFunc;
import rx.Observer;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
/**
* Throttle by windowing a stream and returning the first value in each window.
*/
public final class OperationThrottleFirst {
/**
* Throttles to first value in each window.
*
* @param items
* The {@link Observable} which is publishing events.
* @param windowDuration
* Duration of windows within with the first value will be chosen.
* @param unit
* The unit of time for the specified timeout.
* @return A {@link Func1} which performs the throttle operation.
*/
public static OnSubscribeFunc throttleFirst(Observable items, long windowDuration, TimeUnit unit) {
return throttleFirst(items, windowDuration, unit, Schedulers.computation());
}
/**
* Throttles to first value in each window.
*
* @param items
* The {@link Observable} which is publishing events.
* @param windowDuration
* Duration of windows within with the first value will be chosen.
* @param unit
* The unit of time for the specified timeout.
* @param scheduler
* The {@link Scheduler} to use internally to manage the timers which handle timeout for each event.
* @return A {@link Func1} which performs the throttle operation.
*/
public static OnSubscribeFunc throttleFirst(final Observable items, final long windowDuration, final TimeUnit unit, final Scheduler scheduler) {
return new OnSubscribeFunc() {
@Override
public Subscription onSubscribe(Observer super T> observer) {
final AtomicLong lastOnNext = new AtomicLong(0);
final long timeInMilliseconds = unit.toMillis(windowDuration);
return items.filter(new Func1() {
@Override
public Boolean call(T value) {
long now = scheduler.now();
if (lastOnNext.get() == 0 || now - lastOnNext.get() >= timeInMilliseconds) {
lastOnNext.set(now);
return Boolean.TRUE;
} else {
return Boolean.FALSE;
}
}
}).subscribe(observer);
}
};
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy