Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/subscribers/BlockingSubscriber.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.subscribers;
15
16 import java.util.Queue;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.reactivestreams.Subscription;
20
21 import io.reactivex.rxjava3.core.FlowableSubscriber;
22 import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
23 import io.reactivex.rxjava3.internal.util.NotificationLite;
24
25 public final class BlockingSubscriber<T> extends AtomicReference<Subscription> implements FlowableSubscriber<T>, Subscription {
26
27 private static final long serialVersionUID = -4875965440900746268L;
28
29 public static final Object TERMINATED = new Object();
30
31 final Queue<Object> queue;
32
33 public BlockingSubscriber(Queue<Object> queue) {
34 this.queue = queue;
35 }
36
37 @Override
38 public void onSubscribe(Subscription s) {
39 if (SubscriptionHelper.setOnce(this, s)) {
40 queue.offer(NotificationLite.subscription(this));
41 }
42 }
43
44 @Override
45 public void onNext(T t) {
46 queue.offer(NotificationLite.next(t));
47 }
48
49 @Override
50 public void onError(Throwable t) {
51 queue.offer(NotificationLite.error(t));
52 }
53
54 @Override
55 public void onComplete() {
56 queue.offer(NotificationLite.complete());
57 }
58
59 @Override
60 public void request(long n) {
61 get().request(n);
62 }
63
64 @Override
65 public void cancel() {
66 if (SubscriptionHelper.cancel(this)) {
67 queue.offer(TERMINATED);
68 }
69 }
70
71 public boolean isCancelled() {
72 return get() == SubscriptionHelper.CANCELLED;
73 }
74 }