| 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.Function; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 24 |
|
| 25 |
public final class SingleFlatMap<T, R> extends Single<R> { |
| 26 |
final SingleSource<? extends T> source; |
| 27 |
|
| 28 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 29 |
|
| 30 |
public SingleFlatMap(SingleSource<? extends T> source, Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
| 31 |
this.mapper = mapper; |
| 32 |
this.source = source; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
protected void subscribeActual(SingleObserver<? super R> downstream) { |
| 37 |
source.subscribe(new SingleFlatMapCallback<T, R>(downstream, mapper)); |
| 38 |
} |
| 39 |
|
| 40 |
static final class SingleFlatMapCallback<T, R> |
| 41 |
extends AtomicReference<Disposable> |
| 42 |
implements SingleObserver<T>, Disposable { |
| 43 |
private static final long serialVersionUID = 3258103020495908596L; |
| 44 |
|
| 45 |
final SingleObserver<? super R> downstream; |
| 46 |
|
| 47 |
final Function<? super T, ? extends SingleSource<? extends R>> mapper; |
| 48 |
|
| 49 |
SingleFlatMapCallback(SingleObserver<? super R> actual, |
| 50 |
Function<? super T, ? extends SingleSource<? extends R>> mapper) { |
| 51 |
this.downstream = actual; |
| 52 |
this.mapper = mapper; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public void dispose() { |
| 57 |
DisposableHelper.dispose(this); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public boolean isDisposed() { |
| 62 |
return DisposableHelper.isDisposed(get()); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void onSubscribe(Disposable d) { |
| 67 |
if (DisposableHelper.setOnce(this, d)) { |
| 68 |
downstream.onSubscribe(this); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
@Override |
| 73 |
public void onSuccess(T value) { |
| 74 |
SingleSource<? extends R> o; |
| 75 |
|
| 76 |
try { |
| 77 |
o = Objects.requireNonNull(mapper.apply(value), "The single returned by the mapper is null"); |
| 78 |
} catch (Throwable e) { |
| 79 |
Exceptions.throwIfFatal(e); |
| 80 |
downstream.onError(e); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if (!isDisposed()) { |
| 85 |
o.subscribe(new FlatMapSingleObserver<R>(this, downstream)); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
@Override |
| 90 |
public void onError(Throwable e) { |
| 91 |
downstream.onError(e); |
| 92 |
} |
| 93 |
|
| 94 |
static final class FlatMapSingleObserver<R> implements SingleObserver<R> { |
| 95 |
|
| 96 |
final AtomicReference<Disposable> parent; |
| 97 |
|
| 98 |
final SingleObserver<? super R> downstream; |
| 99 |
|
| 100 |
FlatMapSingleObserver(AtomicReference<Disposable> parent, SingleObserver<? super R> downstream) { |
| 101 |
this.parent = parent; |
| 102 |
this.downstream = downstream; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onSubscribe(final Disposable d) { |
| 107 |
DisposableHelper.replace(parent, d); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void onSuccess(final R value) { |
| 112 |
downstream.onSuccess(value); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public void onError(final Throwable e) { |
| 117 |
downstream.onError(e); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |