Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/BlockingObserver.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.observers;
15
16 import java.util.Queue;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import io.reactivex.rxjava3.core.Observer;
20 import io.reactivex.rxjava3.disposables.Disposable;
21 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
22 import io.reactivex.rxjava3.internal.util.NotificationLite;
23
24 public final class BlockingObserver<T> extends AtomicReference<Disposable> implements Observer<T>, Disposable {
25
26 private static final long serialVersionUID = -4875965440900746268L;
27
28 public static final Object TERMINATED = new Object();
29
30 final Queue<Object> queue;
31
32 public BlockingObserver(Queue<Object> queue) {
33 this.queue = queue;
34 }
35
36 @Override
37 public void onSubscribe(Disposable d) {
38 DisposableHelper.setOnce(this, d);
39 }
40
41 @Override
42 public void onNext(T t) {
43 queue.offer(NotificationLite.next(t));
44 }
45
46 @Override
47 public void onError(Throwable t) {
48 queue.offer(NotificationLite.error(t));
49 }
50
51 @Override
52 public void onComplete() {
53 queue.offer(NotificationLite.complete());
54 }
55
56 @Override
57 public void dispose() {
58 if (DisposableHelper.dispose(this)) {
59 queue.offer(TERMINATED);
60 }
61 }
62
63 @Override
64 public boolean isDisposed() {
65 return get() == DisposableHelper.DISPOSED;
66 }
67 }