Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableCount.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.flowable;
15
16 import org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.internal.subscriptions.*;
20
21 public final class FlowableCount<T> extends AbstractFlowableWithUpstream<T, Long> {
22
23 public FlowableCount(Flowable<T> source) {
24 super(source);
25 }
26
27 @Override
28 protected void subscribeActual(Subscriber<? super Long> s) {
29 source.subscribe(new CountSubscriber(s));
30 }
31
32 static final class CountSubscriber extends DeferredScalarSubscription<Long>
33 implements FlowableSubscriber<Object> {
34
35 private static final long serialVersionUID = 4973004223787171406L;
36
37 Subscription upstream;
38
39 long count;
40
41 CountSubscriber(Subscriber<? super Long> downstream) {
42 super(downstream);
43 }
44
45 @Override
46 public void onSubscribe(Subscription s) {
47 if (SubscriptionHelper.validate(this.upstream, s)) {
48 this.upstream = s;
49 downstream.onSubscribe(this);
50 s.request(Long.MAX_VALUE);
51 }
52 }
53
54 @Override
55 public void onNext(Object t) {
56 count++;
57 }
58
59 @Override
60 public void onError(Throwable t) {
61 downstream.onError(t);
62 }
63
64 @Override
65 public void onComplete() {
66 complete(count);
67 }
68
69 @Override
70 public void cancel() {
71 super.cancel();
72 upstream.cancel();
73 }
74 }
75 }