Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observers/DisposableObserver.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.observers;
15
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.annotations.NonNull;
19 import io.reactivex.rxjava3.core.Observer;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
22 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
23
24 /**
25 * An abstract {@link Observer} that allows asynchronous cancellation by implementing {@link Disposable}.
26 *
27 * <p>All pre-implemented final methods are thread-safe.
28 *
29 * <p>Use the public {@link #dispose()} method to dispose the sequence from within an
30 * {@code onNext} implementation.
31 *
32 * <p>Like all other consumers, {@code DisposableObserver} can be subscribed only once.
33 * Any subsequent attempt to subscribe it to a new source will yield an
34 * {@link IllegalStateException} with message {@code "It is not allowed to subscribe with a(n) <class name> multiple times."}.
35 *
36 * <p>Implementation of {@link #onStart()}, {@link #onNext(Object)}, {@link #onError(Throwable)}
37 * and {@link #onComplete()} are not allowed to throw any unchecked exceptions.
38 * If for some reason this can't be avoided, use {@link io.reactivex.rxjava3.core.Observable#safeSubscribe(io.reactivex.rxjava3.core.Observer)}
39 * instead of the standard {@code subscribe()} method.
40 *
41 * <p>Example<pre><code>
42 * Disposable d =
43 * Observable.range(1, 5)
44 * .subscribeWith(new DisposableObserver&lt;Integer&gt;() {
45 * &#64;Override public void onStart() {
46 * System.out.println("Start!");
47 * }
48 * &#64;Override public void onNext(Integer t) {
49 * if (t == 3) {
50 * dispose();
51 * }
52 * System.out.println(t);
53 * }
54 * &#64;Override public void onError(Throwable t) {
55 * t.printStackTrace();
56 * }
57 * &#64;Override public void onComplete() {
58 * System.out.println("Done!");
59 * }
60 * });
61 * // ...
62 * d.dispose();
63 * </code></pre>
64 *
65 * @param <T> the received value type
66 */
67 public abstract class DisposableObserver<T> implements Observer<T>, Disposable {
68
69 final AtomicReference<Disposable> upstream = new AtomicReference<>();
70
71 @Override
72 public final void onSubscribe(@NonNull Disposable d) {
73 if (EndConsumerHelper.setOnce(this.upstream, d, getClass())) {
74 onStart();
75 }
76 }
77
78 /**
79 * Called once the single upstream Disposable is set via onSubscribe.
80 */
81 protected void onStart() {
82 }
83
84 @Override
85 public final boolean isDisposed() {
86 return upstream.get() == DisposableHelper.DISPOSED;
87 }
88
89 @Override
90 public final void dispose() {
91 DisposableHelper.dispose(upstream);
92 }
93 }