| 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 java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 23 |
import io.reactivex.rxjava3.functions.*; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 25 |
import io.reactivex.rxjava3.internal.util.BackpressureHelper; |
| 26 |
import io.reactivex.rxjava3.operators.SimplePlainQueue; |
| 27 |
import io.reactivex.rxjava3.operators.SpscArrayQueue; |
| 28 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 29 |
|
| 30 |
public final class FlowableScanSeed<T, R> extends AbstractFlowableWithUpstream<T, R> { |
| 31 |
final BiFunction<R, ? super T, R> accumulator; |
| 32 |
final Supplier<R> seedSupplier; |
| 33 |
|
| 34 |
public FlowableScanSeed(Flowable<T> source, Supplier<R> seedSupplier, BiFunction<R, ? super T, R> accumulator) { |
| 35 |
super(source); |
| 36 |
this.accumulator = accumulator; |
| 37 |
this.seedSupplier = seedSupplier; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
protected void subscribeActual(Subscriber<? super R> s) { |
| 42 |
R r; |
| 43 |
|
| 44 |
try { |
| 45 |
r = Objects.requireNonNull(seedSupplier.get(), "The seed supplied is null"); |
| 46 |
} catch (Throwable e) { |
| 47 |
Exceptions.throwIfFatal(e); |
| 48 |
EmptySubscription.error(e, s); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
source.subscribe(new ScanSeedSubscriber<>(s, accumulator, r, bufferSize())); |
| 53 |
} |
| 54 |
|
| 55 |
static final class ScanSeedSubscriber<T, R> |
| 56 |
extends AtomicInteger |
| 57 |
implements FlowableSubscriber<T>, Subscription { |
| 58 |
private static final long serialVersionUID = -1776795561228106469L; |
| 59 |
|
| 60 |
final Subscriber<? super R> downstream; |
| 61 |
|
| 62 |
final BiFunction<R, ? super T, R> accumulator; |
| 63 |
|
| 64 |
final SimplePlainQueue<R> queue; |
| 65 |
|
| 66 |
final AtomicLong requested; |
| 67 |
|
| 68 |
final int prefetch; |
| 69 |
|
| 70 |
final int limit; |
| 71 |
|
| 72 |
volatile boolean cancelled; |
| 73 |
|
| 74 |
volatile boolean done; |
| 75 |
Throwable error; |
| 76 |
|
| 77 |
Subscription upstream; |
| 78 |
|
| 79 |
R value; |
| 80 |
|
| 81 |
int consumed; |
| 82 |
|
| 83 |
ScanSeedSubscriber(Subscriber<? super R> actual, BiFunction<R, ? super T, R> accumulator, R value, int prefetch) { |
| 84 |
this.downstream = actual; |
| 85 |
this.accumulator = accumulator; |
| 86 |
this.value = value; |
| 87 |
this.prefetch = prefetch; |
| 88 |
this.limit = prefetch - (prefetch >> 2); |
| 89 |
this.queue = new SpscArrayQueue<>(prefetch); |
| 90 |
this.queue.offer(value); |
| 91 |
this.requested = new AtomicLong(); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onSubscribe(Subscription s) { |
| 96 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 97 |
this.upstream = s; |
| 98 |
|
| 99 |
downstream.onSubscribe(this); |
| 100 |
|
| 101 |
s.request(prefetch - 1); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onNext(T t) { |
| 107 |
if (done) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
R v = value; |
| 112 |
try { |
| 113 |
v = Objects.requireNonNull(accumulator.apply(v, t), "The accumulator returned a null value"); |
| 114 |
} catch (Throwable ex) { |
| 115 |
Exceptions.throwIfFatal(ex); |
| 116 |
upstream.cancel(); |
| 117 |
onError(ex); |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
value = v; |
| 122 |
queue.offer(v); |
| 123 |
drain(); |
| 124 |
} |
| 125 |
|
| 126 |
@Override |
| 127 |
public void onError(Throwable t) { |
| 128 |
if (done) { |
| 129 |
RxJavaPlugins.onError(t); |
| 130 |
return; |
| 131 |
} |
| 132 |
error = t; |
| 133 |
done = true; |
| 134 |
drain(); |
| 135 |
} |
| 136 |
|
| 137 |
@Override |
| 138 |
public void onComplete() { |
| 139 |
if (done) { |
| 140 |
return; |
| 141 |
} |
| 142 |
done = true; |
| 143 |
drain(); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public void cancel() { |
| 148 |
cancelled = true; |
| 149 |
upstream.cancel(); |
| 150 |
if (getAndIncrement() == 0) { |
| 151 |
queue.clear(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void request(long n) { |
| 157 |
if (SubscriptionHelper.validate(n)) { |
| 158 |
BackpressureHelper.add(requested, n); |
| 159 |
drain(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void drain() { |
| 164 |
if (getAndIncrement() != 0) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
int missed = 1; |
| 169 |
Subscriber<? super R> a = downstream; |
| 170 |
SimplePlainQueue<R> q = queue; |
| 171 |
int lim = limit; |
| 172 |
int c = consumed; |
| 173 |
|
| 174 |
for (;;) { |
| 175 |
|
| 176 |
long r = requested.get(); |
| 177 |
long e = 0L; |
| 178 |
|
| 179 |
while (e != r) { |
| 180 |
if (cancelled) { |
| 181 |
q.clear(); |
| 182 |
return; |
| 183 |
} |
| 184 |
boolean d = done; |
| 185 |
|
| 186 |
if (d) { |
| 187 |
Throwable ex = error; |
| 188 |
if (ex != null) { |
| 189 |
q.clear(); |
| 190 |
a.onError(ex); |
| 191 |
return; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
R v = q.poll(); |
| 196 |
boolean empty = v == null; |
| 197 |
|
| 198 |
if (d && empty) { |
| 199 |
a.onComplete(); |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
if (empty) { |
| 204 |
break; |
| 205 |
} |
| 206 |
|
| 207 |
a.onNext(v); |
| 208 |
|
| 209 |
e++; |
| 210 |
if (++c == lim) { |
| 211 |
c = 0; |
| 212 |
upstream.request(lim); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if (e == r) { |
| 217 |
if (done) { |
| 218 |
Throwable ex = error; |
| 219 |
if (ex != null) { |
| 220 |
q.clear(); |
| 221 |
a.onError(ex); |
| 222 |
return; |
| 223 |
} |
| 224 |
if (q.isEmpty()) { |
| 225 |
a.onComplete(); |
| 226 |
return; |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
if (e != 0L) { |
| 232 |
BackpressureHelper.produced(requested, e); |
| 233 |
} |
| 234 |
|
| 235 |
consumed = c; |
| 236 |
missed = addAndGet(-missed); |
| 237 |
if (missed == 0) { |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
} |