Debian Codegraph @

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