| 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 <C> the collection type |
| 32 |
*/ |
| 33 |
public final class ParallelCollect<T, C> extends ParallelFlowable<C> { |
| 34 |
|
| 35 |
final ParallelFlowable<? extends T> source; |
| 36 |
|
| 37 |
final Supplier<? extends C> initialCollection; |
| 38 |
|
| 39 |
final BiConsumer<? super C, ? super T> collector; |
| 40 |
|
| 41 |
public ParallelCollect(ParallelFlowable<? extends T> source, |
| 42 |
Supplier<? extends C> initialCollection, BiConsumer<? super C, ? super T> collector) { |
| 43 |
this.source = source; |
| 44 |
this.initialCollection = initialCollection; |
| 45 |
this.collector = collector; |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public void subscribe(Subscriber<? super C>[] subscribers) { |
| 50 |
subscribers = RxJavaPlugins.onSubscribe(this, subscribers); |
| 51 |
|
| 52 |
if (!validate(subscribers)) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
int n = subscribers.length; |
| 57 |
@SuppressWarnings("unchecked") |
| 58 |
Subscriber<T>[] parents = new Subscriber[n]; |
| 59 |
|
| 60 |
for (int i = 0; i < n; i++) { |
| 61 |
|
| 62 |
C initialValue; |
| 63 |
|
| 64 |
try { |
| 65 |
initialValue = Objects.requireNonNull(initialCollection.get(), "The initialSupplier returned a null value"); |
| 66 |
} catch (Throwable ex) { |
| 67 |
Exceptions.throwIfFatal(ex); |
| 68 |
reportError(subscribers, ex); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
parents[i] = new ParallelCollectSubscriber<>(subscribers[i], initialValue, collector); |
| 73 |
} |
| 74 |
|
| 75 |
source.subscribe(parents); |
| 76 |
} |
| 77 |
|
| 78 |
void reportError(Subscriber<?>[] subscribers, Throwable ex) { |
| 79 |
for (Subscriber<?> s : subscribers) { |
| 80 |
EmptySubscription.error(ex, s); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public int parallelism() { |
| 86 |
return source.parallelism(); |
| 87 |
} |
| 88 |
|
| 89 |
static final class ParallelCollectSubscriber<T, C> extends DeferredScalarSubscriber<T, C> { |
| 90 |
|
| 91 |
private static final long serialVersionUID = -4767392946044436228L; |
| 92 |
|
| 93 |
final BiConsumer<? super C, ? super T> collector; |
| 94 |
|
| 95 |
C collection; |
| 96 |
|
| 97 |
boolean done; |
| 98 |
|
| 99 |
ParallelCollectSubscriber(Subscriber<? super C> subscriber, |
| 100 |
C initialValue, BiConsumer<? super C, ? super T> collector) { |
| 101 |
super(subscriber); |
| 102 |
this.collection = initialValue; |
| 103 |
this.collector = collector; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onSubscribe(Subscription s) { |
| 108 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 109 |
this.upstream = s; |
| 110 |
|
| 111 |
downstream.onSubscribe(this); |
| 112 |
|
| 113 |
s.request(Long.MAX_VALUE); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void onNext(T t) { |
| 119 |
if (done) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
try { |
| 124 |
collector.accept(collection, t); |
| 125 |
} catch (Throwable ex) { |
| 126 |
Exceptions.throwIfFatal(ex); |
| 127 |
cancel(); |
| 128 |
onError(ex); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void onError(Throwable t) { |
| 134 |
if (done) { |
| 135 |
RxJavaPlugins.onError(t); |
| 136 |
return; |
| 137 |
} |
| 138 |
done = true; |
| 139 |
collection = null; |
| 140 |
downstream.onError(t); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public void onComplete() { |
| 145 |
if (done) { |
| 146 |
return; |
| 147 |
} |
| 148 |
done = true; |
| 149 |
C c = collection; |
| 150 |
collection = null; |
| 151 |
complete(c); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void cancel() { |
| 156 |
super.cancel(); |
| 157 |
upstream.cancel(); |
| 158 |
} |
| 159 |
} |
| 160 |
} |