| 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.Collection; |
| 17 |
import java.util.Objects; |
| 18 |
|
| 19 |
import org.reactivestreams.*; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.*; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 24 |
import io.reactivex.rxjava3.functions.Supplier; |
| 25 |
import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue; |
| 26 |
import io.reactivex.rxjava3.internal.subscribers.QueueDrainSubscriber; |
| 27 |
import io.reactivex.rxjava3.internal.subscriptions.*; |
| 28 |
import io.reactivex.rxjava3.internal.util.QueueDrainHelper; |
| 29 |
import io.reactivex.rxjava3.subscribers.*; |
| 30 |
|
| 31 |
public final class FlowableBufferExactBoundary<T, U extends Collection<? super T>, B> |
| 32 |
extends AbstractFlowableWithUpstream<T, U> { |
| 33 |
final Publisher<B> boundary; |
| 34 |
final Supplier<U> bufferSupplier; |
| 35 |
|
| 36 |
public FlowableBufferExactBoundary(Flowable<T> source, Publisher<B> boundary, Supplier<U> bufferSupplier) { |
| 37 |
super(source); |
| 38 |
this.boundary = boundary; |
| 39 |
this.bufferSupplier = bufferSupplier; |
| 40 |
} |
| 41 |
|
| 42 |
@Override |
| 43 |
protected void subscribeActual(Subscriber<? super U> s) { |
| 44 |
source.subscribe(new BufferExactBoundarySubscriber<>(new SerializedSubscriber<>(s), bufferSupplier, boundary)); |
| 45 |
} |
| 46 |
|
| 47 |
static final class BufferExactBoundarySubscriber<T, U extends Collection<? super T>, B> |
| 48 |
extends QueueDrainSubscriber<T, U, U> implements FlowableSubscriber<T>, Subscription, Disposable { |
| 49 |
|
| 50 |
final Supplier<U> bufferSupplier; |
| 51 |
final Publisher<B> boundary; |
| 52 |
|
| 53 |
Subscription upstream; |
| 54 |
|
| 55 |
Disposable other; |
| 56 |
|
| 57 |
U buffer; |
| 58 |
|
| 59 |
BufferExactBoundarySubscriber(Subscriber<? super U> actual, Supplier<U> bufferSupplier, |
| 60 |
Publisher<B> boundary) { |
| 61 |
super(actual, new MpscLinkedQueue<>()); |
| 62 |
this.bufferSupplier = bufferSupplier; |
| 63 |
this.boundary = boundary; |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public void onSubscribe(Subscription s) { |
| 68 |
if (!SubscriptionHelper.validate(this.upstream, s)) { |
| 69 |
return; |
| 70 |
} |
| 71 |
this.upstream = s; |
| 72 |
|
| 73 |
U b; |
| 74 |
|
| 75 |
try { |
| 76 |
b = Objects.requireNonNull(bufferSupplier.get(), "The buffer supplied is null"); |
| 77 |
} catch (Throwable e) { |
| 78 |
Exceptions.throwIfFatal(e); |
| 79 |
cancelled = true; |
| 80 |
s.cancel(); |
| 81 |
EmptySubscription.error(e, downstream); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
buffer = b; |
| 86 |
|
| 87 |
BufferBoundarySubscriber<T, U, B> bs = new BufferBoundarySubscriber<>(this); |
| 88 |
other = bs; |
| 89 |
|
| 90 |
downstream.onSubscribe(this); |
| 91 |
|
| 92 |
if (!cancelled) { |
| 93 |
s.request(Long.MAX_VALUE); |
| 94 |
|
| 95 |
boundary.subscribe(bs); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public void onNext(T t) { |
| 101 |
synchronized (this) { |
| 102 |
U b = buffer; |
| 103 |
if (b == null) { |
| 104 |
return; |
| 105 |
} |
| 106 |
b.add(t); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void onError(Throwable t) { |
| 112 |
cancel(); |
| 113 |
downstream.onError(t); |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
public void onComplete() { |
| 118 |
U b; |
| 119 |
synchronized (this) { |
| 120 |
b = buffer; |
| 121 |
if (b == null) { |
| 122 |
return; |
| 123 |
} |
| 124 |
buffer = null; |
| 125 |
} |
| 126 |
queue.offer(b); |
| 127 |
done = true; |
| 128 |
if (enter()) { |
| 129 |
QueueDrainHelper.drainMaxLoop(queue, downstream, false, this, this); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public void request(long n) { |
| 135 |
requested(n); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void cancel() { |
| 140 |
if (!cancelled) { |
| 141 |
cancelled = true; |
| 142 |
other.dispose(); |
| 143 |
upstream.cancel(); |
| 144 |
|
| 145 |
if (enter()) { |
| 146 |
queue.clear(); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void next() { |
| 152 |
|
| 153 |
U next; |
| 154 |
|
| 155 |
try { |
| 156 |
next = Objects.requireNonNull(bufferSupplier.get(), "The buffer supplied is null"); |
| 157 |
} catch (Throwable e) { |
| 158 |
Exceptions.throwIfFatal(e); |
| 159 |
cancel(); |
| 160 |
downstream.onError(e); |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
U b; |
| 165 |
synchronized (this) { |
| 166 |
b = buffer; |
| 167 |
if (b == null) { |
| 168 |
return; |
| 169 |
} |
| 170 |
buffer = next; |
| 171 |
} |
| 172 |
|
| 173 |
fastPathEmitMax(b, false, this); |
| 174 |
} |
| 175 |
|
| 176 |
@Override |
| 177 |
public void dispose() { |
| 178 |
cancel(); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public boolean isDisposed() { |
| 183 |
return cancelled; |
| 184 |
} |
| 185 |
|
| 186 |
@Override |
| 187 |
public boolean accept(Subscriber<? super U> a, U v) { |
| 188 |
downstream.onNext(v); |
| 189 |
return true; |
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
static final class BufferBoundarySubscriber<T, U extends Collection<? super T>, B> extends DisposableSubscriber<B> { |
| 195 |
final BufferExactBoundarySubscriber<T, U, B> parent; |
| 196 |
|
| 197 |
BufferBoundarySubscriber(BufferExactBoundarySubscriber<T, U, B> parent) { |
| 198 |
this.parent = parent; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void onNext(B t) { |
| 203 |
parent.next(); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public void onError(Throwable t) { |
| 208 |
parent.onError(t); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public void onComplete() { |
| 213 |
parent.onComplete(); |
| 214 |
} |
| 215 |
} |
| 216 |
} |