| 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.observable; |
| 15 |
|
| 16 |
import java.util.concurrent.TimeUnit; |
| 17 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.core.*; |
| 20 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 21 |
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; |
| 22 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 23 |
|
| 24 |
public final class ObservableTakeLastTimed<T> extends AbstractObservableWithUpstream<T, T> { |
| 25 |
final long count; |
| 26 |
final long time; |
| 27 |
final TimeUnit unit; |
| 28 |
final Scheduler scheduler; |
| 29 |
final int bufferSize; |
| 30 |
final boolean delayError; |
| 31 |
|
| 32 |
public ObservableTakeLastTimed(ObservableSource<T> source, |
| 33 |
long count, long time, TimeUnit unit, Scheduler scheduler, int bufferSize, boolean delayError) { |
| 34 |
super(source); |
| 35 |
this.count = count; |
| 36 |
this.time = time; |
| 37 |
this.unit = unit; |
| 38 |
this.scheduler = scheduler; |
| 39 |
this.bufferSize = bufferSize; |
| 40 |
this.delayError = delayError; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public void subscribeActual(Observer<? super T> t) { |
| 45 |
source.subscribe(new TakeLastTimedObserver<>(t, count, time, unit, scheduler, bufferSize, delayError)); |
| 46 |
} |
| 47 |
|
| 48 |
static final class TakeLastTimedObserver<T> |
| 49 |
extends AtomicBoolean implements Observer<T>, Disposable { |
| 50 |
|
| 51 |
private static final long serialVersionUID = -5677354903406201275L; |
| 52 |
final Observer<? super T> downstream; |
| 53 |
final long count; |
| 54 |
final long time; |
| 55 |
final TimeUnit unit; |
| 56 |
final Scheduler scheduler; |
| 57 |
final SpscLinkedArrayQueue<Object> queue; |
| 58 |
final boolean delayError; |
| 59 |
|
| 60 |
Disposable upstream; |
| 61 |
|
| 62 |
volatile boolean cancelled; |
| 63 |
|
| 64 |
Throwable error; |
| 65 |
|
| 66 |
TakeLastTimedObserver(Observer<? super T> actual, long count, long time, TimeUnit unit, Scheduler scheduler, int bufferSize, boolean delayError) { |
| 67 |
this.downstream = actual; |
| 68 |
this.count = count; |
| 69 |
this.time = time; |
| 70 |
this.unit = unit; |
| 71 |
this.scheduler = scheduler; |
| 72 |
this.queue = new SpscLinkedArrayQueue<>(bufferSize); |
| 73 |
this.delayError = delayError; |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public void onSubscribe(Disposable d) { |
| 78 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 79 |
this.upstream = d; |
| 80 |
downstream.onSubscribe(this); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public void onNext(T t) { |
| 86 |
final SpscLinkedArrayQueue<Object> q = queue; |
| 87 |
|
| 88 |
long now = scheduler.now(unit); |
| 89 |
long time = this.time; |
| 90 |
long c = count; |
| 91 |
boolean unbounded = c == Long.MAX_VALUE; |
| 92 |
|
| 93 |
q.offer(now, t); |
| 94 |
|
| 95 |
while (!q.isEmpty()) { |
| 96 |
long ts = (Long)q.peek(); |
| 97 |
if (ts <= now - time || (!unbounded && (q.size() >> 1) > c)) { |
| 98 |
q.poll(); |
| 99 |
q.poll(); |
| 100 |
} else { |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void onError(Throwable t) { |
| 108 |
error = t; |
| 109 |
drain(); |
| 110 |
} |
| 111 |
|
| 112 |
@Override |
| 113 |
public void onComplete() { |
| 114 |
drain(); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public void dispose() { |
| 119 |
if (!cancelled) { |
| 120 |
cancelled = true; |
| 121 |
upstream.dispose(); |
| 122 |
|
| 123 |
if (compareAndSet(false, true)) { |
| 124 |
queue.clear(); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public boolean isDisposed() { |
| 131 |
return cancelled; |
| 132 |
} |
| 133 |
|
| 134 |
void drain() { |
| 135 |
if (!compareAndSet(false, true)) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
final Observer<? super T> a = downstream; |
| 140 |
final SpscLinkedArrayQueue<Object> q = queue; |
| 141 |
final boolean delayError = this.delayError; |
| 142 |
final long timestampLimit = scheduler.now(unit) - time; |
| 143 |
|
| 144 |
for (;;) { |
| 145 |
if (cancelled) { |
| 146 |
q.clear(); |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
if (!delayError) { |
| 151 |
Throwable ex = error; |
| 152 |
if (ex != null) { |
| 153 |
q.clear(); |
| 154 |
a.onError(ex); |
| 155 |
return; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
Object ts = q.poll(); // the timestamp long |
| 160 |
boolean empty = ts == null; |
| 161 |
|
| 162 |
if (empty) { |
| 163 |
Throwable ex = error; |
| 164 |
if (ex != null) { |
| 165 |
a.onError(ex); |
| 166 |
} else { |
| 167 |
a.onComplete(); |
| 168 |
} |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
@SuppressWarnings("unchecked") |
| 173 |
T o = (T)q.poll(); |
| 174 |
|
| 175 |
if ((Long)ts < timestampLimit) { |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
a.onNext(o); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |