| 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.jdk8; |
| 15 |
|
| 16 |
import java.util.*; |
| 17 |
|
| 18 |
import org.reactivestreams.*; |
| 19 |
|
| 20 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 21 |
import io.reactivex.rxjava3.functions.Function; |
| 22 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 23 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 24 |
import io.reactivex.rxjava3.parallel.ParallelFlowable; |
| 25 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 26 |
|
| 27 |
/** |
| 28 |
* Maps each 'rail' of the source ParallelFlowable with a mapper function. |
| 29 |
* |
| 30 |
* @param <T> the input value type |
| 31 |
* @param <R> the output value type |
| 32 |
* @since 3.0.0 |
| 33 |
*/ |
| 34 |
public final class ParallelMapOptional<T, R> extends ParallelFlowable<R> { |
| 35 |
|
| 36 |
final ParallelFlowable<T> source; |
| 37 |
|
| 38 |
final Function<? super T, Optional<? extends R>> mapper; |
| 39 |
|
| 40 |
public ParallelMapOptional(ParallelFlowable<T> source, Function<? super T, Optional<? extends R>> mapper) { |
| 41 |
this.source = source; |
| 42 |
this.mapper = mapper; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void subscribe(Subscriber<? super R>[] subscribers) { |
| 47 |
if (!validate(subscribers)) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
int n = subscribers.length; |
| 52 |
@SuppressWarnings("unchecked") |
| 53 |
Subscriber<? super T>[] parents = new Subscriber[n]; |
| 54 |
|
| 55 |
for (int i = 0; i < n; i++) { |
| 56 |
Subscriber<? super R> a = subscribers[i]; |
| 57 |
if (a instanceof ConditionalSubscriber) { |
| 58 |
parents[i] = new ParallelMapConditionalSubscriber<>((ConditionalSubscriber<? super R>)a, mapper); |
| 59 |
} else { |
| 60 |
parents[i] = new ParallelMapSubscriber<>(a, mapper); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
source.subscribe(parents); |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public int parallelism() { |
| 69 |
return source.parallelism(); |
| 70 |
} |
| 71 |
|
| 72 |
static final class ParallelMapSubscriber<T, R> implements ConditionalSubscriber<T>, Subscription { |
| 73 |
|
| 74 |
final Subscriber<? super R> downstream; |
| 75 |
|
| 76 |
final Function<? super T, Optional<? extends R>> mapper; |
| 77 |
|
| 78 |
Subscription upstream; |
| 79 |
|
| 80 |
boolean done; |
| 81 |
|
| 82 |
ParallelMapSubscriber(Subscriber<? super R> actual, Function<? super T, Optional<? extends R>> mapper) { |
| 83 |
this.downstream = actual; |
| 84 |
this.mapper = mapper; |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public void request(long n) { |
| 89 |
upstream.request(n); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void cancel() { |
| 94 |
upstream.cancel(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onSubscribe(Subscription s) { |
| 99 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 100 |
this.upstream = s; |
| 101 |
|
| 102 |
downstream.onSubscribe(this); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onNext(T t) { |
| 108 |
if (!tryOnNext(t)) { |
| 109 |
upstream.request(1); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public boolean tryOnNext(T t) { |
| 115 |
if (done) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
Optional<? extends R> v; |
| 119 |
|
| 120 |
try { |
| 121 |
v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null Optional"); |
| 122 |
} catch (Throwable ex) { |
| 123 |
Exceptions.throwIfFatal(ex); |
| 124 |
cancel(); |
| 125 |
onError(ex); |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
if (v.isPresent()) { |
| 130 |
downstream.onNext(v.get()); |
| 131 |
return true; |
| 132 |
} |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public void onError(Throwable t) { |
| 138 |
if (done) { |
| 139 |
RxJavaPlugins.onError(t); |
| 140 |
return; |
| 141 |
} |
| 142 |
done = true; |
| 143 |
downstream.onError(t); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void onComplete() { |
| 148 |
if (done) { |
| 149 |
return; |
| 150 |
} |
| 151 |
done = true; |
| 152 |
downstream.onComplete(); |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
static final class ParallelMapConditionalSubscriber<T, R> implements ConditionalSubscriber<T>, Subscription { |
| 157 |
|
| 158 |
final ConditionalSubscriber<? super R> downstream; |
| 159 |
|
| 160 |
final Function<? super T, Optional<? extends R>> mapper; |
| 161 |
|
| 162 |
Subscription upstream; |
| 163 |
|
| 164 |
boolean done; |
| 165 |
|
| 166 |
ParallelMapConditionalSubscriber(ConditionalSubscriber<? super R> actual, Function<? super T, Optional<? extends R>> mapper) { |
| 167 |
this.downstream = actual; |
| 168 |
this.mapper = mapper; |
| 169 |
} |
| 170 |
|
| 171 |
@Override |
| 172 |
public void request(long n) { |
| 173 |
upstream.request(n); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void cancel() { |
| 178 |
upstream.cancel(); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public void onSubscribe(Subscription s) { |
| 183 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 184 |
this.upstream = s; |
| 185 |
|
| 186 |
downstream.onSubscribe(this); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public void onNext(T t) { |
| 192 |
if (!tryOnNext(t)) { |
| 193 |
upstream.request(1); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
@Override |
| 198 |
public boolean tryOnNext(T t) { |
| 199 |
if (done) { |
| 200 |
return false; |
| 201 |
} |
| 202 |
Optional<? extends R> v; |
| 203 |
|
| 204 |
try { |
| 205 |
v = Objects.requireNonNull(mapper.apply(t), "The mapper returned a null value"); |
| 206 |
} catch (Throwable ex) { |
| 207 |
Exceptions.throwIfFatal(ex); |
| 208 |
cancel(); |
| 209 |
onError(ex); |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
return v.isPresent() && downstream.tryOnNext(v.get()); |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public void onError(Throwable t) { |
| 218 |
if (done) { |
| 219 |
RxJavaPlugins.onError(t); |
| 220 |
return; |
| 221 |
} |
| 222 |
done = true; |
| 223 |
downstream.onError(t); |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public void onComplete() { |
| 228 |
if (done) { |
| 229 |
return; |
| 230 |
} |
| 231 |
done = true; |
| 232 |
downstream.onComplete(); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
} |