| 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.maybe; |
| 15 |
|
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.Objects; |
| 18 |
import java.util.concurrent.atomic.AtomicLong; |
| 19 |
|
| 20 |
import org.reactivestreams.Subscriber; |
| 21 |
|
| 22 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 23 |
import io.reactivex.rxjava3.core.*; |
| 24 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 25 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 26 |
import io.reactivex.rxjava3.functions.Function; |
| 27 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 28 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 29 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 30 |
|
| 31 |
/** |
| 32 |
* Maps a success value into an Iterable and streams it back as a Flowable. |
| 33 |
* |
| 34 |
* @param <T> the source value type |
| 35 |
* @param <R> the element type of the Iterable |
| 36 |
*/ |
| 37 |
public final class MaybeFlatMapIterableFlowable<T, R> extends Flowable<R> { |
| 38 |
|
| 39 |
final MaybeSource<T> source; |
| 40 |
|
| 41 |
final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 42 |
|
| 43 |
public MaybeFlatMapIterableFlowable(MaybeSource<T> source, |
| 44 |
Function<? super T, ? extends Iterable<? extends R>> mapper) { |
| 45 |
this.source = source; |
| 46 |
this.mapper = mapper; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 51 |
source.subscribe(new FlatMapIterableObserver<>(s, mapper)); |
| 52 |
} |
| 53 |
|
| 54 |
static final class FlatMapIterableObserver<T, R> |
| 55 |
extends BasicIntQueueSubscription<R> |
| 56 |
implements MaybeObserver<T> { |
| 57 |
|
| 58 |
private static final long serialVersionUID = -8938804753851907758L; |
| 59 |
|
| 60 |
final Subscriber<? super R> downstream; |
| 61 |
|
| 62 |
final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 63 |
|
| 64 |
final AtomicLong requested; |
| 65 |
|
| 66 |
Disposable upstream; |
| 67 |
|
| 68 |
volatile Iterator<? extends R> it; |
| 69 |
|
| 70 |
volatile boolean cancelled; |
| 71 |
|
| 72 |
boolean outputFused; |
| 73 |
|
| 74 |
FlatMapIterableObserver(Subscriber<? super R> actual, |
| 75 |
Function<? super T, ? extends Iterable<? extends R>> mapper) { |
| 76 |
this.downstream = actual; |
| 77 |
this.mapper = mapper; |
| 78 |
this.requested = new AtomicLong(); |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public void onSubscribe(Disposable d) { |
| 83 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 84 |
this.upstream = d; |
| 85 |
|
| 86 |
downstream.onSubscribe(this); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void onSuccess(T value) { |
| 92 |
Iterator<? extends R> iterator; |
| 93 |
boolean has; |
| 94 |
try { |
| 95 |
iterator = mapper.apply(value).iterator(); |
| 96 |
|
| 97 |
has = iterator.hasNext(); |
| 98 |
} catch (Throwable ex) { |
| 99 |
Exceptions.throwIfFatal(ex); |
| 100 |
downstream.onError(ex); |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
if (!has) { |
| 105 |
downstream.onComplete(); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
this.it = iterator; |
| 110 |
drain(); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void onError(Throwable e) { |
| 115 |
upstream = DisposableHelper.DISPOSED; |
| 116 |
downstream.onError(e); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void onComplete() { |
| 121 |
downstream.onComplete(); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void request(long n) { |
| 126 |
if (SubscriptionHelper.validate(n)) { |
| 127 |
BackpressureHelper.add(requested, n); |
| 128 |
drain(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public void cancel() { |
| 134 |
cancelled = true; |
| 135 |
upstream.dispose(); |
| 136 |
upstream = DisposableHelper.DISPOSED; |
| 137 |
} |
| 138 |
|
| 139 |
void fastPath(Subscriber<? super R> a, Iterator<? extends R> iterator) { |
| 140 |
for (;;) { |
| 141 |
if (cancelled) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
R v; |
| 146 |
|
| 147 |
try { |
| 148 |
v = iterator.next(); |
| 149 |
} catch (Throwable ex) { |
| 150 |
Exceptions.throwIfFatal(ex); |
| 151 |
a.onError(ex); |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
a.onNext(v); |
| 156 |
|
| 157 |
if (cancelled) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
boolean b; |
| 162 |
|
| 163 |
try { |
| 164 |
b = iterator.hasNext(); |
| 165 |
} catch (Throwable ex) { |
| 166 |
Exceptions.throwIfFatal(ex); |
| 167 |
a.onError(ex); |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
if (!b) { |
| 172 |
a.onComplete(); |
| 173 |
return; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
void drain() { |
| 179 |
if (getAndIncrement() != 0) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
Subscriber<? super R> a = downstream; |
| 184 |
Iterator<? extends R> iterator = this.it; |
| 185 |
|
| 186 |
if (outputFused && iterator != null) { |
| 187 |
a.onNext(null); |
| 188 |
a.onComplete(); |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
int missed = 1; |
| 193 |
|
| 194 |
for (;;) { |
| 195 |
|
| 196 |
if (iterator != null) { |
| 197 |
long r = requested.get(); |
| 198 |
|
| 199 |
if (r == Long.MAX_VALUE) { |
| 200 |
fastPath(a, iterator); |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
long e = 0L; |
| 205 |
|
| 206 |
while (e != r) { |
| 207 |
if (cancelled) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
R v; |
| 212 |
|
| 213 |
try { |
| 214 |
v = Objects.requireNonNull(iterator.next(), "The iterator returned a null value"); |
| 215 |
} catch (Throwable ex) { |
| 216 |
Exceptions.throwIfFatal(ex); |
| 217 |
a.onError(ex); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
a.onNext(v); |
| 222 |
|
| 223 |
if (cancelled) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
e++; |
| 228 |
|
| 229 |
boolean b; |
| 230 |
|
| 231 |
try { |
| 232 |
b = iterator.hasNext(); |
| 233 |
} catch (Throwable ex) { |
| 234 |
Exceptions.throwIfFatal(ex); |
| 235 |
a.onError(ex); |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
if (!b) { |
| 240 |
a.onComplete(); |
| 241 |
return; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
if (e != 0L) { |
| 246 |
BackpressureHelper.produced(requested, e); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
missed = addAndGet(-missed); |
| 251 |
if (missed == 0) { |
| 252 |
break; |
| 253 |
} |
| 254 |
|
| 255 |
if (iterator == null) { |
| 256 |
iterator = it; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public int requestFusion(int mode) { |
| 263 |
if ((mode & ASYNC) != 0) { |
| 264 |
outputFused = true; |
| 265 |
return ASYNC; |
| 266 |
} |
| 267 |
return NONE; |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
public void clear() { |
| 272 |
it = null; |
| 273 |
} |
| 274 |
|
| 275 |
@Override |
| 276 |
public boolean isEmpty() { |
| 277 |
return it == null; |
| 278 |
} |
| 279 |
|
| 280 |
@Nullable |
| 281 |
@Override |
| 282 |
public R poll() { |
| 283 |
Iterator<? extends R> iterator = it; |
| 284 |
|
| 285 |
if (iterator != null) { |
| 286 |
R v = Objects.requireNonNull(iterator.next(), "The iterator returned a null value"); |
| 287 |
if (!iterator.hasNext()) { |
| 288 |
it = null; |
| 289 |
} |
| 290 |
return v; |
| 291 |
} |
| 292 |
return null; |
| 293 |
} |
| 294 |
|
| 295 |
} |
| 296 |
} |