| 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.observable; |
| 15 |
|
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.Objects; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 22 |
import io.reactivex.rxjava3.functions.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 25 |
|
| 26 |
/** |
| 27 |
* Maps a sequence into an Iterable and emits its values. |
| 28 |
* |
| 29 |
* @param <T> the input value type to map to Iterable |
| 30 |
* @param <R> the element type of the Iterable and the output |
| 31 |
*/ |
| 32 |
public final class ObservableFlattenIterable<T, R> extends AbstractObservableWithUpstream<T, R> { |
| 33 |
|
| 34 |
final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 35 |
|
| 36 |
public ObservableFlattenIterable(ObservableSource<T> source, |
| 37 |
Function<? super T, ? extends Iterable<? extends R>> mapper) { |
| 38 |
super(source); |
| 39 |
this.mapper = mapper; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void subscribeActual(Observer<? super R> observer) { |
| 44 |
source.subscribe(new FlattenIterableObserver<>(observer, mapper)); |
| 45 |
} |
| 46 |
|
| 47 |
static final class FlattenIterableObserver<T, R> implements Observer<T>, Disposable { |
| 48 |
final Observer<? super R> downstream; |
| 49 |
|
| 50 |
final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 51 |
|
| 52 |
Disposable upstream; |
| 53 |
|
| 54 |
FlattenIterableObserver(Observer<? super R> actual, Function<? super T, ? extends Iterable<? extends R>> mapper) { |
| 55 |
this.downstream = actual; |
| 56 |
this.mapper = mapper; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public void onSubscribe(Disposable d) { |
| 61 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 62 |
this.upstream = d; |
| 63 |
|
| 64 |
downstream.onSubscribe(this); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public void onNext(T value) { |
| 70 |
if (upstream == DisposableHelper.DISPOSED) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
Iterator<? extends R> it; |
| 75 |
|
| 76 |
try { |
| 77 |
it = mapper.apply(value).iterator(); |
| 78 |
} catch (Throwable ex) { |
| 79 |
Exceptions.throwIfFatal(ex); |
| 80 |
upstream.dispose(); |
| 81 |
onError(ex); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
Observer<? super R> a = downstream; |
| 86 |
|
| 87 |
for (;;) { |
| 88 |
boolean b; |
| 89 |
|
| 90 |
try { |
| 91 |
b = it.hasNext(); |
| 92 |
} catch (Throwable ex) { |
| 93 |
Exceptions.throwIfFatal(ex); |
| 94 |
upstream.dispose(); |
| 95 |
onError(ex); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
if (b) { |
| 100 |
R v; |
| 101 |
|
| 102 |
try { |
| 103 |
v = Objects.requireNonNull(it.next(), "The iterator returned a null value"); |
| 104 |
} catch (Throwable ex) { |
| 105 |
Exceptions.throwIfFatal(ex); |
| 106 |
upstream.dispose(); |
| 107 |
onError(ex); |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
a.onNext(v); |
| 112 |
} else { |
| 113 |
break; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public void onError(Throwable e) { |
| 120 |
if (upstream == DisposableHelper.DISPOSED) { |
| 121 |
RxJavaPlugins.onError(e); |
| 122 |
return; |
| 123 |
} |
| 124 |
upstream = DisposableHelper.DISPOSED; |
| 125 |
downstream.onError(e); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public void onComplete() { |
| 130 |
if (upstream == DisposableHelper.DISPOSED) { |
| 131 |
return; |
| 132 |
} |
| 133 |
upstream = DisposableHelper.DISPOSED; |
| 134 |
downstream.onComplete(); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public boolean isDisposed() { |
| 139 |
return upstream.isDisposed(); |
| 140 |
} |
| 141 |
|
| 142 |
@Override |
| 143 |
public void dispose() { |
| 144 |
upstream.dispose(); |
| 145 |
upstream = DisposableHelper.DISPOSED; |
| 146 |
} |
| 147 |
} |
| 148 |
} |