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