Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDetach.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
20 /**
21 * Breaks the references between the upstream and downstream when the Maybe terminates.
22 *
23 * @param <T> the value type
24 */
25 public final class MaybeDetach<T> extends AbstractMaybeWithUpstream<T, T> {
26
27 public MaybeDetach(MaybeSource<T> source) {
28 super(source);
29 }
30
31 @Override
32 protected void subscribeActual(MaybeObserver<? super T> observer) {
33 source.subscribe(new DetachMaybeObserver<>(observer));
34 }
35
36 static final class DetachMaybeObserver<T> implements MaybeObserver<T>, Disposable {
37
38 MaybeObserver<? super T> downstream;
39
40 Disposable upstream;
41
42 DetachMaybeObserver(MaybeObserver<? super T> downstream) {
43 this.downstream = downstream;
44 }
45
46 @Override
47 public void dispose() {
48 downstream = null;
49 upstream.dispose();
50 upstream = DisposableHelper.DISPOSED;
51 }
52
53 @Override
54 public boolean isDisposed() {
55 return upstream.isDisposed();
56 }
57
58 @Override
59 public void onSubscribe(Disposable d) {
60 if (DisposableHelper.validate(this.upstream, d)) {
61 this.upstream = d;
62
63 downstream.onSubscribe(this);
64 }
65 }
66
67 @Override
68 public void onSuccess(T value) {
69 upstream = DisposableHelper.DISPOSED;
70 MaybeObserver<? super T> a = downstream;
71 if (a != null) {
72 downstream = null;
73 a.onSuccess(value);
74 }
75 }
76
77 @Override
78 public void onError(Throwable e) {
79 upstream = DisposableHelper.DISPOSED;
80 MaybeObserver<? super T> a = downstream;
81 if (a != null) {
82 downstream = null;
83 a.onError(e);
84 }
85 }
86
87 @Override
88 public void onComplete() {
89 upstream = DisposableHelper.DISPOSED;
90 MaybeObserver<? super T> a = downstream;
91 if (a != null) {
92 downstream = null;
93 a.onComplete();
94 }
95 }
96 }
97 }