Debian Codegraph @

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