Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/maybe/MaybeDoOnEvent.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.BiConsumer;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21
22 /**
23 * Calls a BiConsumer with the success, error values of the upstream Maybe or with two nulls if
24 * the Maybe completed.
25 *
26 * @param <T> the value type
27 */
28 public final class MaybeDoOnEvent<T> extends AbstractMaybeWithUpstream<T, T> {
29
30 final BiConsumer<? super T, ? super Throwable> onEvent;
31
32 public MaybeDoOnEvent(MaybeSource<T> source, BiConsumer<? super T, ? super Throwable> onEvent) {
33 super(source);
34 this.onEvent = onEvent;
35 }
36
37 @Override
38 protected void subscribeActual(MaybeObserver<? super T> observer) {
39 source.subscribe(new DoOnEventMaybeObserver<>(observer, onEvent));
40 }
41
42 static final class DoOnEventMaybeObserver<T> implements MaybeObserver<T>, Disposable {
43 final MaybeObserver<? super T> downstream;
44
45 final BiConsumer<? super T, ? super Throwable> onEvent;
46
47 Disposable upstream;
48
49 DoOnEventMaybeObserver(MaybeObserver<? super T> actual, BiConsumer<? super T, ? super Throwable> onEvent) {
50 this.downstream = actual;
51 this.onEvent = onEvent;
52 }
53
54 @Override
55 public void dispose() {
56 upstream.dispose();
57 upstream = DisposableHelper.DISPOSED;
58 }
59
60 @Override
61 public boolean isDisposed() {
62 return upstream.isDisposed();
63 }
64
65 @Override
66 public void onSubscribe(Disposable d) {
67 if (DisposableHelper.validate(this.upstream, d)) {
68 this.upstream = d;
69
70 downstream.onSubscribe(this);
71 }
72 }
73
74 @Override
75 public void onSuccess(T value) {
76 upstream = DisposableHelper.DISPOSED;
77
78 try {
79 onEvent.accept(value, null);
80 } catch (Throwable ex) {
81 Exceptions.throwIfFatal(ex);
82 downstream.onError(ex);
83 return;
84 }
85
86 downstream.onSuccess(value);
87 }
88
89 @Override
90 public void onError(Throwable e) {
91 upstream = DisposableHelper.DISPOSED;
92
93 try {
94 onEvent.accept(null, e);
95 } catch (Throwable ex) {
96 Exceptions.throwIfFatal(ex);
97 e = new CompositeException(e, ex);
98 }
99
100 downstream.onError(e);
101 }
102
103 @Override
104 public void onComplete() {
105 upstream = DisposableHelper.DISPOSED;
106
107 try {
108 onEvent.accept(null, null);
109 } catch (Throwable ex) {
110 Exceptions.throwIfFatal(ex);
111 downstream.onError(ex);
112 return;
113 }
114
115 downstream.onComplete();
116 }
117 }
118 }