Debian Codegraph @

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