Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observers/DefaultObserver.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 io.reactivex.rxjava3.annotations.NonNull;
17 import io.reactivex.rxjava3.core.Observer;
18 import io.reactivex.rxjava3.disposables.Disposable;
19 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
20 import io.reactivex.rxjava3.internal.util.EndConsumerHelper;
21
22 /**
23 * Abstract base implementation of an {@link io.reactivex.rxjava3.core.Observer Observer} with support for cancelling a
24 * subscription via {@link #cancel()} (synchronously) and calls {@link #onStart()}
25 * when the subscription happens.
26 *
27 * <p>All pre-implemented final methods are thread-safe.
28 *
29 * <p>Use the protected {@link #cancel()} to dispose the sequence from within an
30 * {@code onNext} implementation.
31 *
32 * <p>Like all other consumers, {@code DefaultObserver} 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 * Observable.range(1, 5)
43 * .subscribe(new DefaultObserver&lt;Integer&gt;() {
44 * &#64;Override public void onStart() {
45 * System.out.println("Start!");
46 * }
47 * &#64;Override public void onNext(Integer t) {
48 * if (t == 3) {
49 * cancel();
50 * }
51 * System.out.println(t);
52 * }
53 * &#64;Override public void onError(Throwable t) {
54 * t.printStackTrace();
55 * }
56 * &#64;Override public void onComplete() {
57 * System.out.println("Done!");
58 * }
59 * });
60 * </code></pre>
61 *
62 * @param <T> the value type
63 */
64 public abstract class DefaultObserver<T> implements Observer<T> {
65
66 private Disposable upstream;
67
68 @Override
69 public final void onSubscribe(@NonNull Disposable d) {
70 if (EndConsumerHelper.validate(this.upstream, d, getClass())) {
71 this.upstream = d;
72 onStart();
73 }
74 }
75
76 /**
77 * Cancels the upstream's disposable.
78 */
79 protected final void cancel() {
80 Disposable upstream = this.upstream;
81 this.upstream = DisposableHelper.DISPOSED;
82 upstream.dispose();
83 }
84 /**
85 * Called once the subscription has been set on this observer; override this
86 * to perform initialization.
87 */
88 protected void onStart() {
89 }
90
91 }