| 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.ArrayDeque; |
| 17 |
import java.util.concurrent.atomic.*; |
| 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.subjects.UnicastSubject; |
| 23 |
|
| 24 |
public final class ObservableWindow<T> extends AbstractObservableWithUpstream<T, Observable<T>> { |
| 25 |
final long count; |
| 26 |
final long skip; |
| 27 |
final int capacityHint; |
| 28 |
|
| 29 |
public ObservableWindow(ObservableSource<T> source, long count, long skip, int capacityHint) { |
| 30 |
super(source); |
| 31 |
this.count = count; |
| 32 |
this.skip = skip; |
| 33 |
this.capacityHint = capacityHint; |
| 34 |
} |
| 35 |
|
| 36 |
@Override |
| 37 |
public void subscribeActual(Observer<? super Observable<T>> t) { |
| 38 |
if (count == skip) { |
| 39 |
source.subscribe(new WindowExactObserver<>(t, count, capacityHint)); |
| 40 |
} else { |
| 41 |
source.subscribe(new WindowSkipObserver<>(t, count, skip, capacityHint)); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
static final class WindowExactObserver<T> |
| 46 |
extends AtomicInteger |
| 47 |
implements Observer<T>, Disposable, Runnable { |
| 48 |
|
| 49 |
private static final long serialVersionUID = -7481782523886138128L; |
| 50 |
final Observer<? super Observable<T>> downstream; |
| 51 |
final long count; |
| 52 |
final int capacityHint; |
| 53 |
|
| 54 |
final AtomicBoolean cancelled; |
| 55 |
|
| 56 |
long size; |
| 57 |
|
| 58 |
Disposable upstream; |
| 59 |
|
| 60 |
UnicastSubject<T> window; |
| 61 |
|
| 62 |
WindowExactObserver(Observer<? super Observable<T>> actual, long count, int capacityHint) { |
| 63 |
this.downstream = actual; |
| 64 |
this.count = count; |
| 65 |
this.capacityHint = capacityHint; |
| 66 |
this.cancelled = new AtomicBoolean(); |
| 67 |
this.lazySet(1); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void onSubscribe(Disposable d) { |
| 72 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 73 |
this.upstream = d; |
| 74 |
|
| 75 |
downstream.onSubscribe(this); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void onNext(T t) { |
| 81 |
UnicastSubject<T> w = window; |
| 82 |
ObservableWindowSubscribeIntercept<T> intercept = null; |
| 83 |
if (w == null && !cancelled.get()) { |
| 84 |
getAndIncrement(); |
| 85 |
|
| 86 |
w = UnicastSubject.create(capacityHint, this); |
| 87 |
window = w; |
| 88 |
intercept = new ObservableWindowSubscribeIntercept<>(w); |
| 89 |
downstream.onNext(intercept); |
| 90 |
} |
| 91 |
|
| 92 |
if (w != null) { |
| 93 |
w.onNext(t); |
| 94 |
|
| 95 |
if (++size >= count) { |
| 96 |
size = 0; |
| 97 |
window = null; |
| 98 |
w.onComplete(); |
| 99 |
} |
| 100 |
|
| 101 |
if (intercept != null && intercept.tryAbandon()) { |
| 102 |
window = null; |
| 103 |
w.onComplete(); |
| 104 |
w = null; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
@Override |
| 110 |
public void onError(Throwable t) { |
| 111 |
UnicastSubject<T> w = window; |
| 112 |
if (w != null) { |
| 113 |
window = null; |
| 114 |
w.onError(t); |
| 115 |
} |
| 116 |
downstream.onError(t); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void onComplete() { |
| 121 |
UnicastSubject<T> w = window; |
| 122 |
if (w != null) { |
| 123 |
window = null; |
| 124 |
w.onComplete(); |
| 125 |
} |
| 126 |
downstream.onComplete(); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void dispose() { |
| 131 |
if (cancelled.compareAndSet(false, true)) { |
| 132 |
run(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
@Override |
| 137 |
public boolean isDisposed() { |
| 138 |
return cancelled.get(); |
| 139 |
} |
| 140 |
|
| 141 |
@Override |
| 142 |
public void run() { |
| 143 |
if (decrementAndGet() == 0) { |
| 144 |
upstream.dispose(); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
static final class WindowSkipObserver<T> extends AtomicInteger |
| 150 |
implements Observer<T>, Disposable, Runnable { |
| 151 |
|
| 152 |
private static final long serialVersionUID = 3366976432059579510L; |
| 153 |
final Observer<? super Observable<T>> downstream; |
| 154 |
final long count; |
| 155 |
final long skip; |
| 156 |
final int capacityHint; |
| 157 |
final ArrayDeque<UnicastSubject<T>> windows; |
| 158 |
|
| 159 |
final AtomicBoolean cancelled; |
| 160 |
|
| 161 |
long index; |
| 162 |
|
| 163 |
/** Counts how many elements were emitted to the very first window in windows. */ |
| 164 |
long firstEmission; |
| 165 |
|
| 166 |
Disposable upstream; |
| 167 |
|
| 168 |
WindowSkipObserver(Observer<? super Observable<T>> actual, long count, long skip, int capacityHint) { |
| 169 |
this.downstream = actual; |
| 170 |
this.count = count; |
| 171 |
this.skip = skip; |
| 172 |
this.capacityHint = capacityHint; |
| 173 |
this.windows = new ArrayDeque<>(); |
| 174 |
this.cancelled = new AtomicBoolean(); |
| 175 |
this.lazySet(1); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public void onSubscribe(Disposable d) { |
| 180 |
if (DisposableHelper.validate(this.upstream, d)) { |
| 181 |
this.upstream = d; |
| 182 |
|
| 183 |
downstream.onSubscribe(this); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void onNext(T t) { |
| 189 |
final ArrayDeque<UnicastSubject<T>> ws = windows; |
| 190 |
|
| 191 |
long i = index; |
| 192 |
|
| 193 |
long s = skip; |
| 194 |
|
| 195 |
ObservableWindowSubscribeIntercept<T> intercept = null; |
| 196 |
|
| 197 |
if (i % s == 0 && !cancelled.get()) { |
| 198 |
getAndIncrement(); |
| 199 |
UnicastSubject<T> w = UnicastSubject.create(capacityHint, this); |
| 200 |
intercept = new ObservableWindowSubscribeIntercept<>(w); |
| 201 |
ws.offer(w); |
| 202 |
downstream.onNext(intercept); |
| 203 |
} |
| 204 |
|
| 205 |
long c = firstEmission + 1; |
| 206 |
|
| 207 |
for (UnicastSubject<T> w : ws) { |
| 208 |
w.onNext(t); |
| 209 |
} |
| 210 |
|
| 211 |
if (c >= count) { |
| 212 |
ws.poll().onComplete(); |
| 213 |
if (ws.isEmpty() && cancelled.get()) { |
| 214 |
return; |
| 215 |
} |
| 216 |
firstEmission = c - s; |
| 217 |
} else { |
| 218 |
firstEmission = c; |
| 219 |
} |
| 220 |
|
| 221 |
index = i + 1; |
| 222 |
|
| 223 |
if (intercept != null && intercept.tryAbandon()) { |
| 224 |
intercept.window.onComplete(); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public void onError(Throwable t) { |
| 230 |
final ArrayDeque<UnicastSubject<T>> ws = windows; |
| 231 |
while (!ws.isEmpty()) { |
| 232 |
ws.poll().onError(t); |
| 233 |
} |
| 234 |
downstream.onError(t); |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void onComplete() { |
| 239 |
final ArrayDeque<UnicastSubject<T>> ws = windows; |
| 240 |
while (!ws.isEmpty()) { |
| 241 |
ws.poll().onComplete(); |
| 242 |
} |
| 243 |
downstream.onComplete(); |
| 244 |
} |
| 245 |
|
| 246 |
@Override |
| 247 |
public void dispose() { |
| 248 |
if (cancelled.compareAndSet(false, true)) { |
| 249 |
run(); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public boolean isDisposed() { |
| 255 |
return cancelled.get(); |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public void run() { |
| 260 |
if (decrementAndGet() == 0) { |
| 261 |
upstream.dispose(); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} |