Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeCount.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.maybe;
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.HasUpstreamMaybeSource;
20
21 /**
22 * Signals 1L if the source signalled an item or 0L if the source is empty.
23 *
24 * @param <T> the source value type
25 */
26 public final class MaybeCount<T> extends Single<Long> implements HasUpstreamMaybeSource<T> {
27
28 final MaybeSource<T> source;
29
30 public MaybeCount(MaybeSource<T> source) {
31 this.source = source;
32 }
33
34 @Override
35 public MaybeSource<T> source() {
36 return source;
37 }
38
39 @Override
40 protected void subscribeActual(SingleObserver<? super Long> observer) {
41 source.subscribe(new CountMaybeObserver(observer));
42 }
43
44 static final class CountMaybeObserver implements MaybeObserver<Object>, Disposable {
45 final SingleObserver<? super Long> downstream;
46
47 Disposable upstream;
48
49 CountMaybeObserver(SingleObserver<? super Long> downstream) {
50 this.downstream = downstream;
51 }
52
53 @Override
54 public void onSubscribe(Disposable d) {
55 if (DisposableHelper.validate(this.upstream, d)) {
56 this.upstream = d;
57
58 downstream.onSubscribe(this);
59 }
60 }
61
62 @Override
63 public void onSuccess(Object value) {
64 upstream = DisposableHelper.DISPOSED;
65 downstream.onSuccess(1L);
66 }
67
68 @Override
69 public void onError(Throwable e) {
70 upstream = DisposableHelper.DISPOSED;
71 downstream.onError(e);
72 }
73
74 @Override
75 public void onComplete() {
76 upstream = DisposableHelper.DISPOSED;
77 downstream.onSuccess(0L);
78 }
79
80 @Override
81 public boolean isDisposed() {
82 return upstream.isDisposed();
83 }
84
85 @Override
86 public void dispose() {
87 upstream.dispose();
88 upstream = DisposableHelper.DISPOSED;
89 }
90 }
91 }