Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/operators/single/SingleFlatMapIterableObservable.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.operators.single;
15
16 import java.util.Iterator;
17 import java.util.Objects;
18
19 import io.reactivex.rxjava3.annotations.Nullable;
20 import io.reactivex.rxjava3.core.*;
21 import io.reactivex.rxjava3.disposables.Disposable;
22 import io.reactivex.rxjava3.exceptions.Exceptions;
23 import io.reactivex.rxjava3.functions.Function;
24 import io.reactivex.rxjava3.internal.disposables.DisposableHelper;
25 import io.reactivex.rxjava3.internal.observers.BasicIntQueueDisposable;
26
27 /**
28 * Maps a success value into an Iterable and streams it back as an Observable.
29 *
30 * @param <T> the source value type
31 * @param <R> the element type of the Iterable
32 */
33 public final class SingleFlatMapIterableObservable<T, R> extends Observable<R> {
34
35 final SingleSource<T> source;
36
37 final Function<? super T, ? extends Iterable<? extends R>> mapper;
38
39 public SingleFlatMapIterableObservable(SingleSource<T> source,
40 Function<? super T, ? extends Iterable<? extends R>> mapper) {
41 this.source = source;
42 this.mapper = mapper;
43 }
44
45 @Override
46 protected void subscribeActual(Observer<? super R> observer) {
47 source.subscribe(new FlatMapIterableObserver<>(observer, mapper));
48 }
49
50 static final class FlatMapIterableObserver<T, R>
51 extends BasicIntQueueDisposable<R>
52 implements SingleObserver<T> {
53
54 private static final long serialVersionUID = -8938804753851907758L;
55
56 final Observer<? super R> downstream;
57
58 final Function<? super T, ? extends Iterable<? extends R>> mapper;
59
60 Disposable upstream;
61
62 volatile Iterator<? extends R> it;
63
64 volatile boolean cancelled;
65
66 boolean outputFused;
67
68 FlatMapIterableObserver(Observer<? super R> actual,
69 Function<? super T, ? extends Iterable<? extends R>> mapper) {
70 this.downstream = actual;
71 this.mapper = mapper;
72 }
73
74 @Override
75 public void onSubscribe(Disposable d) {
76 if (DisposableHelper.validate(this.upstream, d)) {
77 this.upstream = d;
78
79 downstream.onSubscribe(this);
80 }
81 }
82
83 @Override
84 public void onSuccess(T value) {
85 Observer<? super R> a = downstream;
86 Iterator<? extends R> iterator;
87 boolean has;
88 try {
89 iterator = mapper.apply(value).iterator();
90
91 has = iterator.hasNext();
92 } catch (Throwable ex) {
93 Exceptions.throwIfFatal(ex);
94 downstream.onError(ex);
95 return;
96 }
97
98 if (!has) {
99 a.onComplete();
100 return;
101 }
102
103 if (outputFused) {
104 it = iterator;
105 a.onNext(null);
106 a.onComplete();
107 } else {
108 for (;;) {
109 if (cancelled) {
110 return;
111 }
112
113 R v;
114
115 try {
116 v = iterator.next();
117 } catch (Throwable ex) {
118 Exceptions.throwIfFatal(ex);
119 a.onError(ex);
120 return;
121 }
122
123 a.onNext(v);
124
125 if (cancelled) {
126 return;
127 }
128
129 boolean b;
130
131 try {
132 b = iterator.hasNext();
133 } catch (Throwable ex) {
134 Exceptions.throwIfFatal(ex);
135 a.onError(ex);
136 return;
137 }
138
139 if (!b) {
140 a.onComplete();
141 return;
142 }
143 }
144 }
145 }
146
147 @Override
148 public void onError(Throwable e) {
149 upstream = DisposableHelper.DISPOSED;
150 downstream.onError(e);
151 }
152
153 @Override
154 public void dispose() {
155 cancelled = true;
156 upstream.dispose();
157 upstream = DisposableHelper.DISPOSED;
158 }
159
160 @Override
161 public boolean isDisposed() {
162 return cancelled;
163 }
164
165 @Override
166 public int requestFusion(int mode) {
167 if ((mode & ASYNC) != 0) {
168 outputFused = true;
169 return ASYNC;
170 }
171 return NONE;
172 }
173
174 @Override
175 public void clear() {
176 it = null;
177 }
178
179 @Override
180 public boolean isEmpty() {
181 return it == null;
182 }
183
184 @Nullable
185 @Override
186 public R poll() {
187 Iterator<? extends R> iterator = it;
188
189 if (iterator != null) {
190 R v = Objects.requireNonNull(iterator.next(), "The iterator returned a null value");
191 if (!iterator.hasNext()) {
192 it = null;
193 }
194 return v;
195 }
196 return null;
197 }
198
199 }
200 }