| 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.flowable; |
| 15 |
|
| 16 |
import org.reactivestreams.Subscriber; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.Nullable; |
| 19 |
import io.reactivex.rxjava3.core.Flowable; |
| 20 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 21 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 22 |
import io.reactivex.rxjava3.operators.ConditionalSubscriber; |
| 23 |
|
| 24 |
/** |
| 25 |
* Emits a range of integer values. |
| 26 |
*/ |
| 27 |
public final class FlowableRange extends Flowable<Integer> { |
| 28 |
final int start; |
| 29 |
final int end; |
| 30 |
public FlowableRange(int start, int count) { |
| 31 |
this.start = start; |
| 32 |
this.end = start + count; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public void subscribeActual(Subscriber<? super Integer> s) { |
| 37 |
if (s instanceof ConditionalSubscriber) { |
| 38 |
s.onSubscribe(new RangeConditionalSubscription( |
| 39 |
(ConditionalSubscriber<? super Integer>)s, start, end)); |
| 40 |
} else { |
| 41 |
s.onSubscribe(new RangeSubscription(s, start, end)); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
abstract static class BaseRangeSubscription extends BasicQueueSubscription<Integer> { |
| 46 |
private static final long serialVersionUID = -2252972430506210021L; |
| 47 |
|
| 48 |
final int end; |
| 49 |
|
| 50 |
int index; |
| 51 |
|
| 52 |
volatile boolean cancelled; |
| 53 |
|
| 54 |
BaseRangeSubscription(int index, int end) { |
| 55 |
this.index = index; |
| 56 |
this.end = end; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public final int requestFusion(int mode) { |
| 61 |
return mode & SYNC; |
| 62 |
} |
| 63 |
|
| 64 |
@Nullable |
| 65 |
@Override |
| 66 |
public final Integer poll() { |
| 67 |
int i = index; |
| 68 |
if (i == end) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
index = i + 1; |
| 72 |
return i; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public final boolean isEmpty() { |
| 77 |
return index == end; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public final void clear() { |
| 82 |
index = end; |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public final void request(long n) { |
| 87 |
if (SubscriptionHelper.validate(n)) { |
| 88 |
if (BackpressureHelper.add(this, n) == 0L) { |
| 89 |
if (n == Long.MAX_VALUE) { |
| 90 |
fastPath(); |
| 91 |
} else { |
| 92 |
slowPath(n); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public final void cancel() { |
| 100 |
cancelled = true; |
| 101 |
} |
| 102 |
|
| 103 |
abstract void fastPath(); |
| 104 |
|
| 105 |
abstract void slowPath(long r); |
| 106 |
} |
| 107 |
|
| 108 |
static final class RangeSubscription extends BaseRangeSubscription { |
| 109 |
|
| 110 |
private static final long serialVersionUID = 2587302975077663557L; |
| 111 |
|
| 112 |
final Subscriber<? super Integer> downstream; |
| 113 |
|
| 114 |
RangeSubscription(Subscriber<? super Integer> actual, int index, int end) { |
| 115 |
super(index, end); |
| 116 |
this.downstream = actual; |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
void fastPath() { |
| 121 |
int f = end; |
| 122 |
Subscriber<? super Integer> a = downstream; |
| 123 |
|
| 124 |
for (int i = index; i != f; i++) { |
| 125 |
if (cancelled) { |
| 126 |
return; |
| 127 |
} |
| 128 |
a.onNext(i); |
| 129 |
} |
| 130 |
if (cancelled) { |
| 131 |
return; |
| 132 |
} |
| 133 |
a.onComplete(); |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
void slowPath(long r) { |
| 138 |
long e = 0; |
| 139 |
int f = end; |
| 140 |
int i = index; |
| 141 |
Subscriber<? super Integer> a = downstream; |
| 142 |
|
| 143 |
for (;;) { |
| 144 |
|
| 145 |
while (e != r && i != f) { |
| 146 |
if (cancelled) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
a.onNext(i); |
| 151 |
|
| 152 |
e++; |
| 153 |
i++; |
| 154 |
} |
| 155 |
|
| 156 |
if (i == f) { |
| 157 |
if (!cancelled) { |
| 158 |
a.onComplete(); |
| 159 |
} |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
r = get(); |
| 164 |
if (e == r) { |
| 165 |
index = i; |
| 166 |
r = addAndGet(-e); |
| 167 |
if (r == 0L) { |
| 168 |
return; |
| 169 |
} |
| 170 |
e = 0L; |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
static final class RangeConditionalSubscription extends BaseRangeSubscription { |
| 177 |
|
| 178 |
private static final long serialVersionUID = 2587302975077663557L; |
| 179 |
|
| 180 |
final ConditionalSubscriber<? super Integer> downstream; |
| 181 |
|
| 182 |
RangeConditionalSubscription(ConditionalSubscriber<? super Integer> actual, int index, int end) { |
| 183 |
super(index, end); |
| 184 |
this.downstream = actual; |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
void fastPath() { |
| 189 |
int f = end; |
| 190 |
ConditionalSubscriber<? super Integer> a = downstream; |
| 191 |
|
| 192 |
for (int i = index; i != f; i++) { |
| 193 |
if (cancelled) { |
| 194 |
return; |
| 195 |
} |
| 196 |
a.tryOnNext(i); |
| 197 |
} |
| 198 |
if (cancelled) { |
| 199 |
return; |
| 200 |
} |
| 201 |
a.onComplete(); |
| 202 |
} |
| 203 |
|
| 204 |
@Override |
| 205 |
void slowPath(long r) { |
| 206 |
long e = 0; |
| 207 |
int f = end; |
| 208 |
int i = index; |
| 209 |
ConditionalSubscriber<? super Integer> a = downstream; |
| 210 |
|
| 211 |
for (;;) { |
| 212 |
|
| 213 |
while (e != r && i != f) { |
| 214 |
if (cancelled) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if (a.tryOnNext(i)) { |
| 219 |
e++; |
| 220 |
} |
| 221 |
|
| 222 |
i++; |
| 223 |
} |
| 224 |
|
| 225 |
if (i == f) { |
| 226 |
if (!cancelled) { |
| 227 |
a.onComplete(); |
| 228 |
} |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
r = get(); |
| 233 |
if (e == r) { |
| 234 |
index = i; |
| 235 |
r = addAndGet(-e); |
| 236 |
if (r == 0) { |
| 237 |
return; |
| 238 |
} |
| 239 |
e = 0; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
} |