Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDelaySubscriptionOtherPublisher.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 java.util.concurrent.atomic.AtomicReference;
17
18 import org.reactivestreams.*;
19
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
23 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
24 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
25
26 /**
27 * Delay the subscription to the main Maybe until the other signals an item or completes.
28 *
29 * @param <T> the main value type
30 * @param <U> the other value type
31 */
32 public final class MaybeDelaySubscriptionOtherPublisher<T, U> extends AbstractMaybeWithUpstream<T, T> {
33
34 final Publisher<U> other;
35
36 public MaybeDelaySubscriptionOtherPublisher(MaybeSource<T> source, Publisher<U> other) {
37 super(source);
38 this.other = other;
39 }
40
41 @Override
42 protected void subscribeActual(MaybeObserver<? super T> observer) {
43 other.subscribe(new OtherSubscriber<>(observer, source));
44 }
45
46 static final class OtherSubscriber<T> implements FlowableSubscriber<Object>, Disposable {
47 final DelayMaybeObserver<T> main;
48
49 MaybeSource<T> source;
50
51 Subscription upstream;
52
53 OtherSubscriber(MaybeObserver<? super T> actual, MaybeSource<T> source) {
54 this.main = new DelayMaybeObserver<>(actual);
55 this.source = source;
56 }
57
58 @Override
59 public void onSubscribe(Subscription s) {
60 if (SubscriptionHelper.validate(this.upstream, s)) {
61 this.upstream = s;
62
63 main.downstream.onSubscribe(this);
64
65 s.request(Long.MAX_VALUE);
66 }
67 }
68
69 @Override
70 public void onNext(Object t) {
71 if (upstream != SubscriptionHelper.CANCELLED) {
72 upstream.cancel();
73 upstream = SubscriptionHelper.CANCELLED;
74
75 subscribeNext();
76 }
77 }
78
79 @Override
80 public void onError(Throwable t) {
81 if (upstream != SubscriptionHelper.CANCELLED) {
82 upstream = SubscriptionHelper.CANCELLED;
83
84 main.downstream.onError(t);
85 } else {
86 RxJavaPlugins.onError(t);
87 }
88 }
89
90 @Override
91 public void onComplete() {
92 if (upstream != SubscriptionHelper.CANCELLED) {
93 upstream = SubscriptionHelper.CANCELLED;
94
95 subscribeNext();
96 }
97 }
98
99 void subscribeNext() {
100 MaybeSource<T> src = source;
101 source = null;
102
103 src.subscribe(main);
104 }
105
106 @Override
107 public boolean isDisposed() {
108 return DisposableHelper.isDisposed(main.get());
109 }
110
111 @Override
112 public void dispose() {
113 upstream.cancel();
114 upstream = SubscriptionHelper.CANCELLED;
115 DisposableHelper.dispose(main);
116 }
117 }
118
119 static final class DelayMaybeObserver<T> extends AtomicReference<Disposable>
120 implements MaybeObserver<T> {
121
122 private static final long serialVersionUID = 706635022205076709L;
123
124 final MaybeObserver<? super T> downstream;
125
126 DelayMaybeObserver(MaybeObserver<? super T> downstream) {
127 this.downstream = downstream;
128 }
129
130 @Override
131 public void onSubscribe(Disposable d) {
132 DisposableHelper.setOnce(this, d);
133 }
134
135 @Override
136 public void onSuccess(T value) {
137 downstream.onSuccess(value);
138 }
139
140 @Override
141 public void onError(Throwable e) {
142 downstream.onError(e);
143 }
144
145 @Override
146 public void onComplete() {
147 downstream.onComplete();
148 }
149 }
150 }