Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/DisposableAutoReleaseObserver.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 /*
15 * Copyright 2016-2019 David Karnok
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License");
18 * you may not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 */
29
30 package io.reactivex.rxjava3.internal.observers;
31
32 import io.reactivex.rxjava3.core.Observer;
33 import io.reactivex.rxjava3.disposables.DisposableContainer;
34 import io.reactivex.rxjava3.exceptions.Exceptions;
35 import io.reactivex.rxjava3.functions.*;
36 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
37
38 /**
39 * Wraps lambda callbacks and when the upstream terminates or this observer gets disposed,
40 * removes itself from a {@link io.reactivex.rxjava3.disposables.CompositeDisposable}.
41 * <p>History: 0.18.0 @ RxJavaExtensions
42 * @param <T> the element type consumed
43 * @since 3.1.0
44 */
45 public final class DisposableAutoReleaseObserver<T>
46 extends AbstractDisposableAutoRelease
47 implements Observer<T> {
48
49 private static final long serialVersionUID = 8924480688481408726L;
50
51 final Consumer<? super T> onNext;
52
53 public DisposableAutoReleaseObserver(
54 DisposableContainer composite,
55 Consumer<? super T> onNext,
56 Consumer<? super Throwable> onError,
57 Action onComplete
58 ) {
59 super(composite, onError, onComplete);
60 this.onNext = onNext;
61 }
62
63 @Override
64 public void onNext(T t) {
65 if (get() != DisposableHelper.DISPOSED) {
66 try {
67 onNext.accept(t);
68 } catch (Throwable e) {
69 Exceptions.throwIfFatal(e);
70 get().dispose();
71 onError(e);
72 }
73 }
74 }
75
76 }