Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObservableOnErrorComplete.java

raw
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.observable;
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 * @since 3.0.0
28 */
29 public final class ObservableOnErrorComplete<T> extends AbstractObservableWithUpstream<T, T> {
30
31 final Predicate<? super Throwable> predicate;
32
33 public ObservableOnErrorComplete(ObservableSource<T> source,
34 Predicate<? super Throwable> predicate) {
35 super(source);
36 this.predicate = predicate;
37 }
38
39 @Override
40 protected void subscribeActual(Observer<? super T> observer) {
41 source.subscribe(new OnErrorCompleteObserver<>(observer, predicate));
42 }
43
44 public static final class OnErrorCompleteObserver<T>
45 implements Observer<T>, Disposable {
46
47 final Observer<? super T> downstream;
48
49 final Predicate<? super Throwable> predicate;
50
51 Disposable upstream;
52
53 public OnErrorCompleteObserver(Observer<? super T> actual, Predicate<? super Throwable> predicate) {
54 this.downstream = actual;
55 this.predicate = predicate;
56 }
57
58 @Override
59 public void onSubscribe(Disposable d) {
60 if (DisposableHelper.validate(this.upstream, d)) {
61 this.upstream = d;
62
63 downstream.onSubscribe(this);
64 }
65 }
66
67 @Override
68 public void onNext(T value) {
69 downstream.onNext(value);
70 }
71
72 @Override
73 public void onError(Throwable e) {
74 boolean b;
75
76 try {
77 b = predicate.test(e);
78 } catch (Throwable ex) {
79 Exceptions.throwIfFatal(ex);
80 downstream.onError(new CompositeException(e, ex));
81 return;
82 }
83
84 if (b) {
85 downstream.onComplete();
86 } else {
87 downstream.onError(e);
88 }
89 }
90
91 @Override
92 public void onComplete() {
93 downstream.onComplete();
94 }
95
96 @Override
97 public void dispose() {
98 upstream.dispose();
99 }
100
101 @Override
102 public boolean isDisposed() {
103 return upstream.isDisposed();
104 }
105 }
106 }