| 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.maybe; |
| 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 MaybeSource 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 |
*/ |
| 33 |
public final class MaybeFlatMapBiSelector<T, U, R> extends AbstractMaybeWithUpstream<T, R> { |
| 34 |
|
| 35 |
final Function<? super T, ? extends MaybeSource<? extends U>> mapper; |
| 36 |
|
| 37 |
final BiFunction<? super T, ? super U, ? extends R> resultSelector; |
| 38 |
|
| 39 |
public MaybeFlatMapBiSelector(MaybeSource<T> source, |
| 40 |
Function<? super T, ? extends MaybeSource<? extends U>> mapper, |
| 41 |
BiFunction<? super T, ? super U, ? extends R> resultSelector) { |
| 42 |
super(source); |
| 43 |
this.mapper = mapper; |
| 44 |
this.resultSelector = resultSelector; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
protected void subscribeActual(MaybeObserver<? super R> observer) { |
| 49 |
source.subscribe(new FlatMapBiMainObserver<T, U, R>(observer, mapper, resultSelector)); |
| 50 |
} |
| 51 |
|
| 52 |
static final class FlatMapBiMainObserver<T, U, R> |
| 53 |
implements MaybeObserver<T>, Disposable { |
| 54 |
|
| 55 |
final Function<? super T, ? extends MaybeSource<? extends U>> mapper; |
| 56 |
|
| 57 |
final InnerObserver<T, U, R> inner; |
| 58 |
|
| 59 |
FlatMapBiMainObserver(MaybeObserver<? super R> actual, |
| 60 |
Function<? super T, ? extends MaybeSource<? extends U>> mapper, |
| 61 |
BiFunction<? super T, ? super U, ? extends R> resultSelector) { |
| 62 |
this.inner = new InnerObserver<>(actual, resultSelector); |
| 63 |
this.mapper = mapper; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void dispose() { |
| 68 |
DisposableHelper.dispose(inner); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean isDisposed() { |
| 73 |
return DisposableHelper.isDisposed(inner.get()); |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onSubscribe(Disposable d) { |
| 78 |
if (DisposableHelper.setOnce(inner, d)) { |
| 79 |
inner.downstream.onSubscribe(this); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onSuccess(T value) { |
| 85 |
MaybeSource<? extends U> next; |
| 86 |
|
| 87 |
try { |
| 88 |
next = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null MaybeSource"); |
| 89 |
} catch (Throwable ex) { |
| 90 |
Exceptions.throwIfFatal(ex); |
| 91 |
inner.downstream.onError(ex); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if (DisposableHelper.replace(inner, null)) { |
| 96 |
inner.value = value; |
| 97 |
next.subscribe(inner); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public void onError(Throwable e) { |
| 103 |
inner.downstream.onError(e); |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onComplete() { |
| 108 |
inner.downstream.onComplete(); |
| 109 |
} |
| 110 |
|
| 111 |
static final class InnerObserver<T, U, R> |
| 112 |
extends AtomicReference<Disposable> |
| 113 |
implements MaybeObserver<U> { |
| 114 |
|
| 115 |
private static final long serialVersionUID = -2897979525538174559L; |
| 116 |
|
| 117 |
final MaybeObserver<? super R> downstream; |
| 118 |
|
| 119 |
final BiFunction<? super T, ? super U, ? extends R> resultSelector; |
| 120 |
|
| 121 |
T value; |
| 122 |
|
| 123 |
InnerObserver(MaybeObserver<? super R> actual, |
| 124 |
BiFunction<? super T, ? super U, ? extends R> resultSelector) { |
| 125 |
this.downstream = actual; |
| 126 |
this.resultSelector = resultSelector; |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void onSubscribe(Disposable d) { |
| 131 |
DisposableHelper.setOnce(this, d); |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public void onSuccess(U value) { |
| 136 |
T t = this.value; |
| 137 |
this.value = null; |
| 138 |
|
| 139 |
R r; |
| 140 |
|
| 141 |
try { |
| 142 |
r = Objects.requireNonNull(resultSelector.apply(t, value), "The resultSelector returned a null value"); |
| 143 |
} catch (Throwable ex) { |
| 144 |
Exceptions.throwIfFatal(ex); |
| 145 |
downstream.onError(ex); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
downstream.onSuccess(r); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public void onError(Throwable e) { |
| 154 |
downstream.onError(e); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
public void onComplete() { |
| 159 |
downstream.onComplete(); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |