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