| 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.flowable; |
| 15 |
|
| 16 |
import java.util.Arrays; |
| 17 |
import java.util.Objects; |
| 18 |
import java.util.concurrent.atomic.*; |
| 19 |
|
| 20 |
import org.reactivestreams.*; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.annotations.*; |
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 25 |
import io.reactivex.rxjava3.functions.Function; |
| 26 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 27 |
import io.reactivex.rxjava3.internal.util.*; |
| 28 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 29 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 30 |
|
| 31 |
/** |
| 32 |
* Combines a main sequence of values with the latest from multiple other sequences via |
| 33 |
* a selector function. |
| 34 |
* |
| 35 |
* @param <T> the main sequence's type |
| 36 |
* @param <R> the output type |
| 37 |
*/ |
| 38 |
public final class FlowableWithLatestFromMany<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 39 |
@Nullable |
| 40 |
final Publisher<?>[] otherArray; |
| 41 |
|
| 42 |
@Nullable |
| 43 |
final Iterable<? extends Publisher<?>> otherIterable; |
| 44 |
|
| 45 |
final Function<? super Object[], R> combiner; |
| 46 |
|
| 47 |
public FlowableWithLatestFromMany(@NonNull Flowable<T> source, @NonNull Publisher<?>[] otherArray, Function<? super Object[], R> combiner) { |
| 48 |
super(source); |
| 49 |
this.otherArray = otherArray; |
| 50 |
this.otherIterable = null; |
| 51 |
this.combiner = combiner; |
| 52 |
} |
| 53 |
|
| 54 |
public FlowableWithLatestFromMany(@NonNull Flowable<T> source, @NonNull Iterable<? extends Publisher<?>> otherIterable, @NonNull Function<? super Object[], R> combiner) { |
| 55 |
super(source); |
| 56 |
this.otherArray = null; |
| 57 |
this.otherIterable = otherIterable; |
| 58 |
this.combiner = combiner; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 63 |
Publisher<?>[] others = otherArray; |
| 64 |
int n = 0; |
| 65 |
if (others == null) { |
| 66 |
others = new Publisher[8]; |
| 67 |
|
| 68 |
try { |
| 69 |
for (Publisher<?> p : otherIterable) { |
| 70 |
if (n == others.length) { |
| 71 |
others = Arrays.copyOf(others, n + (n >> 1)); |
| 72 |
} |
| 73 |
others[n++] = p; |
| 74 |
} |
| 75 |
} catch (Throwable ex) { |
| 76 |
Exceptions.throwIfFatal(ex); |
| 77 |
EmptySubscription.error(ex, s); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
} else { |
| 82 |
n = others.length; |
| 83 |
} |
| 84 |
|
| 85 |
if (n == 0) { |
| 86 |
new FlowableMap<>(source, new SingletonArrayFunc()).subscribeActual(s); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
WithLatestFromSubscriber<T, R> parent = new WithLatestFromSubscriber<>(s, combiner, n); |
| 91 |
s.onSubscribe(parent); |
| 92 |
parent.subscribe(others, n); |
| 93 |
|
| 94 |
source.subscribe(parent); |
| 95 |
} |
| 96 |
|
| 97 |
static final class WithLatestFromSubscriber<T, R> |
| 98 |
extends AtomicInteger |
| 99 |
implements ConditionalSubscriber<T>, Subscription { |
| 100 |
|
| 101 |
private static final long serialVersionUID = 1577321883966341961L; |
| 102 |
|
| 103 |
final Subscriber<? super R> downstream; |
| 104 |
|
| 105 |
final Function<? super Object[], R> combiner; |
| 106 |
|
| 107 |
final WithLatestInnerSubscriber[] subscribers; |
| 108 |
|
| 109 |
final AtomicReferenceArray<Object> values; |
| 110 |
|
| 111 |
final AtomicReference<Subscription> upstream; |
| 112 |
|
| 113 |
final AtomicLong requested; |
| 114 |
|
| 115 |
final AtomicThrowable error; |
| 116 |
|
| 117 |
volatile boolean done; |
| 118 |
|
| 119 |
WithLatestFromSubscriber(Subscriber<? super R> actual, Function<? super Object[], R> combiner, int n) { |
| 120 |
this.downstream = actual; |
| 121 |
this.combiner = combiner; |
| 122 |
WithLatestInnerSubscriber[] s = new WithLatestInnerSubscriber[n]; |
| 123 |
for (int i = 0; i < n; i++) { |
| 124 |
s[i] = new WithLatestInnerSubscriber(this, i); |
| 125 |
} |
| 126 |
this.subscribers = s; |
| 127 |
this.values = new AtomicReferenceArray<>(n); |
| 128 |
this.upstream = new AtomicReference<>(); |
| 129 |
this.requested = new AtomicLong(); |
| 130 |
this.error = new AtomicThrowable(); |
| 131 |
} |
| 132 |
|
| 133 |
void subscribe(Publisher<?>[] others, int n) { |
| 134 |
WithLatestInnerSubscriber[] subscribers = this.subscribers; |
| 135 |
AtomicReference<Subscription> upstream = this.upstream; |
| 136 |
for (int i = 0; i < n; i++) { |
| 137 |
if (upstream.get() == SubscriptionHelper.CANCELLED) { |
| 138 |
return; |
| 139 |
} |
| 140 |
others[i].subscribe(subscribers[i]); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onSubscribe(Subscription s) { |
| 146 |
SubscriptionHelper.deferredSetOnce(this.upstream, requested, s); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onNext(T t) { |
| 151 |
if (!tryOnNext(t) && !done) { |
| 152 |
upstream.get().request(1); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public boolean tryOnNext(T t) { |
| 158 |
if (done) { |
| 159 |
return false; |
| 160 |
} |
| 161 |
AtomicReferenceArray<Object> ara = values; |
| 162 |
int n = ara.length(); |
| 163 |
Object[] objects = new Object[n + 1]; |
| 164 |
objects[0] = t; |
| 165 |
|
| 166 |
for (int i = 0; i < n; i++) { |
| 167 |
Object o = ara.get(i); |
| 168 |
if (o == null) { |
| 169 |
// somebody hasn't signalled yet, skip this T |
| 170 |
return false; |
| 171 |
} |
| 172 |
objects[i + 1] = o; |
| 173 |
} |
| 174 |
|
| 175 |
R v; |
| 176 |
|
| 177 |
try { |
| 178 |
v = Objects.requireNonNull(combiner.apply(objects), "The combiner returned a null value"); |
| 179 |
} catch (Throwable ex) { |
| 180 |
Exceptions.throwIfFatal(ex); |
| 181 |
cancel(); |
| 182 |
onError(ex); |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
HalfSerializer.onNext(downstream, v, this, error); |
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void onError(Throwable t) { |
| 192 |
if (done) { |
| 193 |
RxJavaPlugins.onError(t); |
| 194 |
return; |
| 195 |
} |
| 196 |
done = true; |
| 197 |
cancelAllBut(-1); |
| 198 |
HalfSerializer.onError(downstream, t, this, error); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void onComplete() { |
| 203 |
if (!done) { |
| 204 |
done = true; |
| 205 |
cancelAllBut(-1); |
| 206 |
HalfSerializer.onComplete(downstream, this, error); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void request(long n) { |
| 212 |
SubscriptionHelper.deferredRequest(upstream, requested, n); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void cancel() { |
| 217 |
SubscriptionHelper.cancel(upstream); |
| 218 |
for (WithLatestInnerSubscriber s : subscribers) { |
| 219 |
s.dispose(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
void innerNext(int index, Object o) { |
| 224 |
values.set(index, o); |
| 225 |
} |
| 226 |
|
| 227 |
void innerError(int index, Throwable t) { |
| 228 |
done = true; |
| 229 |
SubscriptionHelper.cancel(upstream); |
| 230 |
cancelAllBut(index); |
| 231 |
HalfSerializer.onError(downstream, t, this, error); |
| 232 |
} |
| 233 |
|
| 234 |
void innerComplete(int index, boolean nonEmpty) { |
| 235 |
if (!nonEmpty) { |
| 236 |
done = true; |
| 237 |
SubscriptionHelper.cancel(upstream); |
| 238 |
cancelAllBut(index); |
| 239 |
HalfSerializer.onComplete(downstream, this, error); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
void cancelAllBut(int index) { |
| 244 |
WithLatestInnerSubscriber[] subscribers = this.subscribers; |
| 245 |
for (int i = 0; i < subscribers.length; i++) { |
| 246 |
if (i != index) { |
| 247 |
subscribers[i].dispose(); |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static final class WithLatestInnerSubscriber |
| 254 |
extends AtomicReference<Subscription> |
| 255 |
implements FlowableSubscriber<Object> { |
| 256 |
|
| 257 |
private static final long serialVersionUID = 3256684027868224024L; |
| 258 |
|
| 259 |
final WithLatestFromSubscriber<?, ?> parent; |
| 260 |
|
| 261 |
final int index; |
| 262 |
|
| 263 |
boolean hasValue; |
| 264 |
|
| 265 |
WithLatestInnerSubscriber(WithLatestFromSubscriber<?, ?> parent, int index) { |
| 266 |
this.parent = parent; |
| 267 |
this.index = index; |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public void onSubscribe(Subscription s) { |
| 272 |
SubscriptionHelper.setOnce(this, s, Long.MAX_VALUE); |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public void onNext(Object t) { |
| 277 |
if (!hasValue) { |
| 278 |
hasValue = true; |
| 279 |
} |
| 280 |
parent.innerNext(index, t); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public void onError(Throwable t) { |
| 285 |
parent.innerError(index, t); |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public void onComplete() { |
| 290 |
parent.innerComplete(index, hasValue); |
| 291 |
} |
| 292 |
|
| 293 |
void dispose() { |
| 294 |
SubscriptionHelper.cancel(this); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
final class SingletonArrayFunc implements Function<T, R> { |
| 299 |
@Override |
| 300 |
public R apply(T t) throws Throwable { |
| 301 |
return Objects.requireNonNull(combiner.apply(new Object[] { t }), "The combiner returned a null value"); |
| 302 |
} |
| 303 |
} |
| 304 |
} |