Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/observable/ObserverResourceWrapper.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 java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.Observer;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21
22 public final class ObserverResourceWrapper<T> extends AtomicReference<Disposable> implements Observer<T>, Disposable {
23
24 private static final long serialVersionUID = -8612022020200669122L;
25
26 final Observer<? super T> downstream;
27
28 final AtomicReference<Disposable> upstream = new AtomicReference<>();
29
30 public ObserverResourceWrapper(Observer<? super T> downstream) {
31 this.downstream = downstream;
32 }
33
34 @Override
35 public void onSubscribe(Disposable d) {
36 if (DisposableHelper.setOnce(upstream, d)) {
37 downstream.onSubscribe(this);
38 }
39 }
40
41 @Override
42 public void onNext(T t) {
43 downstream.onNext(t);
44 }
45
46 @Override
47 public void onError(Throwable t) {
48 dispose();
49 downstream.onError(t);
50 }
51
52 @Override
53 public void onComplete() {
54 dispose();
55 downstream.onComplete();
56 }
57
58 @Override
59 public void dispose() {
60 DisposableHelper.dispose(upstream);
61
62 DisposableHelper.dispose(this);
63 }
64
65 @Override
66 public boolean isDisposed() {
67 return upstream.get() == DisposableHelper.DISPOSED;
68 }
69
70 public void setResource(Disposable resource) {
71 DisposableHelper.set(this, resource);
72 }
73 }