Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDoOnTerminate.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.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.Action;
20
21 public final class MaybeDoOnTerminate<T> extends Maybe<T> {
22
23 final MaybeSource<T> source;
24
25 final Action onTerminate;
26
27 public MaybeDoOnTerminate(MaybeSource<T> source, Action onTerminate) {
28 this.source = source;
29 this.onTerminate = onTerminate;
30 }
31
32 @Override
33 protected void subscribeActual(MaybeObserver<? super T> observer) {
34 source.subscribe(new DoOnTerminate(observer));
35 }
36
37 final class DoOnTerminate implements MaybeObserver<T> {
38 final MaybeObserver<? super T> downstream;
39
40 DoOnTerminate(MaybeObserver<? super T> observer) {
41 this.downstream = observer;
42 }
43
44 @Override
45 public void onSubscribe(Disposable d) {
46 downstream.onSubscribe(d);
47 }
48
49 @Override
50 public void onSuccess(T value) {
51 try {
52 onTerminate.run();
53 } catch (Throwable ex) {
54 Exceptions.throwIfFatal(ex);
55 downstream.onError(ex);
56 return;
57 }
58
59 downstream.onSuccess(value);
60 }
61
62 @Override
63 public void onError(Throwable e) {
64 try {
65 onTerminate.run();
66 } catch (Throwable ex) {
67 Exceptions.throwIfFatal(ex);
68 e = new CompositeException(e, ex);
69 }
70
71 downstream.onError(e);
72 }
73
74 @Override
75 public void onComplete() {
76 try {
77 onTerminate.run();
78 } catch (Throwable ex) {
79 Exceptions.throwIfFatal(ex);
80 downstream.onError(ex);
81 return;
82 }
83
84 downstream.onComplete();
85 }
86 }
87 }