| 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.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 25 |
|
| 26 |
public final class SingleZipArray<T, R> extends Single<R> { |
| 27 |
|
| 28 |
final SingleSource<? extends T>[] sources; |
| 29 |
|
| 30 |
final Function<? super Object[], ? extends R> zipper; |
| 31 |
|
| 32 |
public SingleZipArray(SingleSource<? extends T>[] sources, Function<? super Object[], ? extends R> zipper) { |
| 33 |
this.sources = sources; |
| 34 |
this.zipper = zipper; |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
protected void subscribeActual(SingleObserver<? super R> observer) { |
| 39 |
SingleSource<? extends T>[] sources = this.sources; |
| 40 |
int n = sources.length; |
| 41 |
|
| 42 |
if (n == 1) { |
| 43 |
sources[0].subscribe(new SingleMap.MapSingleObserver<>(observer, new SingletonArrayFunc())); |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
ZipCoordinator<T, R> parent = new ZipCoordinator<>(observer, n, zipper); |
| 48 |
|
| 49 |
observer.onSubscribe(parent); |
| 50 |
|
| 51 |
for (int i = 0; i < n; i++) { |
| 52 |
if (parent.isDisposed()) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
SingleSource<? extends T> source = sources[i]; |
| 57 |
|
| 58 |
if (source == null) { |
| 59 |
parent.innerError(new NullPointerException("One of the sources is null"), i); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
source.subscribe(parent.observers[i]); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static final class ZipCoordinator<T, R> extends AtomicInteger implements Disposable { |
| 68 |
|
| 69 |
private static final long serialVersionUID = -5556924161382950569L; |
| 70 |
|
| 71 |
final SingleObserver<? super R> downstream; |
| 72 |
|
| 73 |
final Function<? super Object[], ? extends R> zipper; |
| 74 |
|
| 75 |
final ZipSingleObserver<T>[] observers; |
| 76 |
|
| 77 |
Object[] values; |
| 78 |
|
| 79 |
@SuppressWarnings("unchecked") |
| 80 |
ZipCoordinator(SingleObserver<? super R> observer, int n, Function<? super Object[], ? extends R> zipper) { |
| 81 |
super(n); |
| 82 |
this.downstream = observer; |
| 83 |
this.zipper = zipper; |
| 84 |
ZipSingleObserver<T>[] o = new ZipSingleObserver[n]; |
| 85 |
for (int i = 0; i < n; i++) { |
| 86 |
o[i] = new ZipSingleObserver<>(this, i); |
| 87 |
} |
| 88 |
this.observers = o; |
| 89 |
this.values = new Object[n]; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public boolean isDisposed() { |
| 94 |
return get() <= 0; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void dispose() { |
| 99 |
if (getAndSet(0) > 0) { |
| 100 |
for (ZipSingleObserver<?> d : observers) { |
| 101 |
d.dispose(); |
| 102 |
} |
| 103 |
|
| 104 |
values = null; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
void innerSuccess(T value, int index) { |
| 109 |
Object[] values = this.values; |
| 110 |
if (values != null) { |
| 111 |
values[index] = value; |
| 112 |
} |
| 113 |
if (decrementAndGet() == 0) { |
| 114 |
R v; |
| 115 |
|
| 116 |
try { |
| 117 |
v = Objects.requireNonNull(zipper.apply(values), "The zipper returned a null value"); |
| 118 |
} catch (Throwable ex) { |
| 119 |
Exceptions.throwIfFatal(ex); |
| 120 |
this.values = null; |
| 121 |
downstream.onError(ex); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
this.values = null; |
| 126 |
downstream.onSuccess(v); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
void disposeExcept(int index) { |
| 131 |
ZipSingleObserver<T>[] observers = this.observers; |
| 132 |
int n = observers.length; |
| 133 |
for (int i = 0; i < index; i++) { |
| 134 |
observers[i].dispose(); |
| 135 |
} |
| 136 |
for (int i = index + 1; i < n; i++) { |
| 137 |
observers[i].dispose(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void innerError(Throwable ex, int index) { |
| 142 |
if (getAndSet(0) > 0) { |
| 143 |
disposeExcept(index); |
| 144 |
values = null; |
| 145 |
downstream.onError(ex); |
| 146 |
} else { |
| 147 |
RxJavaPlugins.onError(ex); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
static final class ZipSingleObserver<T> |
| 153 |
extends AtomicReference<Disposable> |
| 154 |
implements SingleObserver<T> { |
| 155 |
|
| 156 |
private static final long serialVersionUID = 3323743579927613702L; |
| 157 |
|
| 158 |
final ZipCoordinator<T, ?> parent; |
| 159 |
|
| 160 |
final int index; |
| 161 |
|
| 162 |
ZipSingleObserver(ZipCoordinator<T, ?> parent, int index) { |
| 163 |
this.parent = parent; |
| 164 |
this.index = index; |
| 165 |
} |
| 166 |
|
| 167 |
public void dispose() { |
| 168 |
DisposableHelper.dispose(this); |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void onSubscribe(Disposable d) { |
| 173 |
DisposableHelper.setOnce(this, d); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void onSuccess(T value) { |
| 178 |
parent.innerSuccess(value, index); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void onError(Throwable e) { |
| 183 |
parent.innerError(e, index); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
final class SingletonArrayFunc implements Function<T, R> { |
| 188 |
@Override |
| 189 |
public R apply(T t) throws Throwable { |
| 190 |
return Objects.requireNonNull(zipper.apply(new Object[] { t }), "The zipper returned a null value"); |
| 191 |
} |
| 192 |
} |
| 193 |
} |