| 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 io.reactivex.rxjava3.core.*; |
| 17 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 18 |
import io.reactivex.rxjava3.exceptions.*; |
| 19 |
import io.reactivex.rxjava3.functions.Predicate; |
| 20 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 21 |
|
| 22 |
/** |
| 23 |
* Emits an onComplete if the source emits an onError and the predicate returns true for |
| 24 |
* that Throwable. |
| 25 |
* |
| 26 |
* @param <T> the value type |
| 27 |
*/ |
| 28 |
public final class MaybeOnErrorComplete<T> extends AbstractMaybeWithUpstream<T, T> { |
| 29 |
|
| 30 |
final Predicate<? super Throwable> predicate; |
| 31 |
|
| 32 |
public MaybeOnErrorComplete(MaybeSource<T> source, |
| 33 |
Predicate<? super Throwable> predicate) { |
| 34 |
super(source); |
| 35 |
this.predicate = predicate; |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void subscribeActual(MaybeObserver<? super T> observer) { |
| 40 |
source.subscribe(new OnErrorCompleteMultiObserver<>(observer, predicate)); |
| 41 |
} |
| 42 |
|
| 43 |
public static final class OnErrorCompleteMultiObserver<T> |
| 44 |
implements MaybeObserver<T>, SingleObserver<T>, Disposable { |
| 45 |
|
| 46 |
final MaybeObserver<? super T> downstream; |
| 47 |
|
| 48 |
final Predicate<? super Throwable> predicate; |
| 49 |
|
| 50 |
Disposable upstream; |
| 51 |
|
| 52 |
public OnErrorCompleteMultiObserver(MaybeObserver<? super T> actual, Predicate<? super Throwable> predicate) { |
| 53 |
this.downstream = actual; |
| 54 |
this.predicate = predicate; |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void onSubscribe(Disposable d) { |
| 59 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 60 |
this.upstream = d; |
| 61 |
|
| 62 |
downstream.onSubscribe(this); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onSuccess(T value) { |
| 68 |
downstream.onSuccess(value); |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onError(Throwable e) { |
| 73 |
boolean b; |
| 74 |
|
| 75 |
try { |
| 76 |
b = predicate.test(e); |
| 77 |
} catch (Throwable ex) { |
| 78 |
Exceptions.throwIfFatal(ex); |
| 79 |
downstream.onError(new CompositeException(e, ex)); |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
if (b) { |
| 84 |
downstream.onComplete(); |
| 85 |
} else { |
| 86 |
downstream.onError(e); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public void onComplete() { |
| 92 |
downstream.onComplete(); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public void dispose() { |
| 97 |
upstream.dispose(); |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public boolean isDisposed() { |
| 102 |
return upstream.isDisposed(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |