Debian Codegraph @

src/main/java/io/reactivex/rxjava3/observers/SerializedObserver.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.*;
21 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
22
23 /**
24 * Serializes access to the {@link Observer#onNext(Object)}, {@link Observer#onError(Throwable)} and
25 * {@link Observer#onComplete()} methods of another {@link Observer}.
26 *
27 * <p>Note that {@link #onSubscribe(Disposable)} is not serialized in respect of the other methods so
28 * make sure the {@code onSubscribe()} is called with a non-null {@link Disposable}
29 * before any of the other methods are called.
30 *
31 * <p>The implementation assumes that the actual {@code Observer}'s methods don't throw.
32 *
33 * @param <T> the value type
34 */
35 public final class SerializedObserver<T> implements Observer<T>, Disposable {
36 final Observer<? super T> downstream;
37 final boolean delayError;
38
39 static final int QUEUE_LINK_SIZE = 4;
40
41 Disposable upstream;
42
43 boolean emitting;
44 AppendOnlyLinkedArrayList<Object> queue;
45
46 volatile boolean done;
47
48 /**
49 * Construct a {@code SerializedObserver} by wrapping the given actual {@link Observer}.
50 * @param downstream the actual {@code Observer}, not {@code null} (not verified)
51 */
52 public SerializedObserver(@NonNull Observer<? super T> downstream) {
53 this(downstream, false);
54 }
55
56 /**
57 * Construct a SerializedObserver by wrapping the given actual {@link Observer} and
58 * optionally delaying the errors till all regular values have been emitted
59 * from the internal buffer.
60 * @param actual the actual {@code Observer}, not {@code null} (not verified)
61 * @param delayError if {@code true}, errors are emitted after regular values have been emitted
62 */
63 public SerializedObserver(@NonNull Observer<? super T> actual, boolean delayError) {
64 this.downstream = actual;
65 this.delayError = delayError;
66 }
67
68 @Override
69 public void onSubscribe(@NonNull Disposable d) {
70 if (DisposableHelper.validate(this.upstream, d)) {
71 this.upstream = d;
72
73 downstream.onSubscribe(this);
74 }
75 }
76
77 @Override
78 public void dispose() {
79 done = true;
80 upstream.dispose();
81 }
82
83 @Override
84 public boolean isDisposed() {
85 return upstream.isDisposed();
86 }
87
88 @Override
89 public void onNext(@NonNull T t) {
90 if (done) {
91 return;
92 }
93 if (t == null) {
94 upstream.dispose();
95 onError(ExceptionHelper.createNullPointerException("onNext called with a null value."));
96 return;
97 }
98 synchronized (this) {
99 if (done) {
100 return;
101 }
102 if (emitting) {
103 AppendOnlyLinkedArrayList<Object> q = queue;
104 if (q == null) {
105 q = new AppendOnlyLinkedArrayList<>(QUEUE_LINK_SIZE);
106 queue = q;
107 }
108 q.add(NotificationLite.next(t));
109 return;
110 }
111 emitting = true;
112 }
113
114 downstream.onNext(t);
115
116 emitLoop();
117 }
118
119 @Override
120 public void onError(@NonNull Throwable t) {
121 if (done) {
122 RxJavaPlugins.onError(t);
123 return;
124 }
125 boolean reportError;
126 synchronized (this) {
127 if (done) {
128 reportError = true;
129 } else
130 if (emitting) {
131 done = true;
132 AppendOnlyLinkedArrayList<Object> q = queue;
133 if (q == null) {
134 q = new AppendOnlyLinkedArrayList<>(QUEUE_LINK_SIZE);
135 queue = q;
136 }
137 Object err = NotificationLite.error(t);
138 if (delayError) {
139 q.add(err);
140 } else {
141 q.setFirst(err);
142 }
143 return;
144 } else {
145 done = true;
146 emitting = true;
147 reportError = false;
148 }
149 }
150
151 if (reportError) {
152 RxJavaPlugins.onError(t);
153 return;
154 }
155
156 downstream.onError(t);
157 // no need to loop because this onError is the last event
158 }
159
160 @Override
161 public void onComplete() {
162 if (done) {
163 return;
164 }
165 synchronized (this) {
166 if (done) {
167 return;
168 }
169 if (emitting) {
170 AppendOnlyLinkedArrayList<Object> q = queue;
171 if (q == null) {
172 q = new AppendOnlyLinkedArrayList<>(QUEUE_LINK_SIZE);
173 queue = q;
174 }
175 q.add(NotificationLite.complete());
176 return;
177 }
178 done = true;
179 emitting = true;
180 }
181
182 downstream.onComplete();
183 // no need to loop because this onComplete is the last event
184 }
185
186 void emitLoop() {
187 for (;;) {
188 AppendOnlyLinkedArrayList<Object> q;
189 synchronized (this) {
190 q = queue;
191 if (q == null) {
192 emitting = false;
193 return;
194 }
195 queue = null;
196 }
197
198 if (q.accept(downstream)) {
199 return;
200 }
201 }
202 }
203 }