-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathFlowableRangeLong.java
248 lines (200 loc) · 6.31 KB
/
FlowableRangeLong.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
/**
* 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.flowable;
import org.reactivestreams.Subscriber;
import io.reactivex.Flowable;
import io.reactivex.annotations.Nullable;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.internal.util.BackpressureHelper;
/**
* Emits a range of long values.
*/
public final class FlowableRangeLong extends Flowable<Long> {
final long start;
final long end;
public FlowableRangeLong(long start, long count) {
this.start = start;
this.end = start + count;
}
@Override
public void subscribeActual(Subscriber<? super Long> s) {
if (s instanceof ConditionalSubscriber) {
s.onSubscribe(new RangeConditionalSubscription(
(ConditionalSubscriber<? super Long>)s, start, end));
} else {
s.onSubscribe(new RangeSubscription(s, start, end));
}
}
abstract static class BaseRangeSubscription extends BasicQueueSubscription<Long> {
private static final long serialVersionUID = -2252972430506210021L;
final long end;
long index;
volatile boolean cancelled;
BaseRangeSubscription(long index, long end) {
this.index = index;
this.end = end;
}
@Override
public final int requestFusion(int mode) {
return mode & SYNC;
}
@Nullable
@Override
public final Long poll() {
long i = index;
if (i == end) {
return null;
}
index = i + 1;
return i;
}
@Override
public final boolean isEmpty() {
return index == end;
}
@Override
public final void clear() {
index = end;
}
@Override
public final void request(long n) {
if (SubscriptionHelper.validate(n)) {
if (BackpressureHelper.add(this, n) == 0L) {
if (n == Long.MAX_VALUE) {
fastPath();
} else {
slowPath(n);
}
}
}
}
@Override
public final void cancel() {
cancelled = true;
}
abstract void fastPath();
abstract void slowPath(long r);
}
static final class RangeSubscription extends BaseRangeSubscription {
private static final long serialVersionUID = 2587302975077663557L;
final Subscriber<? super Long> actual;
RangeSubscription(Subscriber<? super Long> actual, long index, long end) {
super(index, end);
this.actual = actual;
}
@Override
void fastPath() {
long f = end;
Subscriber<? super Long> a = actual;
for (long i = index; i != f; i++) {
if (cancelled) {
return;
}
a.onNext(i);
}
if (cancelled) {
return;
}
a.onComplete();
}
@Override
void slowPath(long r) {
long e = 0;
long f = end;
long i = index;
Subscriber<? super Long> a = actual;
for (;;) {
while (e != r && i != f) {
if (cancelled) {
return;
}
a.onNext(i);
e++;
i++;
}
if (i == f) {
if (!cancelled) {
a.onComplete();
}
return;
}
r = get();
if (e == r) {
index = i;
r = addAndGet(-e);
if (r == 0L) {
return;
}
e = 0L;
}
}
}
}
static final class RangeConditionalSubscription extends BaseRangeSubscription {
private static final long serialVersionUID = 2587302975077663557L;
final ConditionalSubscriber<? super Long> actual;
RangeConditionalSubscription(ConditionalSubscriber<? super Long> actual, long index, long end) {
super(index, end);
this.actual = actual;
}
@Override
void fastPath() {
long f = end;
ConditionalSubscriber<? super Long> a = actual;
for (long i = index; i != f; i++) {
if (cancelled) {
return;
}
a.tryOnNext(i);
}
if (cancelled) {
return;
}
a.onComplete();
}
@Override
void slowPath(long r) {
long e = 0;
long f = end;
long i = index;
ConditionalSubscriber<? super Long> a = actual;
for (;;) {
while (e != r && i != f) {
if (cancelled) {
return;
}
if (a.tryOnNext(i)) {
e++;
}
i++;
}
if (i == f) {
if (!cancelled) {
a.onComplete();
}
return;
}
r = get();
if (e == r) {
index = i;
r = addAndGet(-e);
if (r == 0) {
return;
}
e = 0;
}
}
}
}
}