|
| 1 | +/* |
| 2 | + * Copyright 2016-2017 David Karnok |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package hu.akarnokd.rxjava2.operators; |
| 18 | + |
| 19 | +import java.util.concurrent.Callable; |
| 20 | +import java.util.concurrent.atomic.*; |
| 21 | + |
| 22 | +import org.reactivestreams.*; |
| 23 | + |
| 24 | +import io.reactivex.*; |
| 25 | +import io.reactivex.exceptions.Exceptions; |
| 26 | +import io.reactivex.functions.BiConsumer; |
| 27 | +import io.reactivex.internal.fuseable.SimplePlainQueue; |
| 28 | +import io.reactivex.internal.queue.SpscLinkedArrayQueue; |
| 29 | +import io.reactivex.internal.subscriptions.SubscriptionHelper; |
| 30 | +import io.reactivex.internal.util.BackpressureHelper; |
| 31 | + |
| 32 | +/** |
| 33 | + * Coalesces items into a container if the downstream is not ready to receive items. |
| 34 | + * |
| 35 | + * @param <T> the upstream element type |
| 36 | + * @param <R> the container type emitted to downstream |
| 37 | + * |
| 38 | + * @since 0.17.3 |
| 39 | + */ |
| 40 | +final class FlowableCoalesce<T, R> extends Flowable<R> implements FlowableTransformer<T, R> { |
| 41 | + |
| 42 | + final Publisher<T> source; |
| 43 | + |
| 44 | + final Callable<R> containerSupplier; |
| 45 | + |
| 46 | + final BiConsumer<R, T> coalescer; |
| 47 | + |
| 48 | + final int bufferSize; |
| 49 | + |
| 50 | + FlowableCoalesce(Publisher<T> source, Callable<R> containerSupplier, BiConsumer<R, T> coalescer, int bufferSize) { |
| 51 | + this.source = source; |
| 52 | + this.containerSupplier = containerSupplier; |
| 53 | + this.coalescer = coalescer; |
| 54 | + this.bufferSize = bufferSize; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Publisher<R> apply(Flowable<T> upstream) { |
| 59 | + return new FlowableCoalesce<T, R>(upstream, containerSupplier, coalescer, bufferSize); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void subscribeActual(Subscriber<? super R> s) { |
| 64 | + source.subscribe(new CoalesceSubscriber<T, R>(s, containerSupplier, coalescer, bufferSize)); |
| 65 | + } |
| 66 | + |
| 67 | + static final class CoalesceSubscriber<T, R> extends AtomicInteger |
| 68 | + implements FlowableSubscriber<T>, Subscription { |
| 69 | + |
| 70 | + private static final long serialVersionUID = -6157179110480235565L; |
| 71 | + |
| 72 | + final Subscriber<? super R> actual; |
| 73 | + |
| 74 | + final Callable<R> containerSupplier; |
| 75 | + |
| 76 | + final BiConsumer<R, T> coalescer; |
| 77 | + |
| 78 | + final AtomicLong requested; |
| 79 | + |
| 80 | + final int bufferSize; |
| 81 | + |
| 82 | + volatile SimplePlainQueue<T> queue; |
| 83 | + |
| 84 | + Subscription upstream; |
| 85 | + |
| 86 | + R container; |
| 87 | + |
| 88 | + volatile boolean done; |
| 89 | + |
| 90 | + volatile boolean cancelled; |
| 91 | + |
| 92 | + Throwable error; |
| 93 | + |
| 94 | + long emitted; |
| 95 | + |
| 96 | + CoalesceSubscriber(Subscriber<? super R> actual, Callable<R> containerSupplier, |
| 97 | + BiConsumer<R, T> coalescer, int bufferSize) { |
| 98 | + this.actual = actual; |
| 99 | + this.containerSupplier = containerSupplier; |
| 100 | + this.coalescer = coalescer; |
| 101 | + this.requested = new AtomicLong(); |
| 102 | + this.bufferSize = bufferSize; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onSubscribe(Subscription s) { |
| 107 | + if (SubscriptionHelper.validate(upstream, s)) { |
| 108 | + upstream = s; |
| 109 | + actual.onSubscribe(this); |
| 110 | + |
| 111 | + s.request(Long.MAX_VALUE); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onNext(T t) { |
| 117 | + if (get() == 0 && compareAndSet(0, 1)) { |
| 118 | + SimplePlainQueue<T> q = queue; |
| 119 | + if (q == null || q.isEmpty()) { |
| 120 | + R c = container; |
| 121 | + try { |
| 122 | + if (c == null) { |
| 123 | + c = containerSupplier.call(); |
| 124 | + container = c; |
| 125 | + } |
| 126 | + coalescer.accept(c, t); |
| 127 | + } catch (Throwable ex) { |
| 128 | + Exceptions.throwIfFatal(ex); |
| 129 | + upstream.cancel(); |
| 130 | + container = null; |
| 131 | + actual.onError(ex); |
| 132 | + return; |
| 133 | + } |
| 134 | + long r = requested.get(); |
| 135 | + long e = emitted; |
| 136 | + if (e != r) { |
| 137 | + container = null; |
| 138 | + actual.onNext(c); |
| 139 | + emitted = e + 1; |
| 140 | + } |
| 141 | + if (decrementAndGet() == 0) { |
| 142 | + return; |
| 143 | + } |
| 144 | + } |
| 145 | + } else { |
| 146 | + SimplePlainQueue<T> q = queue; |
| 147 | + if (q == null) { |
| 148 | + q = new SpscLinkedArrayQueue<T>(bufferSize); |
| 149 | + queue = q; |
| 150 | + } |
| 151 | + q.offer(t); |
| 152 | + if (getAndIncrement() != 0) { |
| 153 | + return; |
| 154 | + } |
| 155 | + } |
| 156 | + drainLoop(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void onError(Throwable t) { |
| 161 | + error = t; |
| 162 | + done = true; |
| 163 | + drain(); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void onComplete() { |
| 168 | + done = true; |
| 169 | + drain(); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public void request(long n) { |
| 174 | + if (SubscriptionHelper.validate(n)) { |
| 175 | + BackpressureHelper.add(requested, n); |
| 176 | + drain(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void cancel() { |
| 182 | + cancelled = true; |
| 183 | + upstream.cancel(); |
| 184 | + if (getAndIncrement() == 0) { |
| 185 | + container = null; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + void drain() { |
| 190 | + if (getAndIncrement() != 0) { |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + drainLoop(); |
| 195 | + } |
| 196 | + |
| 197 | + void drainLoop() { |
| 198 | + int missed = 1; |
| 199 | + long e = emitted; |
| 200 | + R c = container; |
| 201 | + Subscriber<? super R> a = actual; |
| 202 | + |
| 203 | + for (;;) { |
| 204 | + if (cancelled) { |
| 205 | + container = null; |
| 206 | + return; |
| 207 | + } |
| 208 | + boolean d = done; |
| 209 | + SimplePlainQueue<T> q = queue; |
| 210 | + boolean empty = q == null || q.isEmpty(); |
| 211 | + |
| 212 | + if (!empty) { |
| 213 | + try { |
| 214 | + if (c == null) { |
| 215 | + c = containerSupplier.call(); |
| 216 | + container = c; |
| 217 | + } |
| 218 | + |
| 219 | + for (;;) { |
| 220 | + T v = q.poll(); |
| 221 | + if (v == null) { |
| 222 | + break; |
| 223 | + } |
| 224 | + coalescer.accept(c, v); |
| 225 | + } |
| 226 | + } catch (Throwable ex) { |
| 227 | + Exceptions.throwIfFatal(ex); |
| 228 | + container = null; |
| 229 | + a.onError(ex); |
| 230 | + return; |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if (c != null && e != requested.get()) { |
| 235 | + a.onNext(c); |
| 236 | + c = null; |
| 237 | + container = null; |
| 238 | + e++; |
| 239 | + } |
| 240 | + |
| 241 | + if (d && c == null) { |
| 242 | + Throwable ex = error; |
| 243 | + container = null; |
| 244 | + if (ex != null) { |
| 245 | + a.onError(ex); |
| 246 | + } else { |
| 247 | + a.onComplete(); |
| 248 | + } |
| 249 | + return; |
| 250 | + } |
| 251 | + |
| 252 | + emitted = e; |
| 253 | + missed = addAndGet(-missed); |
| 254 | + if (missed == 0) { |
| 255 | + break; |
| 256 | + } |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments