Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableTimeInterval.java

raw
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.observable;
15
16 import java.util.concurrent.TimeUnit;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.schedulers.Timed;
22
23 public final class ObservableTimeInterval<T> extends AbstractObservableWithUpstream<T, Timed<T>> {
24 final Scheduler scheduler;
25 final TimeUnit unit;
26
27 public ObservableTimeInterval(ObservableSource<T> source, TimeUnit unit, Scheduler scheduler) {
28 super(source);
29 this.scheduler = scheduler;
30 this.unit = unit;
31 }
32
33 @Override
34 public void subscribeActual(Observer<? super Timed<T>> t) {
35 source.subscribe(new TimeIntervalObserver<>(t, unit, scheduler));
36 }
37
38 static final class TimeIntervalObserver<T> implements Observer<T>, Disposable {
39 final Observer<? super Timed<T>> downstream;
40 final TimeUnit unit;
41 final Scheduler scheduler;
42
43 long lastTime;
44
45 Disposable upstream;
46
47 TimeIntervalObserver(Observer<? super Timed<T>> actual, TimeUnit unit, Scheduler scheduler) {
48 this.downstream = actual;
49 this.scheduler = scheduler;
50 this.unit = unit;
51 }
52
53 @Override
54 public void onSubscribe(Disposable d) {
55 if (DisposableHelper.validate(this.upstream, d)) {
56 this.upstream = d;
57 lastTime = scheduler.now(unit);
58 downstream.onSubscribe(this);
59 }
60 }
61
62 @Override
63 public void dispose() {
64 upstream.dispose();
65 }
66
67 @Override
68 public boolean isDisposed() {
69 return upstream.isDisposed();
70 }
71
72 @Override
73 public void onNext(T t) {
74 long now = scheduler.now(unit);
75 long last = lastTime;
76 lastTime = now;
77 long delta = now - last;
78 downstream.onNext(new Timed<>(t, delta, unit));
79 }
80
81 @Override
82 public void onError(Throwable t) {
83 downstream.onError(t);
84 }
85
86 @Override
87 public void onComplete() {
88 downstream.onComplete();
89 }
90 }
91 }