Debian Codegraph @

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