|
| 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.SingleObserver; |
| 25 | +import io.reactivex.SingleSource; |
| 26 | +import io.reactivex.disposables.Disposable; |
| 27 | +import io.reactivex.exceptions.Exceptions; |
| 28 | +import io.reactivex.functions.Function; |
| 29 | +import io.reactivex.internal.functions.ObjectHelper; |
| 30 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 31 | + |
| 32 | +public final class SingleFlatMapPublisher<S, T> extends Flowable<T> { |
| 33 | + |
| 34 | + final SingleSource<S> source; |
| 35 | + final Function<? super S, ? extends Publisher<? extends T>> mapper; |
| 36 | + |
| 37 | + public SingleFlatMapPublisher(SingleSource<S> source, |
| 38 | + Function<? super S, ? extends Publisher<? extends T>> mapper) { |
| 39 | + this.source = source; |
| 40 | + this.mapper = mapper; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected void subscribeActual(Subscriber<? super T> actual) { |
| 45 | + source.subscribe(new SingleFlatMapPublisherObserver<S, T>(actual, mapper)); |
| 46 | + } |
| 47 | + |
| 48 | + static final class SingleFlatMapPublisherObserver<S, T> extends AtomicLong |
| 49 | + implements SingleObserver<S>, Subscriber<T>, Subscription { |
| 50 | + |
| 51 | + private static final long serialVersionUID = 7759721921468635667L; |
| 52 | + |
| 53 | + final Subscriber<? super T> actual; |
| 54 | + final Function<? super S, ? extends Publisher<? extends T>> mapper; |
| 55 | + final AtomicReference<Subscription> parent = new AtomicReference<Subscription>(); |
| 56 | + Disposable disposable; |
| 57 | + |
| 58 | + SingleFlatMapPublisherObserver(Subscriber<? super T> actual, |
| 59 | + Function<? super S, ? extends Publisher<? extends T>> mapper) { |
| 60 | + this.actual = actual; |
| 61 | + this.mapper = mapper; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void onSubscribe(Disposable d) { |
| 66 | + this.disposable = d; |
| 67 | + actual.onSubscribe(this); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void onSuccess(S value) { |
| 72 | + Publisher<? extends T> f; |
| 73 | + try { |
| 74 | + f = ObjectHelper.requireNonNull(mapper.apply(value), "mapper returns null"); |
| 75 | + } catch (Exception e) { |
| 76 | + Exceptions.throwIfFatal(e); |
| 77 | + actual.onError(e); |
| 78 | + return; |
| 79 | + } |
| 80 | + f.subscribe(this); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void onSubscribe(Subscription s) { |
| 85 | + SubscriptionHelper.deferredSetOnce(parent, this, s); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onNext(T t) { |
| 90 | + actual.onNext(t); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onComplete() { |
| 95 | + actual.onComplete(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void onError(Throwable e) { |
| 100 | + actual.onError(e); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void request(long n) { |
| 105 | + SubscriptionHelper.deferredRequest(parent, this, n); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void cancel() { |
| 110 | + disposable.dispose(); |
| 111 | + SubscriptionHelper.cancel(parent); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments