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