Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/jdk8/FlowableFromStream.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.jdk8;
15
16 import java.util.Iterator;
17 import java.util.Objects;
18 import java.util.concurrent.atomic.AtomicLong;
19 import java.util.stream.Stream;
20
21 import org.reactivestreams.Subscriber;
22
23 import io.reactivex.rxjava3.annotations.*;
24 import io.reactivex.rxjava3.core.Flowable;
25 import io.reactivex.rxjava3.exceptions.Exceptions;
26 import io.reactivex.rxjava3.internal.subscriptions.*;
27 import io.reactivex.rxjava3.internal.util.BackpressureHelper;
28 import io.reactivex.rxjava3.operators.ConditionalSubscriber;
29 import io.reactivex.rxjava3.operators.QueueSubscription;
30 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
31
32 /**
33 * Wraps a {@link Stream} and emits its values as a Flowable sequence.
34 * @param <T> the element type of the Stream
35 * @since 3.0.0
36 */
37 public final class FlowableFromStream<T> extends Flowable<T> {
38
39 final Stream<T> stream;
40
41 public FlowableFromStream(Stream<T> stream) {
42 this.stream = stream;
43 }
44
45 @Override
46 protected void subscribeActual(Subscriber<? super T> s) {
47 subscribeStream(s, stream);
48 }
49
50 /**
51 * Subscribes to the Stream by picking the normal or conditional stream Subscription implementation.
52 * @param <T> the element type of the flow
53 * @param s the subscriber to drive
54 * @param stream the sequence to consume
55 */
56 public static <T> void subscribeStream(Subscriber<? super T> s, Stream<T> stream) {
57 Iterator<T> iterator;
58 try {
59 iterator = stream.iterator();
60
61 if (!iterator.hasNext()) {
62 EmptySubscription.complete(s);
63 closeSafely(stream);
64 return;
65 }
66 } catch (Throwable ex) {
67 Exceptions.throwIfFatal(ex);
68 EmptySubscription.error(ex, s);
69 closeSafely(stream);
70 return;
71 }
72
73 if (s instanceof ConditionalSubscriber) {
74 s.onSubscribe(new StreamConditionalSubscription<>((ConditionalSubscriber<? super T>)s, iterator, stream));
75 } else {
76 s.onSubscribe(new StreamSubscription<>(s, iterator, stream));
77 }
78 }
79
80 static void closeSafely(AutoCloseable c) {
81 try {
82 c.close();
83 } catch (Throwable ex) {
84 Exceptions.throwIfFatal(ex);
85 RxJavaPlugins.onError(ex);
86 }
87 }
88
89 abstract static class AbstractStreamSubscription<T> extends AtomicLong implements QueueSubscription<T> {
90
91 private static final long serialVersionUID = -9082954702547571853L;
92
93 Iterator<T> iterator;
94
95 AutoCloseable closeable;
96
97 volatile boolean cancelled;
98
99 boolean once;
100
101 AbstractStreamSubscription(Iterator<T> iterator, AutoCloseable closeable) {
102 this.iterator = iterator;
103 this.closeable = closeable;
104 }
105
106 @Override
107 public void request(long n) {
108 if (SubscriptionHelper.validate(n)) {
109 if (BackpressureHelper.add(this, n) == 0L) {
110 run(n);
111 }
112 }
113 }
114
115 abstract void run(long n);
116
117 @Override
118 public void cancel() {
119 cancelled = true;
120 request(1L);
121 }
122
123 @Override
124 public int requestFusion(int mode) {
125 if ((mode & SYNC) != 0) {
126 lazySet(Long.MAX_VALUE);
127 return SYNC;
128 }
129 return NONE;
130 }
131
132 @Override
133 public boolean offer(@NonNull T value) {
134 throw new UnsupportedOperationException();
135 }
136
137 @Override
138 public boolean offer(@NonNull T v1, @NonNull T v2) {
139 throw new UnsupportedOperationException();
140 }
141
142 @Nullable
143 @Override
144 public T poll() {
145 if (iterator == null) {
146 return null;
147 }
148 if (!once) {
149 once = true;
150 } else {
151 if (!iterator.hasNext()) {
152 clear();
153 return null;
154 }
155 }
156 return Objects.requireNonNull(iterator.next(), "The Stream's Iterator.next() returned a null value");
157 }
158
159 @Override
160 public boolean isEmpty() {
161 Iterator<T> it = iterator;
162 if (it != null) {
163 if (!once || it.hasNext()) {
164 return false;
165 }
166 clear();
167 }
168 return true;
169 }
170
171 @Override
172 public void clear() {
173 iterator = null;
174 AutoCloseable c = closeable;
175 closeable = null;
176 if (c != null) {
177 closeSafely(c);
178 }
179 }
180 }
181
182 static final class StreamSubscription<T> extends AbstractStreamSubscription<T> {
183
184 private static final long serialVersionUID = -9082954702547571853L;
185
186 final Subscriber<? super T> downstream;
187
188 StreamSubscription(Subscriber<? super T> downstream, Iterator<T> iterator, AutoCloseable closeable) {
189 super(iterator, closeable);
190 this.downstream = downstream;
191 }
192
193 @Override
194 public void run(long n) {
195 long emitted = 0L;
196 Iterator<T> iterator = this.iterator;
197 Subscriber<? super T> downstream = this.downstream;
198
199 for (;;) {
200
201 if (cancelled) {
202 clear();
203 break;
204 } else {
205 T next;
206 try {
207 next = Objects.requireNonNull(iterator.next(), "The Stream's Iterator returned a null value");
208 } catch (Throwable ex) {
209 Exceptions.throwIfFatal(ex);
210 downstream.onError(ex);
211 cancelled = true;
212 continue;
213 }
214
215 downstream.onNext(next);
216
217 if (cancelled) {
218 continue;
219 }
220
221 try {
222 if (!iterator.hasNext()) {
223 downstream.onComplete();
224 cancelled = true;
225 continue;
226 }
227 } catch (Throwable ex) {
228 Exceptions.throwIfFatal(ex);
229 downstream.onError(ex);
230 cancelled = true;
231 continue;
232 }
233
234 if (++emitted != n) {
235 continue;
236 }
237 }
238
239 n = get();
240 if (emitted == n) {
241 if (compareAndSet(n, 0L)) {
242 break;
243 }
244 n = get();
245 }
246 }
247 }
248 }
249
250 static final class StreamConditionalSubscription<T> extends AbstractStreamSubscription<T> {
251
252 private static final long serialVersionUID = -9082954702547571853L;
253
254 final ConditionalSubscriber<? super T> downstream;
255
256 StreamConditionalSubscription(ConditionalSubscriber<? super T> downstream, Iterator<T> iterator, AutoCloseable closeable) {
257 super(iterator, closeable);
258 this.downstream = downstream;
259 }
260
261 @Override
262 public void run(long n) {
263 long emitted = 0L;
264 Iterator<T> iterator = this.iterator;
265 ConditionalSubscriber<? super T> downstream = this.downstream;
266
267 for (;;) {
268
269 if (cancelled) {
270 clear();
271 break;
272 } else {
273 T next;
274 try {
275 next = Objects.requireNonNull(iterator.next(), "The Stream's Iterator returned a null value");
276 } catch (Throwable ex) {
277 Exceptions.throwIfFatal(ex);
278 downstream.onError(ex);
279 cancelled = true;
280 continue;
281 }
282
283 if (downstream.tryOnNext(next)) {
284 emitted++;
285 }
286
287 if (cancelled) {
288 continue;
289 }
290
291 try {
292 if (!iterator.hasNext()) {
293 downstream.onComplete();
294 cancelled = true;
295 continue;
296 }
297 } catch (Throwable ex) {
298 Exceptions.throwIfFatal(ex);
299 downstream.onError(ex);
300 cancelled = true;
301 continue;
302 }
303
304 if (emitted != n) {
305 continue;
306 }
307 }
308
309 n = get();
310 if (emitted == n) {
311 if (compareAndSet(n, 0L)) {
312 break;
313 }
314 n = get();
315 }
316 }
317 }
318 }
319 }