| 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 a value into a MaybeSource and relays its signal. |
| 27 |
* |
| 28 |
* @param <T> the source value type |
| 29 |
* @param <R> the result value type |
| 30 |
*/ |
| 31 |
public final class MaybeFlatten<T, R> extends AbstractMaybeWithUpstream<T, R> { |
| 32 |
|
| 33 |
final Function<? super T, ? extends MaybeSource<? extends R>> mapper; |
| 34 |
|
| 35 |
public MaybeFlatten(MaybeSource<T> source, Function<? super T, ? extends MaybeSource<? extends R>> mapper) { |
| 36 |
super(source); |
| 37 |
this.mapper = mapper; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(MaybeObserver<? super R> observer) { |
| 42 |
source.subscribe(new FlatMapMaybeObserver<>(observer, mapper)); |
| 43 |
} |
| 44 |
|
| 45 |
static final class FlatMapMaybeObserver<T, R> |
| 46 |
extends AtomicReference<Disposable> |
| 47 |
implements MaybeObserver<T>, Disposable { |
| 48 |
|
| 49 |
private static final long serialVersionUID = 4375739915521278546L; |
| 50 |
|
| 51 |
final MaybeObserver<? super R> downstream; |
| 52 |
|
| 53 |
final Function<? super T, ? extends MaybeSource<? extends R>> mapper; |
| 54 |
|
| 55 |
Disposable upstream; |
| 56 |
|
| 57 |
FlatMapMaybeObserver(MaybeObserver<? super R> actual, |
| 58 |
Function<? super T, ? extends MaybeSource<? extends R>> mapper) { |
| 59 |
this.downstream = actual; |
| 60 |
this.mapper = mapper; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void dispose() { |
| 65 |
DisposableHelper.dispose(this); |
| 66 |
upstream.dispose(); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public boolean isDisposed() { |
| 71 |
return DisposableHelper.isDisposed(get()); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void onSubscribe(Disposable d) { |
| 76 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 77 |
this.upstream = d; |
| 78 |
|
| 79 |
downstream.onSubscribe(this); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
@Override |
| 84 |
public void onSuccess(T value) { |
| 85 |
MaybeSource<? extends R> source; |
| 86 |
|
| 87 |
try { |
| 88 |
source = Objects.requireNonNull(mapper.apply(value), "The mapper returned a null MaybeSource"); |
| 89 |
} catch (Throwable ex) { |
| 90 |
Exceptions.throwIfFatal(ex); |
| 91 |
downstream.onError(ex); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if (!isDisposed()) { |
| 96 |
source.subscribe(new InnerObserver()); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void onError(Throwable e) { |
| 102 |
downstream.onError(e); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onComplete() { |
| 107 |
downstream.onComplete(); |
| 108 |
} |
| 109 |
|
| 110 |
final class InnerObserver implements MaybeObserver<R> { |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onSubscribe(Disposable d) { |
| 114 |
DisposableHelper.setOnce(FlatMapMaybeObserver.this, d); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void onSuccess(R value) { |
| 119 |
downstream.onSuccess(value); |
| 120 |
} |
| 121 |
|
| 122 |
@Override |
| 123 |
public void onError(Throwable e) { |
| 124 |
downstream.onError(e); |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
public void onComplete() { |
| 129 |
downstream.onComplete(); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |