| 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.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 MaybeZipArray<T, R> extends Maybe<R> { |
| 27 |
|
| 28 |
final MaybeSource<? extends T>[] sources; |
| 29 |
|
| 30 |
final Function<? super Object[], ? extends R> zipper; |
| 31 |
|
| 32 |
public MaybeZipArray(MaybeSource<? 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(MaybeObserver<? super R> observer) { |
| 39 |
MaybeSource<? extends T>[] sources = this.sources; |
| 40 |
int n = sources.length; |
| 41 |
|
| 42 |
if (n == 1) { |
| 43 |
sources[0].subscribe(new MaybeMap.MapMaybeObserver<>(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 |
MaybeSource<? 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 |
source.subscribe(parent.observers[i]); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static final class ZipCoordinator<T, R> extends AtomicInteger implements Disposable { |
| 67 |
|
| 68 |
private static final long serialVersionUID = -5556924161382950569L; |
| 69 |
|
| 70 |
final MaybeObserver<? super R> downstream; |
| 71 |
|
| 72 |
final Function<? super Object[], ? extends R> zipper; |
| 73 |
|
| 74 |
final ZipMaybeObserver<T>[] observers; |
| 75 |
|
| 76 |
Object[] values; |
| 77 |
|
| 78 |
@SuppressWarnings("unchecked") |
| 79 |
ZipCoordinator(MaybeObserver<? super R> observer, int n, Function<? super Object[], ? extends R> zipper) { |
| 80 |
super(n); |
| 81 |
this.downstream = observer; |
| 82 |
this.zipper = zipper; |
| 83 |
ZipMaybeObserver<T>[] o = new ZipMaybeObserver[n]; |
| 84 |
for (int i = 0; i < n; i++) { |
| 85 |
o[i] = new ZipMaybeObserver<>(this, i); |
| 86 |
} |
| 87 |
this.observers = o; |
| 88 |
this.values = new Object[n]; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public boolean isDisposed() { |
| 93 |
return get() <= 0; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public void dispose() { |
| 98 |
if (getAndSet(0) > 0) { |
| 99 |
for (ZipMaybeObserver<?> d : observers) { |
| 100 |
d.dispose(); |
| 101 |
} |
| 102 |
|
| 103 |
values = null; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void innerSuccess(T value, int index) { |
| 108 |
Object[] values = this.values; |
| 109 |
if (values != null) { |
| 110 |
values[index] = value; |
| 111 |
} |
| 112 |
if (decrementAndGet() == 0) { |
| 113 |
R v; |
| 114 |
|
| 115 |
try { |
| 116 |
v = Objects.requireNonNull(zipper.apply(values), "The zipper returned a null value"); |
| 117 |
} catch (Throwable ex) { |
| 118 |
Exceptions.throwIfFatal(ex); |
| 119 |
this.values = null; |
| 120 |
downstream.onError(ex); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
this.values = null; |
| 125 |
downstream.onSuccess(v); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
void disposeExcept(int index) { |
| 130 |
ZipMaybeObserver<T>[] observers = this.observers; |
| 131 |
int n = observers.length; |
| 132 |
for (int i = 0; i < index; i++) { |
| 133 |
observers[i].dispose(); |
| 134 |
} |
| 135 |
for (int i = index + 1; i < n; i++) { |
| 136 |
observers[i].dispose(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void innerError(Throwable ex, int index) { |
| 141 |
if (getAndSet(0) > 0) { |
| 142 |
disposeExcept(index); |
| 143 |
values = null; |
| 144 |
downstream.onError(ex); |
| 145 |
} else { |
| 146 |
RxJavaPlugins.onError(ex); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
void innerComplete(int index) { |
| 151 |
if (getAndSet(0) > 0) { |
| 152 |
disposeExcept(index); |
| 153 |
values = null; |
| 154 |
downstream.onComplete(); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
static final class ZipMaybeObserver<T> |
| 160 |
extends AtomicReference<Disposable> |
| 161 |
implements MaybeObserver<T> { |
| 162 |
|
| 163 |
private static final long serialVersionUID = 3323743579927613702L; |
| 164 |
|
| 165 |
final ZipCoordinator<T, ?> parent; |
| 166 |
|
| 167 |
final int index; |
| 168 |
|
| 169 |
ZipMaybeObserver(ZipCoordinator<T, ?> parent, int index) { |
| 170 |
this.parent = parent; |
| 171 |
this.index = index; |
| 172 |
} |
| 173 |
|
| 174 |
public void dispose() { |
| 175 |
DisposableHelper.dispose(this); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onSubscribe(Disposable d) { |
| 180 |
DisposableHelper.setOnce(this, d); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void onSuccess(T value) { |
| 185 |
parent.innerSuccess(value, index); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public void onError(Throwable e) { |
| 190 |
parent.innerError(e, index); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public void onComplete() { |
| 195 |
parent.innerComplete(index); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
final class SingletonArrayFunc implements Function<T, R> { |
| 200 |
@Override |
| 201 |
public R apply(T t) throws Throwable { |
| 202 |
return Objects.requireNonNull(zipper.apply(new Object[] { t }), "The zipper returned a null value"); |
| 203 |
} |
| 204 |
} |
| 205 |
} |