Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/observers/InnerQueuedObserver.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.concurrent.atomic.AtomicReference;
17
18 import io.reactivex.rxjava3.core.Observer;
19 import io.reactivex.rxjava3.disposables.Disposable;
20 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
21 import io.reactivex.rxjava3.internal.util.QueueDrainHelper;
22 import io.reactivex.rxjava3.operators.QueueDisposable;
23 import io.reactivex.rxjava3.operators.QueueSubscription;
24 import io.reactivex.rxjava3.operators.SimpleQueue;
25
26 /**
27 * Subscriber that can fuse with the upstream and calls a support interface
28 * whenever an event is available.
29 *
30 * @param <T> the value type
31 */
32 public final class InnerQueuedObserver<T>
33 extends AtomicReference<Disposable>
34 implements Observer<T>, Disposable {
35
36 private static final long serialVersionUID = -5417183359794346637L;
37
38 final InnerQueuedObserverSupport<T> parent;
39
40 final int prefetch;
41
42 SimpleQueue<T> queue;
43
44 volatile boolean done;
45
46 int fusionMode;
47
48 public InnerQueuedObserver(InnerQueuedObserverSupport<T> parent, int prefetch) {
49 this.parent = parent;
50 this.prefetch = prefetch;
51 }
52
53 @Override
54 public void onSubscribe(Disposable d) {
55 if (DisposableHelper.setOnce(this, d)) {
56 if (d instanceof QueueDisposable) {
57 @SuppressWarnings("unchecked")
58 QueueDisposable<T> qd = (QueueDisposable<T>) d;
59
60 int m = qd.requestFusion(QueueDisposable.ANY);
61 if (m == QueueSubscription.SYNC) {
62 fusionMode = m;
63 queue = qd;
64 done = true;
65 parent.innerComplete(this);
66 return;
67 }
68 if (m == QueueDisposable.ASYNC) {
69 fusionMode = m;
70 queue = qd;
71 return;
72 }
73 }
74
75 queue = QueueDrainHelper.createQueue(-prefetch);
76 }
77 }
78
79 @Override
80 public void onNext(T t) {
81 if (fusionMode == QueueDisposable.NONE) {
82 parent.innerNext(this, t);
83 } else {
84 parent.drain();
85 }
86 }
87
88 @Override
89 public void onError(Throwable t) {
90 parent.innerError(this, t);
91 }
92
93 @Override
94 public void onComplete() {
95 parent.innerComplete(this);
96 }
97
98 @Override
99 public void dispose() {
100 DisposableHelper.dispose(this);
101 }
102
103 @Override
104 public boolean isDisposed() {
105 return DisposableHelper.isDisposed(get());
106 }
107
108 public boolean isDone() {
109 return done;
110 }
111
112 public void setDone() {
113 this.done = true;
114 }
115
116 public SimpleQueue<T> queue() {
117 return queue;
118 }
119 }