|
| 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.maybe; |
| 15 | + |
| 16 | +import java.util.concurrent.atomic.AtomicReference; |
| 17 | + |
| 18 | +import io.reactivex.*; |
| 19 | +import io.reactivex.annotations.Experimental; |
| 20 | +import io.reactivex.disposables.Disposable; |
| 21 | +import io.reactivex.exceptions.Exceptions; |
| 22 | +import io.reactivex.functions.Function; |
| 23 | +import io.reactivex.internal.disposables.DisposableHelper; |
| 24 | +import io.reactivex.internal.functions.ObjectHelper; |
| 25 | + |
| 26 | +/** |
| 27 | + * Maps the success value of the source MaybeSource into a Single. |
| 28 | + * @param <T> the input value type |
| 29 | + * @param <R> the result value type |
| 30 | + * |
| 31 | + * @since 2.0.2 - experimental |
| 32 | + */ |
| 33 | +@Experimental |
| 34 | +public final class MaybeFlatMapSingleElement<T, R> extends Maybe<R> { |
| 35 | + |
| 36 | + final MaybeSource<T> source; |
| 37 | + |
| 38 | + final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 39 | + |
| 40 | + public MaybeFlatMapSingleElement(MaybeSource<T> source, Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
| 41 | + this.source = source; |
| 42 | + this.mapper = mapper; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void subscribeActual(MaybeObserver<? super R> actual) { |
| 47 | + source.subscribe(new FlatMapMaybeObserver<T, R>(actual, mapper)); |
| 48 | + } |
| 49 | + |
| 50 | + static final class FlatMapMaybeObserver<T, R> |
| 51 | + extends AtomicReference<Disposable> |
| 52 | + implements MaybeObserver<T>, Disposable { |
| 53 | + |
| 54 | + private static final long serialVersionUID = 4827726964688405508L; |
| 55 | + |
| 56 | + final MaybeObserver<? super R> actual; |
| 57 | + |
| 58 | + final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 59 | + |
| 60 | + FlatMapMaybeObserver(MaybeObserver<? super R> actual, Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
| 61 | + this.actual = actual; |
| 62 | + this.mapper = mapper; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void dispose() { |
| 67 | + DisposableHelper.dispose(this); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean isDisposed() { |
| 72 | + return DisposableHelper.isDisposed(get()); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onSubscribe(Disposable d) { |
| 77 | + if (DisposableHelper.setOnce(this, d)) { |
| 78 | + actual.onSubscribe(this); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onSuccess(T value) { |
| 84 | + SingleSource<? extends R> ss; |
| 85 | + |
| 86 | + try { |
| 87 | + ss = ObjectHelper.requireNonNull(mapper.apply(value), "The mapper returned a null SingleSource"); |
| 88 | + } catch (Throwable ex) { |
| 89 | + Exceptions.throwIfFatal(ex); |
| 90 | + onError(ex); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + ss.subscribe(new FlatMapSingleObserver<R>(this, actual)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void onError(Throwable e) { |
| 99 | + actual.onError(e); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onComplete() { |
| 104 | + actual.onComplete(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + static final class FlatMapSingleObserver<R> implements SingleObserver<R> { |
| 109 | + |
| 110 | + final AtomicReference<Disposable> parent; |
| 111 | + |
| 112 | + final MaybeObserver<? super R> actual; |
| 113 | + |
| 114 | + FlatMapSingleObserver(AtomicReference<Disposable> parent, MaybeObserver<? super R> actual) { |
| 115 | + this.parent = parent; |
| 116 | + this.actual = actual; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void onSubscribe(final Disposable d) { |
| 121 | + DisposableHelper.replace(parent, d); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public void onSuccess(final R value) { |
| 126 | + actual.onSuccess(value); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void onError(final Throwable e) { |
| 131 | + actual.onError(e); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments