| 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.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
|
| 25 |
/** |
| 26 |
* Maps the success value of the source MaybeSource into a Single. |
| 27 |
* <p>History: 2.0.2 - experimental |
| 28 |
* @param <T> the input value type |
| 29 |
* @param <R> the result value type |
| 30 |
* @since 2.1 |
| 31 |
*/ |
| 32 |
public final class MaybeFlatMapSingle<T, R> extends Maybe<R> { |
| 33 |
|
| 34 |
final MaybeSource<T> source; |
| 35 |
|
| 36 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 37 |
|
| 38 |
public MaybeFlatMapSingle(MaybeSource<T> source, Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
| 39 |
this.source = source; |
| 40 |
this.mapper = mapper; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
protected void subscribeActual(MaybeObserver<? super R> downstream) { |
| 45 |
source.subscribe(new FlatMapMaybeObserver<>(downstream, mapper)); |
| 46 |
} |
| 47 |
|
| 48 |
static final class FlatMapMaybeObserver<T, R> |
| 49 |
extends AtomicReference<Disposable> |
| 50 |
implements MaybeObserver<T>, Disposable { |
| 51 |
|
| 52 |
private static final long serialVersionUID = 4827726964688405508L; |
| 53 |
|
| 54 |
final MaybeObserver<? super R> downstream; |
| 55 |
|
| 56 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 57 |
|
| 58 |
FlatMapMaybeObserver(MaybeObserver<? super R> actual, Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
| 59 |
this.downstream = actual; |
| 60 |
this.mapper = mapper; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void dispose() { |
| 65 |
DisposableHelper.dispose(this); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean isDisposed() { |
| 70 |
return DisposableHelper.isDisposed(get()); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onSubscribe(Disposable d) { |
| 75 |
if (DisposableHelper.setOnce(this, d)) { |
| 76 |
downstream.onSubscribe(this); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public void onSuccess(T value) { |
| 82 |
SingleSource<? extends R> ss; |
| 83 |
|
| 84 |
try { |
| 85 |
ss = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null SingleSource"); |
| 86 |
} catch (Throwable ex) { |
| 87 |
Exceptions.throwIfFatal(ex); |
| 88 |
onError(ex); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
if (!isDisposed()) { |
| 93 |
ss.subscribe(new FlatMapSingleObserver<R>(this, downstream)); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void onError(Throwable e) { |
| 99 |
downstream.onError(e); |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void onComplete() { |
| 104 |
downstream.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> downstream; |
| 113 |
|
| 114 |
FlatMapSingleObserver(AtomicReference<Disposable> parent, MaybeObserver<? super R> downstream) { |
| 115 |
this.parent = parent; |
| 116 |
this.downstream = downstream; |
| 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 |
downstream.onSuccess(value); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void onError(final Throwable e) { |
| 131 |
downstream.onError(e); |
| 132 |
} |
| 133 |
} |
| 134 |
} |