Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeCache.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
21 /**
22 * Consumes the source once and replays its signal to any current or future MaybeObservers.
23 *
24 * @param <T> the value type
25 */
26 public final class MaybeCache<T> extends Maybe<T> implements MaybeObserver<T> {
27
28 @SuppressWarnings("rawtypes")
29 static final CacheDisposable[] EMPTY = new CacheDisposable[0];
30
31 @SuppressWarnings("rawtypes")
32 static final CacheDisposable[] TERMINATED = new CacheDisposable[0];
33
34 final AtomicReference<MaybeSource<T>> source;
35
36 final AtomicReference<CacheDisposable<T>[]> observers;
37
38 T value;
39
40 Throwable error;
41
42 @SuppressWarnings("unchecked")
43 public MaybeCache(MaybeSource<T> source) {
44 this.source = new AtomicReference<>(source);
45 this.observers = new AtomicReference<>(EMPTY);
46 }
47
48 @Override
49 protected void subscribeActual(MaybeObserver<? super T> observer) {
50 CacheDisposable<T> parent = new CacheDisposable<>(observer, this);
51 observer.onSubscribe(parent);
52
53 if (add(parent)) {
54 if (parent.isDisposed()) {
55 remove(parent);
56 return;
57 }
58 } else {
59 if (!parent.isDisposed()) {
60 Throwable ex = error;
61 if (ex != null) {
62 observer.onError(ex);
63 } else {
64 T v = value;
65 if (v != null) {
66 observer.onSuccess(v);
67 } else {
68 observer.onComplete();
69 }
70 }
71 }
72 return;
73 }
74
75 MaybeSource<T> src = source.getAndSet(null);
76 if (src != null) {
77 src.subscribe(this);
78 }
79 }
80
81 @Override
82 public void onSubscribe(Disposable d) {
83 // deliberately ignored
84 }
85
86 @SuppressWarnings("unchecked")
87 @Override
88 public void onSuccess(T value) {
89 this.value = value;
90 for (CacheDisposable<T> inner : observers.getAndSet(TERMINATED)) {
91 if (!inner.isDisposed()) {
92 inner.downstream.onSuccess(value);
93 }
94 }
95 }
96
97 @SuppressWarnings("unchecked")
98 @Override
99 public void onError(Throwable e) {
100 this.error = e;
101 for (CacheDisposable<T> inner : observers.getAndSet(TERMINATED)) {
102 if (!inner.isDisposed()) {
103 inner.downstream.onError(e);
104 }
105 }
106 }
107
108 @SuppressWarnings("unchecked")
109 @Override
110 public void onComplete() {
111 for (CacheDisposable<T> inner : observers.getAndSet(TERMINATED)) {
112 if (!inner.isDisposed()) {
113 inner.downstream.onComplete();
114 }
115 }
116 }
117
118 boolean add(CacheDisposable<T> inner) {
119 for (;;) {
120 CacheDisposable<T>[] a = observers.get();
121 if (a == TERMINATED) {
122 return false;
123 }
124 int n = a.length;
125
126 @SuppressWarnings("unchecked")
127 CacheDisposable<T>[] b = new CacheDisposable[n + 1];
128 System.arraycopy(a, 0, b, 0, n);
129 b[n] = inner;
130 if (observers.compareAndSet(a, b)) {
131 return true;
132 }
133 }
134 }
135
136 @SuppressWarnings("unchecked")
137 void remove(CacheDisposable<T> inner) {
138 for (;;) {
139 CacheDisposable<T>[] a = observers.get();
140 int n = a.length;
141 if (n == 0) {
142 return;
143 }
144
145 int j = -1;
146
147 for (int i = 0; i < n; i++) {
148 if (a[i] == inner) {
149 j = i;
150 break;
151 }
152 }
153
154 if (j < 0) {
155 return;
156 }
157
158 CacheDisposable<T>[] b;
159 if (n == 1) {
160 b = EMPTY;
161 } else {
162 b = new CacheDisposable[n - 1];
163 System.arraycopy(a, 0, b, 0, j);
164 System.arraycopy(a, j + 1, b, j, n - j - 1);
165 }
166 if (observers.compareAndSet(a, b)) {
167 return;
168 }
169 }
170 }
171
172 static final class CacheDisposable<T>
173 extends AtomicReference<MaybeCache<T>>
174 implements Disposable {
175
176 private static final long serialVersionUID = -5791853038359966195L;
177
178 final MaybeObserver<? super T> downstream;
179
180 CacheDisposable(MaybeObserver<? super T> actual, MaybeCache<T> parent) {
181 super(parent);
182 this.downstream = actual;
183 }
184
185 @Override
186 public void dispose() {
187 MaybeCache<T> mc = getAndSet(null);
188 if (mc != null) {
189 mc.remove(this);
190 }
191 }
192
193 @Override
194 public boolean isDisposed() {
195 return get() == null;
196 }
197 }
198 }