-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathSingleFlatMapIterableFlowable.java
291 lines (229 loc) · 7.83 KB
/
SingleFlatMapIterableFlowable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
* Copyright (c) 2016-present, RxJava Contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
* the License for the specific language governing permissions and limitations under the License.
*/
package io.reactivex.internal.operators.single;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;
import io.reactivex.annotations.Nullable;
import org.reactivestreams.Subscriber;
import io.reactivex.*;
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.disposables.DisposableHelper;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.internal.util.BackpressureHelper;
/**
* Maps a success value into an Iterable and streams it back as a Flowable.
*
* @param <T> the source value type
* @param <R> the element type of the Iterable
*/
public final class SingleFlatMapIterableFlowable<T, R> extends Flowable<R> {
final SingleSource<T> source;
final Function<? super T, ? extends Iterable<? extends R>> mapper;
public SingleFlatMapIterableFlowable(SingleSource<T> source,
Function<? super T, ? extends Iterable<? extends R>> mapper) {
this.source = source;
this.mapper = mapper;
}
@Override
protected void subscribeActual(Subscriber<? super R> s) {
source.subscribe(new FlatMapIterableObserver<T, R>(s, mapper));
}
static final class FlatMapIterableObserver<T, R>
extends BasicIntQueueSubscription<R>
implements SingleObserver<T> {
private static final long serialVersionUID = -8938804753851907758L;
final Subscriber<? super R> actual;
final Function<? super T, ? extends Iterable<? extends R>> mapper;
final AtomicLong requested;
Disposable d;
volatile Iterator<? extends R> it;
volatile boolean cancelled;
boolean outputFused;
FlatMapIterableObserver(Subscriber<? super R> actual,
Function<? super T, ? extends Iterable<? extends R>> mapper) {
this.actual = actual;
this.mapper = mapper;
this.requested = new AtomicLong();
}
@Override
public void onSubscribe(Disposable d) {
if (DisposableHelper.validate(this.d, d)) {
this.d = d;
actual.onSubscribe(this);
}
}
@Override
public void onSuccess(T value) {
Iterator<? extends R> iterator;
boolean has;
try {
iterator = mapper.apply(value).iterator();
has = iterator.hasNext();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
actual.onError(ex);
return;
}
if (!has) {
actual.onComplete();
return;
}
this.it = iterator;
drain();
}
@Override
public void onError(Throwable e) {
d = DisposableHelper.DISPOSED;
actual.onError(e);
}
@Override
public void request(long n) {
if (SubscriptionHelper.validate(n)) {
BackpressureHelper.add(requested, n);
drain();
}
}
@Override
public void cancel() {
cancelled = true;
d.dispose();
d = DisposableHelper.DISPOSED;
}
void drain() {
if (getAndIncrement() != 0) {
return;
}
Subscriber<? super R> a = actual;
Iterator<? extends R> iterator = this.it;
if (outputFused && iterator != null) {
a.onNext(null);
a.onComplete();
return;
}
int missed = 1;
for (;;) {
if (iterator != null) {
long r = requested.get();
long e = 0L;
if (r == Long.MAX_VALUE) {
slowPath(a, iterator);
return;
}
while (e != r) {
if (cancelled) {
return;
}
R v;
try {
v = ObjectHelper.requireNonNull(iterator.next(), "The iterator returned a null value");
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
a.onError(ex);
return;
}
a.onNext(v);
if (cancelled) {
return;
}
e++;
boolean b;
try {
b = iterator.hasNext();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
a.onError(ex);
return;
}
if (!b) {
a.onComplete();
return;
}
}
if (e != 0L) {
BackpressureHelper.produced(requested, e);
}
}
missed = addAndGet(-missed);
if (missed == 0) {
break;
}
if (iterator == null) {
iterator = it;
}
}
}
void slowPath(Subscriber<? super R> a, Iterator<? extends R> iterator) {
for (;;) {
if (cancelled) {
return;
}
R v;
try {
v = iterator.next();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
a.onError(ex);
return;
}
a.onNext(v);
if (cancelled) {
return;
}
boolean b;
try {
b = iterator.hasNext();
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
a.onError(ex);
return;
}
if (!b) {
a.onComplete();
return;
}
}
}
@Override
public int requestFusion(int mode) {
if ((mode & ASYNC) != 0) {
outputFused = true;
return ASYNC;
}
return NONE;
}
@Override
public void clear() {
it = null;
}
@Override
public boolean isEmpty() {
return it == null;
}
@Nullable
@Override
public R poll() throws Exception {
Iterator<? extends R> iterator = it;
if (iterator != null) {
R v = ObjectHelper.requireNonNull(iterator.next(), "The iterator returned a null value");
if (!iterator.hasNext()) {
it = null;
}
return v;
}
return null;
}
}
}