|
| 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.internal.operators.single; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.*; |
| 17 | + |
| 18 | +import io.reactivex.*; |
| 19 | +import io.reactivex.disposables.Disposable; |
| 20 | +import io.reactivex.exceptions.Exceptions; |
| 21 | +import io.reactivex.functions.Function; |
| 22 | +import io.reactivex.internal.disposables.DisposableHelper; |
| 23 | +import io.reactivex.internal.functions.ObjectHelper; |
| 24 | +import io.reactivex.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 | + |
| 43 | + if (n == 1) { |
| 44 | + sources[0].subscribe(new SingleMap.MapSingleObserver<T, R>(observer, new Function<T, R>() { |
| 45 | + @Override |
| 46 | + public R apply(T t) throws Exception { |
| 47 | + return zipper.apply(new Object[] { t }); |
| 48 | + } |
| 49 | + })); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + ZipCoordinator<T, R> parent = new ZipCoordinator<T, R>(observer, n, zipper); |
| 54 | + |
| 55 | + observer.onSubscribe(parent); |
| 56 | + |
| 57 | + for (int i = 0; i < n; i++) { |
| 58 | + if (parent.isDisposed()) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + SingleSource<? extends T> source = sources[i]; |
| 63 | + |
| 64 | + if (source == null) { |
| 65 | + parent.innerError(new NullPointerException("One of the sources is null"), i); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + source.subscribe(parent.observers[i]); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static final class ZipCoordinator<T, R> extends AtomicInteger implements Disposable { |
| 74 | + |
| 75 | + |
| 76 | + private static final long serialVersionUID = -5556924161382950569L; |
| 77 | + |
| 78 | + final SingleObserver<? super R> actual; |
| 79 | + |
| 80 | + final Function<? super Object[], ? extends R> zipper; |
| 81 | + |
| 82 | + final ZipSingleObserver<T>[] observers; |
| 83 | + |
| 84 | + final Object[] values; |
| 85 | + |
| 86 | + @SuppressWarnings("unchecked") |
| 87 | + ZipCoordinator(SingleObserver<? super R> observer, int n, Function<? super Object[], ? extends R> zipper) { |
| 88 | + super(n); |
| 89 | + this.actual = observer; |
| 90 | + this.zipper = zipper; |
| 91 | + ZipSingleObserver<T>[] o = new ZipSingleObserver[n]; |
| 92 | + for (int i = 0; i < n; i++) { |
| 93 | + o[i] = new ZipSingleObserver<T>(this, i); |
| 94 | + } |
| 95 | + this.observers = o; |
| 96 | + this.values = new Object[n]; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean isDisposed() { |
| 101 | + return get() <= 0; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void dispose() { |
| 106 | + if (getAndSet(0) > 0) { |
| 107 | + for (ZipSingleObserver<?> d : observers) { |
| 108 | + d.dispose(); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + void innerSuccess(T value, int index) { |
| 114 | + values[index] = value; |
| 115 | + if (decrementAndGet() == 0) { |
| 116 | + R v; |
| 117 | + |
| 118 | + try { |
| 119 | + v = ObjectHelper.requireNonNull(zipper.apply(values), "The zipper returned a null value"); |
| 120 | + } catch (Throwable ex) { |
| 121 | + Exceptions.throwIfFatal(ex); |
| 122 | + actual.onError(ex); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + actual.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 | + actual.onError(ex); |
| 145 | + } else { |
| 146 | + RxJavaPlugins.onError(ex); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + static final class ZipSingleObserver<T> |
| 152 | + extends AtomicReference<Disposable> |
| 153 | + implements SingleObserver<T> { |
| 154 | + |
| 155 | + private static final long serialVersionUID = 3323743579927613702L; |
| 156 | + |
| 157 | + final ZipCoordinator<T, ?> parent; |
| 158 | + |
| 159 | + final int index; |
| 160 | + |
| 161 | + ZipSingleObserver(ZipCoordinator<T, ?> parent, int index) { |
| 162 | + this.parent = parent; |
| 163 | + this.index = index; |
| 164 | + } |
| 165 | + |
| 166 | + public void dispose() { |
| 167 | + DisposableHelper.dispose(this); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void onSubscribe(Disposable d) { |
| 172 | + DisposableHelper.setOnce(this, d); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void onSuccess(T value) { |
| 177 | + parent.innerSuccess(value, index); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void onError(Throwable e) { |
| 182 | + parent.innerError(e, index); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments