|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 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.internal.operators.observable; |
| 15 | + |
| 16 | +import java.util.Iterator; |
| 17 | + |
| 18 | +import io.reactivex.*; |
| 19 | +import io.reactivex.disposables.Disposable; |
| 20 | +import io.reactivex.exceptions.Exceptions; |
| 21 | +import io.reactivex.functions.Function; |
| 22 | +import io.reactivex.internal.disposables.DisposableHelper; |
| 23 | +import io.reactivex.internal.functions.ObjectHelper; |
| 24 | +import io.reactivex.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<T, R>(observer, mapper)); |
| 45 | + } |
| 46 | + |
| 47 | + static final class FlattenIterableObserver<T, R> implements Observer<T>, Disposable { |
| 48 | + final Observer<? super R> actual; |
| 49 | + |
| 50 | + final Function<? super T, ? extends Iterable<? extends R>> mapper; |
| 51 | + |
| 52 | + Disposable d; |
| 53 | + |
| 54 | + FlattenIterableObserver(Observer<? super R> actual, Function<? super T, ? extends Iterable<? extends R>> mapper) { |
| 55 | + this.actual = actual; |
| 56 | + this.mapper = mapper; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onSubscribe(Disposable d) { |
| 61 | + if (DisposableHelper.validate(this.d, d)) { |
| 62 | + this.d = d; |
| 63 | + |
| 64 | + actual.onSubscribe(this); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void onNext(T value) { |
| 70 | + if (d == 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 | + d.dispose(); |
| 81 | + onError(ex); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + Observer<? super R> a = actual; |
| 86 | + |
| 87 | + for (;;) { |
| 88 | + boolean b; |
| 89 | + |
| 90 | + try { |
| 91 | + b = it.hasNext(); |
| 92 | + } catch (Throwable ex) { |
| 93 | + Exceptions.throwIfFatal(ex); |
| 94 | + d.dispose(); |
| 95 | + onError(ex); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (b) { |
| 100 | + R v; |
| 101 | + |
| 102 | + try { |
| 103 | + v = ObjectHelper.requireNonNull(it.next(), "The iterator returned a null value"); |
| 104 | + } catch (Throwable ex) { |
| 105 | + Exceptions.throwIfFatal(ex); |
| 106 | + d.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 (d == DisposableHelper.DISPOSED) { |
| 121 | + RxJavaPlugins.onError(e); |
| 122 | + return; |
| 123 | + } |
| 124 | + actual.onError(e); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onComplete() { |
| 129 | + if (d == DisposableHelper.DISPOSED) { |
| 130 | + return; |
| 131 | + } |
| 132 | + actual.onComplete(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public boolean isDisposed() { |
| 137 | + return d.isDisposed(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void dispose() { |
| 142 | + d.dispose(); |
| 143 | + d = DisposableHelper.DISPOSED; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments