Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleTimeInterval.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.single;
15
16 import java.util.concurrent.TimeUnit;
17
18 import io.reactivex.rxjava3.annotations.NonNull;
19 import io.reactivex.rxjava3.core.*;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
22 import io.reactivex.rxjava3.schedulers.Timed;
23
24 /**
25 * Measures the time between subscription and the success item emission
26 * from the upstream and emits this as a {@link Timed} success value.
27 * @param <T> the element type of the sequence
28 * @since 3.0.0
29 */
30 public final class SingleTimeInterval<T> extends Single<Timed<T>> {
31
32 final SingleSource<T> source;
33
34 final TimeUnit unit;
35
36 final Scheduler scheduler;
37
38 final boolean start;
39
40 public SingleTimeInterval(SingleSource<T> source, TimeUnit unit, Scheduler scheduler, boolean start) {
41 this.source = source;
42 this.unit = unit;
43 this.scheduler = scheduler;
44 this.start = start;
45 }
46
47 @Override
48 protected void subscribeActual(@NonNull SingleObserver<? super @NonNull Timed<T>> observer) {
49 source.subscribe(new TimeIntervalSingleObserver<>(observer, unit, scheduler, start));
50 }
51
52 static final class TimeIntervalSingleObserver<T> implements SingleObserver<T>, Disposable {
53
54 final SingleObserver<? super Timed<T>> downstream;
55
56 final TimeUnit unit;
57
58 final Scheduler scheduler;
59
60 final long startTime;
61
62 Disposable upstream;
63
64 TimeIntervalSingleObserver(SingleObserver<? super Timed<T>> downstream, TimeUnit unit, Scheduler scheduler, boolean start) {
65 this.downstream = downstream;
66 this.unit = unit;
67 this.scheduler = scheduler;
68 this.startTime = start ? scheduler.now(unit) : 0L;
69 }
70
71 @Override
72 public void onSubscribe(@NonNull Disposable d) {
73 if (DisposableHelper.validate(this.upstream, d)) {
74 this.upstream = d;
75
76 downstream.onSubscribe(this);
77 }
78 }
79
80 @Override
81 public void onSuccess(@NonNull T t) {
82 downstream.onSuccess(new Timed<>(t, scheduler.now(unit) - startTime, unit));
83 }
84
85 @Override
86 public void onError(@NonNull Throwable e) {
87 downstream.onError(e);
88 }
89
90 @Override
91 public void dispose() {
92 upstream.dispose();
93 }
94
95 @Override
96 public boolean isDisposed() {
97 return upstream.isDisposed();
98 }
99 }
100 }