|
| 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.internal.operators.single; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.AtomicLong; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
| 18 | + |
| 19 | +import org.reactivestreams.Publisher; |
| 20 | +import org.reactivestreams.Subscriber; |
| 21 | +import org.reactivestreams.Subscription; |
| 22 | + |
| 23 | +import io.reactivex.Flowable; |
| 24 | +import io.reactivex.FlowableSubscriber; |
| 25 | +import io.reactivex.Scheduler; |
| 26 | +import io.reactivex.SingleObserver; |
| 27 | +import io.reactivex.SingleSource; |
| 28 | +import io.reactivex.disposables.Disposable; |
| 29 | +import io.reactivex.exceptions.Exceptions; |
| 30 | +import io.reactivex.functions.Function; |
| 31 | +import io.reactivex.internal.functions.ObjectHelper; |
| 32 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 33 | + |
| 34 | +/** |
| 35 | + * A Flowable that emits items based on applying a specified function to the item emitted by the |
| 36 | + * source Single, where that function returns a Publisher. |
| 37 | + * <p> |
| 38 | + * <img width="640" height="305" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.flatMapPublisher.png" alt=""> |
| 39 | + * <dl> |
| 40 | + * <dt><b>Backpressure:</b></dt> |
| 41 | + * <dd>The returned {@code Flowable} honors the backpressure of the downstream consumer |
| 42 | + * and the {@code Publisher} returned by the mapper function is expected to honor it as well.</dd> |
| 43 | + * <dt><b>Scheduler:</b></dt> |
| 44 | + * <dd>{@code flatMapPublisher} does not operate by default on a particular {@link Scheduler}.</dd> |
| 45 | + * </dl> |
| 46 | + * |
| 47 | + * @param <T> the source value type |
| 48 | + * @param <R> the result value type |
| 49 | + * |
| 50 | + * @see <a href="http://reactivex.io/documentation/operators/flatmap.html">ReactiveX operators documentation: FlatMap</a> |
| 51 | + * @since 2.1.15 |
| 52 | + */ |
| 53 | +public final class SingleFlatMapPublisher<T, R> extends Flowable<R> { |
| 54 | + |
| 55 | + final SingleSource<T> source; |
| 56 | + final Function<? super T, ? extends Publisher<? extends R>> mapper; |
| 57 | + |
| 58 | + public SingleFlatMapPublisher(SingleSource<T> source, |
| 59 | + Function<? super T, ? extends Publisher<? extends R>> mapper) { |
| 60 | + this.source = source; |
| 61 | + this.mapper = mapper; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void subscribeActual(Subscriber<? super R> actual) { |
| 66 | + source.subscribe(new SingleFlatMapPublisherObserver<T, R>(actual, mapper)); |
| 67 | + } |
| 68 | + |
| 69 | + static final class SingleFlatMapPublisherObserver<S, T> extends AtomicLong |
| 70 | + implements SingleObserver<S>, FlowableSubscriber<T>, Subscription { |
| 71 | + |
| 72 | + private static final long serialVersionUID = 7759721921468635667L; |
| 73 | + |
| 74 | + final Subscriber<? super T> actual; |
| 75 | + final Function<? super S, ? extends Publisher<? extends T>> mapper; |
| 76 | + final AtomicReference<Subscription> parent; |
| 77 | + Disposable disposable; |
| 78 | + |
| 79 | + SingleFlatMapPublisherObserver(Subscriber<? super T> actual, |
| 80 | + Function<? super S, ? extends Publisher<? extends T>> mapper) { |
| 81 | + this.actual = actual; |
| 82 | + this.mapper = mapper; |
| 83 | + this.parent = new AtomicReference<Subscription>(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onSubscribe(Disposable d) { |
| 88 | + this.disposable = d; |
| 89 | + actual.onSubscribe(this); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void onSuccess(S value) { |
| 94 | + Publisher<? extends T> f; |
| 95 | + try { |
| 96 | + f = ObjectHelper.requireNonNull(mapper.apply(value), "the mapper returned a null Publisher"); |
| 97 | + } catch (Throwable e) { |
| 98 | + Exceptions.throwIfFatal(e); |
| 99 | + actual.onError(e); |
| 100 | + return; |
| 101 | + } |
| 102 | + f.subscribe(this); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onSubscribe(Subscription s) { |
| 107 | + SubscriptionHelper.deferredSetOnce(parent, this, s); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void onNext(T t) { |
| 112 | + actual.onNext(t); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onComplete() { |
| 117 | + actual.onComplete(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onError(Throwable e) { |
| 122 | + actual.onError(e); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void request(long n) { |
| 127 | + SubscriptionHelper.deferredRequest(parent, this, n); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void cancel() { |
| 132 | + disposable.dispose(); |
| 133 | + SubscriptionHelper.cancel(parent); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments