|
| 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.single; |
| 15 | + |
| 16 | +import java.util.Objects; |
| 17 | +import java.util.concurrent.atomic.AtomicReference; |
| 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.*; |
| 23 | +import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 | + |
| 25 | +/** |
| 26 | + * Maps a source item to another SingleSource then calls a BiFunction with the |
| 27 | + * original item and the secondary item to generate the final result. |
| 28 | + * |
| 29 | + * @param <T> the main value type |
| 30 | + * @param <U> the second value type |
| 31 | + * @param <R> the result value type |
| 32 | + * @since 3.0.0 |
| 33 | + */ |
| 34 | +public final class SingleFlatMapBiSelector<T, U, R> extends Single<R> { |
| 35 | + |
| 36 | + final SingleSource<T> source; |
| 37 | + |
| 38 | + final Function<? super T, ? extends SingleSource<? extends U>> mapper; |
| 39 | + |
| 40 | + final BiFunction<? super T, ? super U, ? extends R> resultSelector; |
| 41 | + |
| 42 | + public SingleFlatMapBiSelector(SingleSource<T> source, |
| 43 | + Function<? super T, ? extends SingleSource<? extends U>> mapper, |
| 44 | + BiFunction<? super T, ? super U, ? extends R> resultSelector) { |
| 45 | + this.source = source; |
| 46 | + this.mapper = mapper; |
| 47 | + this.resultSelector = resultSelector; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void subscribeActual(SingleObserver<? super R> observer) { |
| 52 | + source.subscribe(new FlatMapBiMainObserver<T, U, R>(observer, mapper, resultSelector)); |
| 53 | + } |
| 54 | + |
| 55 | + static final class FlatMapBiMainObserver<T, U, R> |
| 56 | + implements SingleObserver<T>, Disposable { |
| 57 | + |
| 58 | + final Function<? super T, ? extends SingleSource<? extends U>> mapper; |
| 59 | + |
| 60 | + final InnerObserver<T, U, R> inner; |
| 61 | + |
| 62 | + FlatMapBiMainObserver(SingleObserver<? super R> actual, |
| 63 | + Function<? super T, ? extends SingleSource<? extends U>> mapper, |
| 64 | + BiFunction<? super T, ? super U, ? extends R> resultSelector) { |
| 65 | + this.inner = new InnerObserver<>(actual, resultSelector); |
| 66 | + this.mapper = mapper; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void dispose() { |
| 71 | + DisposableHelper.dispose(inner); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public boolean isDisposed() { |
| 76 | + return DisposableHelper.isDisposed(inner.get()); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onSubscribe(Disposable d) { |
| 81 | + if (DisposableHelper.setOnce(inner, d)) { |
| 82 | + inner.downstream.onSubscribe(this); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onSuccess(T value) { |
| 88 | + SingleSource<? extends U> next; |
| 89 | + |
| 90 | + try { |
| 91 | + next = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null MaybeSource"); |
| 92 | + } catch (Throwable ex) { |
| 93 | + Exceptions.throwIfFatal(ex); |
| 94 | + inner.downstream.onError(ex); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + if (DisposableHelper.replace(inner, null)) { |
| 99 | + inner.value = value; |
| 100 | + next.subscribe(inner); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void onError(Throwable e) { |
| 106 | + inner.downstream.onError(e); |
| 107 | + } |
| 108 | + |
| 109 | + static final class InnerObserver<T, U, R> |
| 110 | + extends AtomicReference<Disposable> |
| 111 | + implements SingleObserver<U> { |
| 112 | + |
| 113 | + private static final long serialVersionUID = -2897979525538174559L; |
| 114 | + |
| 115 | + final SingleObserver<? super R> downstream; |
| 116 | + |
| 117 | + final BiFunction<? super T, ? super U, ? extends R> resultSelector; |
| 118 | + |
| 119 | + T value; |
| 120 | + |
| 121 | + InnerObserver(SingleObserver<? super R> actual, |
| 122 | + BiFunction<? super T, ? super U, ? extends R> resultSelector) { |
| 123 | + this.downstream = actual; |
| 124 | + this.resultSelector = resultSelector; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onSubscribe(Disposable d) { |
| 129 | + DisposableHelper.setOnce(this, d); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onSuccess(U value) { |
| 134 | + T t = this.value; |
| 135 | + this.value = null; |
| 136 | + |
| 137 | + R r; |
| 138 | + |
| 139 | + try { |
| 140 | + r = Objects.requireNonNull(resultSelector.apply(t, value), "The resultSelector returned a null value"); |
| 141 | + } catch (Throwable ex) { |
| 142 | + Exceptions.throwIfFatal(ex); |
| 143 | + downstream.onError(ex); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + downstream.onSuccess(r); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void onError(Throwable e) { |
| 152 | + downstream.onError(e); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments