497 lines
16 KiB
Java
497 lines
16 KiB
Java
package io.reactivex.internal.operators.observable;
|
|
|
|
import io.reactivex.ObservableSource;
|
|
import io.reactivex.Observer;
|
|
import io.reactivex.Scheduler;
|
|
import io.reactivex.disposables.Disposable;
|
|
import io.reactivex.exceptions.Exceptions;
|
|
import io.reactivex.internal.disposables.DisposableHelper;
|
|
import io.reactivex.internal.disposables.EmptyDisposable;
|
|
import io.reactivex.internal.functions.ObjectHelper;
|
|
import io.reactivex.internal.observers.QueueDrainObserver;
|
|
import io.reactivex.internal.queue.MpscLinkedQueue;
|
|
import io.reactivex.internal.util.QueueDrainHelper;
|
|
import io.reactivex.observers.SerializedObserver;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public final class ObservableBufferTimed<T, U extends Collection<? super T>> extends AbstractObservableWithUpstream<T, U> {
|
|
final long b;
|
|
final long c;
|
|
final TimeUnit d;
|
|
final Scheduler e;
|
|
final Callable<U> f;
|
|
final int g;
|
|
final boolean h;
|
|
|
|
static final class BufferExactBoundedObserver<T, U extends Collection<? super T>> extends QueueDrainObserver<T, U, U> implements Runnable, Disposable {
|
|
final Callable<U> g;
|
|
final long h;
|
|
final TimeUnit i;
|
|
final int j;
|
|
final boolean k;
|
|
final Scheduler.Worker l;
|
|
U m;
|
|
Disposable n;
|
|
Disposable o;
|
|
long p;
|
|
long q;
|
|
|
|
BufferExactBoundedObserver(Observer<? super U> observer, Callable<U> callable, long j, TimeUnit timeUnit, int i, boolean z, Scheduler.Worker worker) {
|
|
super(observer, new MpscLinkedQueue());
|
|
this.g = callable;
|
|
this.h = j;
|
|
this.i = timeUnit;
|
|
this.j = i;
|
|
this.k = z;
|
|
this.l = worker;
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
@Override // io.reactivex.internal.observers.QueueDrainObserver, io.reactivex.internal.util.ObservableQueueDrain
|
|
public /* bridge */ /* synthetic */ void a(Observer observer, Object obj) {
|
|
a((Observer<? super Observer>) observer, (Observer) obj);
|
|
}
|
|
|
|
@Override // io.reactivex.disposables.Disposable
|
|
public void dispose() {
|
|
if (this.d) {
|
|
return;
|
|
}
|
|
this.d = true;
|
|
this.o.dispose();
|
|
this.l.dispose();
|
|
synchronized (this) {
|
|
this.m = null;
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onComplete() {
|
|
U u;
|
|
this.l.dispose();
|
|
synchronized (this) {
|
|
u = this.m;
|
|
this.m = null;
|
|
}
|
|
this.c.offer(u);
|
|
this.e = true;
|
|
if (d()) {
|
|
QueueDrainHelper.a(this.c, this.b, false, this, this);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onError(Throwable th) {
|
|
synchronized (this) {
|
|
this.m = null;
|
|
}
|
|
this.b.onError(th);
|
|
this.l.dispose();
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onNext(T t) {
|
|
synchronized (this) {
|
|
U u = this.m;
|
|
if (u == null) {
|
|
return;
|
|
}
|
|
u.add(t);
|
|
if (u.size() < this.j) {
|
|
return;
|
|
}
|
|
this.m = null;
|
|
this.p++;
|
|
if (this.k) {
|
|
this.n.dispose();
|
|
}
|
|
b(u, false, this);
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The buffer supplied is null");
|
|
U u2 = call;
|
|
synchronized (this) {
|
|
this.m = u2;
|
|
this.q++;
|
|
}
|
|
if (this.k) {
|
|
Scheduler.Worker worker = this.l;
|
|
long j = this.h;
|
|
this.n = worker.a(this, j, j, this.i);
|
|
}
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
this.b.onError(th);
|
|
dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
if (DisposableHelper.validate(this.o, disposable)) {
|
|
this.o = disposable;
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The buffer supplied is null");
|
|
this.m = call;
|
|
this.b.onSubscribe(this);
|
|
Scheduler.Worker worker = this.l;
|
|
long j = this.h;
|
|
this.n = worker.a(this, j, j, this.i);
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
disposable.dispose();
|
|
EmptyDisposable.error(th, this.b);
|
|
this.l.dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The bufferSupplier returned a null buffer");
|
|
U u = call;
|
|
synchronized (this) {
|
|
U u2 = this.m;
|
|
if (u2 != null && this.p == this.q) {
|
|
this.m = u;
|
|
b(u2, false, this);
|
|
}
|
|
}
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
dispose();
|
|
this.b.onError(th);
|
|
}
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public void a(Observer<? super U> observer, U u) {
|
|
observer.onNext(u);
|
|
}
|
|
}
|
|
|
|
static final class BufferExactUnboundedObserver<T, U extends Collection<? super T>> extends QueueDrainObserver<T, U, U> implements Runnable, Disposable {
|
|
final Callable<U> g;
|
|
final long h;
|
|
final TimeUnit i;
|
|
final Scheduler j;
|
|
Disposable k;
|
|
U l;
|
|
final AtomicReference<Disposable> m;
|
|
|
|
BufferExactUnboundedObserver(Observer<? super U> observer, Callable<U> callable, long j, TimeUnit timeUnit, Scheduler scheduler) {
|
|
super(observer, new MpscLinkedQueue());
|
|
this.m = new AtomicReference<>();
|
|
this.g = callable;
|
|
this.h = j;
|
|
this.i = timeUnit;
|
|
this.j = scheduler;
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
@Override // io.reactivex.internal.observers.QueueDrainObserver, io.reactivex.internal.util.ObservableQueueDrain
|
|
public /* bridge */ /* synthetic */ void a(Observer observer, Object obj) {
|
|
a((Observer<? super Observer>) observer, (Observer) obj);
|
|
}
|
|
|
|
@Override // io.reactivex.disposables.Disposable
|
|
public void dispose() {
|
|
DisposableHelper.dispose(this.m);
|
|
this.k.dispose();
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onComplete() {
|
|
U u;
|
|
synchronized (this) {
|
|
u = this.l;
|
|
this.l = null;
|
|
}
|
|
if (u != null) {
|
|
this.c.offer(u);
|
|
this.e = true;
|
|
if (d()) {
|
|
QueueDrainHelper.a(this.c, this.b, false, null, this);
|
|
}
|
|
}
|
|
DisposableHelper.dispose(this.m);
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onError(Throwable th) {
|
|
synchronized (this) {
|
|
this.l = null;
|
|
}
|
|
this.b.onError(th);
|
|
DisposableHelper.dispose(this.m);
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onNext(T t) {
|
|
synchronized (this) {
|
|
U u = this.l;
|
|
if (u == null) {
|
|
return;
|
|
}
|
|
u.add(t);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
if (DisposableHelper.validate(this.k, disposable)) {
|
|
this.k = disposable;
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The buffer supplied is null");
|
|
this.l = call;
|
|
this.b.onSubscribe(this);
|
|
if (this.d) {
|
|
return;
|
|
}
|
|
Scheduler scheduler = this.j;
|
|
long j = this.h;
|
|
Disposable a = scheduler.a(this, j, j, this.i);
|
|
if (this.m.compareAndSet(null, a)) {
|
|
return;
|
|
}
|
|
a.dispose();
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
dispose();
|
|
EmptyDisposable.error(th, this.b);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
U u;
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The bufferSupplier returned a null buffer");
|
|
U u2 = call;
|
|
synchronized (this) {
|
|
u = this.l;
|
|
if (u != null) {
|
|
this.l = u2;
|
|
}
|
|
}
|
|
if (u == null) {
|
|
DisposableHelper.dispose(this.m);
|
|
} else {
|
|
a(u, false, this);
|
|
}
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
this.b.onError(th);
|
|
dispose();
|
|
}
|
|
}
|
|
|
|
public void a(Observer<? super U> observer, U u) {
|
|
this.b.onNext(u);
|
|
}
|
|
}
|
|
|
|
static final class BufferSkipBoundedObserver<T, U extends Collection<? super T>> extends QueueDrainObserver<T, U, U> implements Runnable, Disposable {
|
|
final Callable<U> g;
|
|
final long h;
|
|
final long i;
|
|
final TimeUnit j;
|
|
final Scheduler.Worker k;
|
|
final List<U> l;
|
|
Disposable m;
|
|
|
|
final class RemoveFromBuffer implements Runnable {
|
|
private final U a;
|
|
|
|
RemoveFromBuffer(U u) {
|
|
this.a = u;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
synchronized (BufferSkipBoundedObserver.this) {
|
|
BufferSkipBoundedObserver.this.l.remove(this.a);
|
|
}
|
|
BufferSkipBoundedObserver bufferSkipBoundedObserver = BufferSkipBoundedObserver.this;
|
|
bufferSkipBoundedObserver.b(this.a, false, bufferSkipBoundedObserver.k);
|
|
}
|
|
}
|
|
|
|
final class RemoveFromBufferEmit implements Runnable {
|
|
private final U a;
|
|
|
|
RemoveFromBufferEmit(U u) {
|
|
this.a = u;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
synchronized (BufferSkipBoundedObserver.this) {
|
|
BufferSkipBoundedObserver.this.l.remove(this.a);
|
|
}
|
|
BufferSkipBoundedObserver bufferSkipBoundedObserver = BufferSkipBoundedObserver.this;
|
|
bufferSkipBoundedObserver.b(this.a, false, bufferSkipBoundedObserver.k);
|
|
}
|
|
}
|
|
|
|
BufferSkipBoundedObserver(Observer<? super U> observer, Callable<U> callable, long j, long j2, TimeUnit timeUnit, Scheduler.Worker worker) {
|
|
super(observer, new MpscLinkedQueue());
|
|
this.g = callable;
|
|
this.h = j;
|
|
this.i = j2;
|
|
this.j = timeUnit;
|
|
this.k = worker;
|
|
this.l = new LinkedList();
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
@Override // io.reactivex.internal.observers.QueueDrainObserver, io.reactivex.internal.util.ObservableQueueDrain
|
|
public /* bridge */ /* synthetic */ void a(Observer observer, Object obj) {
|
|
a((Observer<? super Observer>) observer, (Observer) obj);
|
|
}
|
|
|
|
@Override // io.reactivex.disposables.Disposable
|
|
public void dispose() {
|
|
if (this.d) {
|
|
return;
|
|
}
|
|
this.d = true;
|
|
f();
|
|
this.m.dispose();
|
|
this.k.dispose();
|
|
}
|
|
|
|
void f() {
|
|
synchronized (this) {
|
|
this.l.clear();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onComplete() {
|
|
ArrayList arrayList;
|
|
synchronized (this) {
|
|
arrayList = new ArrayList(this.l);
|
|
this.l.clear();
|
|
}
|
|
Iterator it = arrayList.iterator();
|
|
while (it.hasNext()) {
|
|
this.c.offer((Collection) it.next());
|
|
}
|
|
this.e = true;
|
|
if (d()) {
|
|
QueueDrainHelper.a(this.c, this.b, false, this.k, this);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onError(Throwable th) {
|
|
this.e = true;
|
|
f();
|
|
this.b.onError(th);
|
|
this.k.dispose();
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onNext(T t) {
|
|
synchronized (this) {
|
|
Iterator<U> it = this.l.iterator();
|
|
while (it.hasNext()) {
|
|
it.next().add(t);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
if (DisposableHelper.validate(this.m, disposable)) {
|
|
this.m = disposable;
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The buffer supplied is null");
|
|
U u = call;
|
|
this.l.add(u);
|
|
this.b.onSubscribe(this);
|
|
Scheduler.Worker worker = this.k;
|
|
long j = this.i;
|
|
worker.a(this, j, j, this.j);
|
|
this.k.a(new RemoveFromBufferEmit(u), this.h, this.j);
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
disposable.dispose();
|
|
EmptyDisposable.error(th, this.b);
|
|
this.k.dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
if (this.d) {
|
|
return;
|
|
}
|
|
try {
|
|
U call = this.g.call();
|
|
ObjectHelper.a(call, "The bufferSupplier returned a null buffer");
|
|
U u = call;
|
|
synchronized (this) {
|
|
if (this.d) {
|
|
return;
|
|
}
|
|
this.l.add(u);
|
|
this.k.a(new RemoveFromBuffer(u), this.h, this.j);
|
|
}
|
|
} catch (Throwable th) {
|
|
Exceptions.b(th);
|
|
this.b.onError(th);
|
|
dispose();
|
|
}
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
public void a(Observer<? super U> observer, U u) {
|
|
observer.onNext(u);
|
|
}
|
|
}
|
|
|
|
public ObservableBufferTimed(ObservableSource<T> observableSource, long j, long j2, TimeUnit timeUnit, Scheduler scheduler, Callable<U> callable, int i, boolean z) {
|
|
super(observableSource);
|
|
this.b = j;
|
|
this.c = j2;
|
|
this.d = timeUnit;
|
|
this.e = scheduler;
|
|
this.f = callable;
|
|
this.g = i;
|
|
this.h = z;
|
|
}
|
|
|
|
@Override // io.reactivex.Observable
|
|
protected void subscribeActual(Observer<? super U> observer) {
|
|
if (this.b == this.c && this.g == Integer.MAX_VALUE) {
|
|
this.a.subscribe(new BufferExactUnboundedObserver(new SerializedObserver(observer), this.f, this.b, this.d, this.e));
|
|
return;
|
|
}
|
|
Scheduler.Worker a = this.e.a();
|
|
if (this.b == this.c) {
|
|
this.a.subscribe(new BufferExactBoundedObserver(new SerializedObserver(observer), this.f, this.b, this.d, this.g, this.h, a));
|
|
} else {
|
|
this.a.subscribe(new BufferSkipBoundedObserver(new SerializedObserver(observer), this.f, this.b, this.c, this.d, a));
|
|
}
|
|
}
|
|
}
|