Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observers/DisposableSingleObserver.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.SingleObserver;
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 SingleObserver} that allows asynchronous cancellation by implementing {@link Disposable}.
26 *
27 * <p>All pre-implemented final methods are thread-safe.
28 *
29 * <p>Like all other consumers, {@code DisposableSingleObserver} can be subscribed only once.
30 * Any subsequent attempt to subscribe it to a new source will yield an
31 * {@link IllegalStateException} with message {@code "It is not allowed to subscribe with a(n) <class name> multiple times."}.
32 *
33 * <p>Implementation of {@link #onStart()}, {@link #onSuccess(Object)} and {@link #onError(Throwable)}
34 * are not allowed to throw any unchecked exceptions.
35 *
36 * <p>Example<pre><code>
37 * Disposable d =
38 * Single.just(1).delay(1, TimeUnit.SECONDS)
39 * .subscribeWith(new DisposableSingleObserver&lt;Integer&gt;() {
40 * &#64;Override public void onStart() {
41 * System.out.println("Start!");
42 * }
43 * &#64;Override public void onSuccess(Integer t) {
44 * System.out.println(t);
45 * }
46 * &#64;Override public void onError(Throwable t) {
47 * t.printStackTrace();
48 * }
49 * });
50 * // ...
51 * d.dispose();
52 * </code></pre>
53 *
54 * @param <T> the received value type
55 */
56 public abstract class DisposableSingleObserver<T> implements SingleObserver<T>, Disposable {
57
58 final AtomicReference<Disposable> upstream = new AtomicReference<>();
59
60 @Override
61 public final void onSubscribe(@NonNull Disposable d) {
62 if (EndConsumerHelper.setOnce(this.upstream, d, getClass())) {
63 onStart();
64 }
65 }
66
67 /**
68 * Called once the single upstream {@link Disposable} is set via {@link #onSubscribe(Disposable)}.
69 */
70 protected void onStart() {
71 }
72
73 @Override
74 public final boolean isDisposed() {
75 return upstream.get() == DisposableHelper.DISPOSED;
76 }
77
78 @Override
79 public final void dispose() {
80 DisposableHelper.dispose(upstream);
81 }
82 }