Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscriptions/EmptySubscription.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.subscriptions;
15
16 import org.reactivestreams.Subscriber;
17
18 import io.reactivex.rxjava3.annotations.Nullable;
19 import io.reactivex.rxjava3.operators.QueueSubscription;
20
21 /**
22 * An empty subscription that does nothing other than validates the request amount.
23 */
24 public enum EmptySubscription implements QueueSubscription<Object> {
25 /** A singleton, stateless instance. */
26 INSTANCE;
27
28 @Override
29 public void request(long n) {
30 SubscriptionHelper.validate(n);
31 }
32
33 @Override
34 public void cancel() {
35 // no-op
36 }
37
38 @Override
39 public String toString() {
40 return "EmptySubscription";
41 }
42
43 /**
44 * Sets the empty subscription instance on the subscriber and then
45 * calls onError with the supplied error.
46 *
47 * <p>Make sure this is only called if the subscriber hasn't received a
48 * subscription already (there is no way of telling this).
49 *
50 * @param e the error to deliver to the subscriber
51 * @param s the target subscriber
52 */
53 public static void error(Throwable e, Subscriber<?> s) {
54 s.onSubscribe(INSTANCE);
55 s.onError(e);
56 }
57
58 /**
59 * Sets the empty subscription instance on the subscriber and then
60 * calls onComplete.
61 *
62 * <p>Make sure this is only called if the subscriber hasn't received a
63 * subscription already (there is no way of telling this).
64 *
65 * @param s the target subscriber
66 */
67 public static void complete(Subscriber<?> s) {
68 s.onSubscribe(INSTANCE);
69 s.onComplete();
70 }
71
72 @Nullable
73 @Override
74 public Object poll() {
75 return null; // always empty
76 }
77
78 @Override
79 public boolean isEmpty() {
80 return true;
81 }
82
83 @Override
84 public void clear() {
85 // nothing to do
86 }
87
88 @Override
89 public int requestFusion(int mode) {
90 return mode & ASYNC; // accept async mode: an onComplete or onError will be signalled after anyway
91 }
92
93 @Override
94 public boolean offer(Object value) {
95 throw new UnsupportedOperationException("Should not be called!");
96 }
97
98 @Override
99 public boolean offer(Object v1, Object v2) {
100 throw new UnsupportedOperationException("Should not be called!");
101 }
102 }