Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/flowable/FlowableCountSingle.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.Subscription;
17
18 import io.reactivex.rxjava3.core.*;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.fuseable.FuseToFlowable;
21 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
22 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
23
24 public final class FlowableCountSingle<T> extends Single<Long> implements FuseToFlowable<Long> {
25
26 final Flowable<T> source;
27
28 public FlowableCountSingle(Flowable<T> source) {
29 this.source = source;
30 }
31
32 @Override
33 protected void subscribeActual(SingleObserver<? super Long> observer) {
34 source.subscribe(new CountSubscriber(observer));
35 }
36
37 @Override
38 public Flowable<Long> fuseToFlowable() {
39 return RxJavaPlugins.onAssembly(new FlowableCount<>(source));
40 }
41
42 static final class CountSubscriber implements FlowableSubscriber<Object>, Disposable {
43
44 final SingleObserver<? super Long> downstream;
45
46 Subscription upstream;
47
48 long count;
49
50 CountSubscriber(SingleObserver<? super Long> downstream) {
51 this.downstream = downstream;
52 }
53
54 @Override
55 public void onSubscribe(Subscription s) {
56 if (SubscriptionHelper.validate(this.upstream, s)) {
57 this.upstream = s;
58 downstream.onSubscribe(this);
59 s.request(Long.MAX_VALUE);
60 }
61 }
62
63 @Override
64 public void onNext(Object t) {
65 count++;
66 }
67
68 @Override
69 public void onError(Throwable t) {
70 upstream = SubscriptionHelper.CANCELLED;
71 downstream.onError(t);
72 }
73
74 @Override
75 public void onComplete() {
76 upstream = SubscriptionHelper.CANCELLED;
77 downstream.onSuccess(count);
78 }
79
80 @Override
81 public void dispose() {
82 upstream.cancel();
83 upstream = SubscriptionHelper.CANCELLED;
84 }
85
86 @Override
87 public boolean isDisposed() {
88 return upstream == SubscriptionHelper.CANCELLED;
89 }
90 }
91 }