Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableCountSingle.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 io.reactivex.rxjava3.core.*;
17 import io.reactivex.rxjava3.disposables.Disposable;
18 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
19 import io.reactivex.rxjava3.internal.fuseable.FuseToObservable;
20 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
21
22 public final class ObservableCountSingle<T> extends Single<Long> implements FuseToObservable<Long> {
23 final ObservableSource<T> source;
24 public ObservableCountSingle(ObservableSource<T> source) {
25 this.source = source;
26 }
27
28 @Override
29 public void subscribeActual(SingleObserver<? super Long> t) {
30 source.subscribe(new CountObserver(t));
31 }
32
33 @Override
34 public Observable<Long> fuseToObservable() {
35 return RxJavaPlugins.onAssembly(new ObservableCount<>(source));
36 }
37
38 static final class CountObserver implements Observer<Object>, Disposable {
39 final SingleObserver<? super Long> downstream;
40
41 Disposable upstream;
42
43 long count;
44
45 CountObserver(SingleObserver<? super Long> downstream) {
46 this.downstream = downstream;
47 }
48
49 @Override
50 public void onSubscribe(Disposable d) {
51 if (DisposableHelper.validate(this.upstream, d)) {
52 this.upstream = d;
53 downstream.onSubscribe(this);
54 }
55 }
56
57 @Override
58 public void dispose() {
59 upstream.dispose();
60 upstream = DisposableHelper.DISPOSED;
61 }
62
63 @Override
64 public boolean isDisposed() {
65 return upstream.isDisposed();
66 }
67
68 @Override
69 public void onNext(Object t) {
70 count++;
71 }
72
73 @Override
74 public void onError(Throwable t) {
75 upstream = DisposableHelper.DISPOSED;
76 downstream.onError(t);
77 }
78
79 @Override
80 public void onComplete() {
81 upstream = DisposableHelper.DISPOSED;
82 downstream.onSuccess(count);
83 }
84 }
85 }