| 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.single; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
|
| 21 |
public final class SingleCache<T> extends Single<T> implements SingleObserver<T> { |
| 22 |
|
| 23 |
@SuppressWarnings("rawtypes") |
| 24 |
static final CacheDisposable[] EMPTY = new CacheDisposable[0]; |
| 25 |
@SuppressWarnings("rawtypes") |
| 26 |
static final CacheDisposable[] TERMINATED = new CacheDisposable[0]; |
| 27 |
|
| 28 |
final SingleSource<? extends T> source; |
| 29 |
|
| 30 |
final AtomicInteger wip; |
| 31 |
|
| 32 |
final AtomicReference<CacheDisposable<T>[]> observers; |
| 33 |
|
| 34 |
T value; |
| 35 |
|
| 36 |
Throwable error; |
| 37 |
|
| 38 |
@SuppressWarnings("unchecked") |
| 39 |
public SingleCache(SingleSource<? extends T> source) { |
| 40 |
this.source = source; |
| 41 |
this.wip = new AtomicInteger(); |
| 42 |
this.observers = new AtomicReference<>(EMPTY); |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
protected void subscribeActual(final SingleObserver<? super T> observer) { |
| 47 |
CacheDisposable<T> d = new CacheDisposable<>(observer, this); |
| 48 |
observer.onSubscribe(d); |
| 49 |
|
| 50 |
if (add(d)) { |
| 51 |
if (d.isDisposed()) { |
| 52 |
remove(d); |
| 53 |
} |
| 54 |
} else { |
| 55 |
Throwable ex = error; |
| 56 |
if (ex != null) { |
| 57 |
observer.onError(ex); |
| 58 |
} else { |
| 59 |
observer.onSuccess(value); |
| 60 |
} |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
if (wip.getAndIncrement() == 0) { |
| 65 |
source.subscribe(this); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
boolean add(CacheDisposable<T> observer) { |
| 70 |
for (;;) { |
| 71 |
CacheDisposable<T>[] a = observers.get(); |
| 72 |
if (a == TERMINATED) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
int n = a.length; |
| 76 |
@SuppressWarnings("unchecked") |
| 77 |
CacheDisposable<T>[] b = new CacheDisposable[n + 1]; |
| 78 |
System.arraycopy(a, 0, b, 0, n); |
| 79 |
b[n] = observer; |
| 80 |
if (observers.compareAndSet(a, b)) { |
| 81 |
return true; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
@SuppressWarnings("unchecked") |
| 87 |
void remove(CacheDisposable<T> observer) { |
| 88 |
for (;;) { |
| 89 |
CacheDisposable<T>[] a = observers.get(); |
| 90 |
int n = a.length; |
| 91 |
if (n == 0) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
int j = -1; |
| 96 |
for (int i = 0; i < n; i++) { |
| 97 |
if (a[i] == observer) { |
| 98 |
j = i; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (j < 0) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
CacheDisposable<T>[] b; |
| 108 |
|
| 109 |
if (n == 1) { |
| 110 |
b = EMPTY; |
| 111 |
} else { |
| 112 |
b = new CacheDisposable[n - 1]; |
| 113 |
System.arraycopy(a, 0, b, 0, j); |
| 114 |
System.arraycopy(a, j + 1, b, j, n - j - 1); |
| 115 |
} |
| 116 |
if (observers.compareAndSet(a, b)) { |
| 117 |
return; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public void onSubscribe(Disposable d) { |
| 124 |
// not supported by this operator |
| 125 |
} |
| 126 |
|
| 127 |
@SuppressWarnings("unchecked") |
| 128 |
@Override |
| 129 |
public void onSuccess(T value) { |
| 130 |
this.value = value; |
| 131 |
|
| 132 |
for (CacheDisposable<T> d : observers.getAndSet(TERMINATED)) { |
| 133 |
if (!d.isDisposed()) { |
| 134 |
d.downstream.onSuccess(value); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
@SuppressWarnings("unchecked") |
| 140 |
@Override |
| 141 |
public void onError(Throwable e) { |
| 142 |
this.error = e; |
| 143 |
|
| 144 |
for (CacheDisposable<T> d : observers.getAndSet(TERMINATED)) { |
| 145 |
if (!d.isDisposed()) { |
| 146 |
d.downstream.onError(e); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
static final class CacheDisposable<T> |
| 152 |
extends AtomicBoolean |
| 153 |
implements Disposable { |
| 154 |
|
| 155 |
private static final long serialVersionUID = 7514387411091976596L; |
| 156 |
|
| 157 |
final SingleObserver<? super T> downstream; |
| 158 |
|
| 159 |
final SingleCache<T> parent; |
| 160 |
|
| 161 |
CacheDisposable(SingleObserver<? super T> actual, SingleCache<T> parent) { |
| 162 |
this.downstream = actual; |
| 163 |
this.parent = parent; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public boolean isDisposed() { |
| 168 |
return get(); |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void dispose() { |
| 173 |
if (compareAndSet(false, true)) { |
| 174 |
parent.remove(this); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |