| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.operators.flowable; |
| 15 |
|
| 16 |
import java.util.concurrent.TimeUnit; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.core.*; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 22 |
import io.reactivex.rxjava3.schedulers.Timed; |
| 23 |
|
| 24 |
public final class FlowableTimeInterval<T> extends AbstractFlowableWithUpstream<T, Timed<T>> { |
| 25 |
final Scheduler scheduler; |
| 26 |
final TimeUnit unit; |
| 27 |
|
| 28 |
public FlowableTimeInterval(Flowable<T> source, TimeUnit unit, Scheduler scheduler) { |
| 29 |
super(source); |
| 30 |
this.scheduler = scheduler; |
| 31 |
this.unit = unit; |
| 32 |
} |
| 33 |
|
| 34 |
@Override |
| 35 |
protected void subscribeActual(Subscriber<? super Timed<T>> s) { |
| 36 |
source.subscribe(new TimeIntervalSubscriber<>(s, unit, scheduler)); |
| 37 |
} |
| 38 |
|
| 39 |
static final class TimeIntervalSubscriber<T> implements FlowableSubscriber<T>, Subscription { |
| 40 |
final Subscriber<? super Timed<T>> downstream; |
| 41 |
final TimeUnit unit; |
| 42 |
final Scheduler scheduler; |
| 43 |
|
| 44 |
Subscription upstream; |
| 45 |
|
| 46 |
long lastTime; |
| 47 |
|
| 48 |
TimeIntervalSubscriber(Subscriber<? super Timed<T>> actual, TimeUnit unit, Scheduler scheduler) { |
| 49 |
this.downstream = actual; |
| 50 |
this.scheduler = scheduler; |
| 51 |
this.unit = unit; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onSubscribe(Subscription s) { |
| 56 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 57 |
lastTime = scheduler.now(unit); |
| 58 |
this.upstream = s; |
| 59 |
downstream.onSubscribe(this); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void onNext(T t) { |
| 65 |
long now = scheduler.now(unit); |
| 66 |
long last = lastTime; |
| 67 |
lastTime = now; |
| 68 |
long delta = now - last; |
| 69 |
downstream.onNext(new Timed<>(t, delta, unit)); |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void onError(Throwable t) { |
| 74 |
downstream.onError(t); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onComplete() { |
| 79 |
downstream.onComplete(); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public void request(long n) { |
| 84 |
upstream.request(n); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void cancel() { |
| 89 |
upstream.cancel(); |
| 90 |
} |
| 91 |
} |
| 92 |
} |