| 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.parallel; |
| 15 |
|
| 16 |
import org.reactivestreams.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 19 |
import io.reactivex.rxjava3.functions.*; |
| 20 |
import io.reactivex.rxjava3.internal.subscribers.DeferredScalarSubscriber; |
| 21 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 22 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 23 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 24 |
|
| 25 |
import java.util.Objects; |
| 26 |
|
| 27 |
/** |
| 28 |
* Reduce the sequence of values in each 'rail' to a single value. |
| 29 |
* |
| 30 |
* @param <T> the input value type |
| 31 |
* @param <R> the result value type |
| 32 |
*/ |
| 33 |
public final class ParallelReduce<T, R> extends ParallelFlowable<R> { |
| 34 |
|
| 35 |
final ParallelFlowable<? extends T> source; |
| 36 |
|
| 37 |
final Supplier<R> initialSupplier; |
| 38 |
|
| 39 |
final BiFunction<R, ? super T, R> reducer; |
| 40 |
|
| 41 |
public ParallelReduce(ParallelFlowable<? extends T> source, Supplier<R> initialSupplier, BiFunction<R, ? super T, R> reducer) { |
| 42 |
this.source = source; |
| 43 |
this.initialSupplier = initialSupplier; |
| 44 |
this.reducer = reducer; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void subscribe(Subscriber<? super R>[] subscribers) { |
| 49 |
subscribers = RxJavaPlugins.onSubscribe(this, subscribers); |
| 50 |
|
| 51 |
if (!validate(subscribers)) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
int n = subscribers.length; |
| 56 |
@SuppressWarnings("unchecked") |
| 57 |
Subscriber<T>[] parents = new Subscriber[n]; |
| 58 |
|
| 59 |
for (int i = 0; i < n; i++) { |
| 60 |
|
| 61 |
R initialValue; |
| 62 |
|
| 63 |
try { |
| 64 |
initialValue = Objects.requireNonNull(initialSupplier.get(), "The initialSupplier returned a null value"); |
| 65 |
} catch (Throwable ex) { |
| 66 |
Exceptions.throwIfFatal(ex); |
| 67 |
reportError(subscribers, ex); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
parents[i] = new ParallelReduceSubscriber<>(subscribers[i], initialValue, reducer); |
| 72 |
} |
| 73 |
|
| 74 |
source.subscribe(parents); |
| 75 |
} |
| 76 |
|
| 77 |
void reportError(Subscriber<?>[] subscribers, Throwable ex) { |
| 78 |
for (Subscriber<?> s : subscribers) { |
| 79 |
EmptySubscription.error(ex, s); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public int parallelism() { |
| 85 |
return source.parallelism(); |
| 86 |
} |
| 87 |
|
| 88 |
static final class ParallelReduceSubscriber<T, R> extends DeferredScalarSubscriber<T, R> { |
| 89 |
|
| 90 |
private static final long serialVersionUID = 8200530050639449080L; |
| 91 |
|
| 92 |
final BiFunction<R, ? super T, R> reducer; |
| 93 |
|
| 94 |
R accumulator; |
| 95 |
|
| 96 |
boolean done; |
| 97 |
|
| 98 |
ParallelReduceSubscriber(Subscriber<? super R> subscriber, R initialValue, BiFunction<R, ? super T, R> reducer) { |
| 99 |
super(subscriber); |
| 100 |
this.accumulator = initialValue; |
| 101 |
this.reducer = reducer; |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
public void onSubscribe(Subscription s) { |
| 106 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 107 |
this.upstream = s; |
| 108 |
|
| 109 |
downstream.onSubscribe(this); |
| 110 |
|
| 111 |
s.request(Long.MAX_VALUE); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onNext(T t) { |
| 117 |
if (!done) { |
| 118 |
R v; |
| 119 |
|
| 120 |
try { |
| 121 |
v = Objects.requireNonNull(reducer.apply(accumulator, t), "The reducer returned a null value"); |
| 122 |
} catch (Throwable ex) { |
| 123 |
Exceptions.throwIfFatal(ex); |
| 124 |
cancel(); |
| 125 |
onError(ex); |
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
accumulator = v; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void onError(Throwable t) { |
| 135 |
if (done) { |
| 136 |
RxJavaPlugins.onError(t); |
| 137 |
return; |
| 138 |
} |
| 139 |
done = true; |
| 140 |
accumulator = null; |
| 141 |
downstream.onError(t); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void onComplete() { |
| 146 |
if (!done) { |
| 147 |
done = true; |
| 148 |
|
| 149 |
R a = accumulator; |
| 150 |
accumulator = null; |
| 151 |
complete(a); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void cancel() { |
| 157 |
super.cancel(); |
| 158 |
upstream.cancel(); |
| 159 |
} |
| 160 |
} |
| 161 |
} |