package io.reactivex.internal.operators.observable; import io.reactivex.Observable; import io.reactivex.ObservableSource; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.internal.disposables.DisposableHelper; import io.reactivex.subjects.UnicastSubject; import java.util.ArrayDeque; import java.util.Iterator; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; /* loaded from: classes2.dex */ public final class ObservableWindow extends AbstractObservableWithUpstream> { final long b; final long c; final int d; static final class WindowExactObserver extends AtomicInteger implements Observer, Disposable, Runnable { final Observer> a; final long b; final int c; long d; Disposable e; UnicastSubject f; volatile boolean g; WindowExactObserver(Observer> observer, long j, int i) { this.a = observer; this.b = j; this.c = i; } @Override // io.reactivex.disposables.Disposable public void dispose() { this.g = true; } @Override // io.reactivex.Observer public void onComplete() { UnicastSubject unicastSubject = this.f; if (unicastSubject != null) { this.f = null; unicastSubject.onComplete(); } this.a.onComplete(); } @Override // io.reactivex.Observer public void onError(Throwable th) { UnicastSubject unicastSubject = this.f; if (unicastSubject != null) { this.f = null; unicastSubject.onError(th); } this.a.onError(th); } @Override // io.reactivex.Observer public void onNext(T t) { UnicastSubject unicastSubject = this.f; if (unicastSubject == null && !this.g) { unicastSubject = UnicastSubject.a(this.c, this); this.f = unicastSubject; this.a.onNext(unicastSubject); } if (unicastSubject != null) { unicastSubject.onNext(t); long j = this.d + 1; this.d = j; if (j >= this.b) { this.d = 0L; this.f = null; unicastSubject.onComplete(); if (this.g) { this.e.dispose(); } } } } @Override // io.reactivex.Observer public void onSubscribe(Disposable disposable) { if (DisposableHelper.validate(this.e, disposable)) { this.e = disposable; this.a.onSubscribe(this); } } @Override // java.lang.Runnable public void run() { if (this.g) { this.e.dispose(); } } } static final class WindowSkipObserver extends AtomicBoolean implements Observer, Disposable, Runnable { final Observer> a; final long b; final long c; final int d; long f; volatile boolean g; long h; Disposable i; final AtomicInteger j = new AtomicInteger(); final ArrayDeque> e = new ArrayDeque<>(); WindowSkipObserver(Observer> observer, long j, long j2, int i) { this.a = observer; this.b = j; this.c = j2; this.d = i; } @Override // io.reactivex.disposables.Disposable public void dispose() { this.g = true; } @Override // io.reactivex.Observer public void onComplete() { ArrayDeque> arrayDeque = this.e; while (!arrayDeque.isEmpty()) { arrayDeque.poll().onComplete(); } this.a.onComplete(); } @Override // io.reactivex.Observer public void onError(Throwable th) { ArrayDeque> arrayDeque = this.e; while (!arrayDeque.isEmpty()) { arrayDeque.poll().onError(th); } this.a.onError(th); } @Override // io.reactivex.Observer public void onNext(T t) { ArrayDeque> arrayDeque = this.e; long j = this.f; long j2 = this.c; if (j % j2 == 0 && !this.g) { this.j.getAndIncrement(); UnicastSubject a = UnicastSubject.a(this.d, this); arrayDeque.offer(a); this.a.onNext(a); } long j3 = this.h + 1; Iterator> it = arrayDeque.iterator(); while (it.hasNext()) { it.next().onNext(t); } if (j3 >= this.b) { arrayDeque.poll().onComplete(); if (arrayDeque.isEmpty() && this.g) { this.i.dispose(); return; } this.h = j3 - j2; } else { this.h = j3; } this.f = j + 1; } @Override // io.reactivex.Observer public void onSubscribe(Disposable disposable) { if (DisposableHelper.validate(this.i, disposable)) { this.i = disposable; this.a.onSubscribe(this); } } @Override // java.lang.Runnable public void run() { if (this.j.decrementAndGet() == 0 && this.g) { this.i.dispose(); } } } public ObservableWindow(ObservableSource observableSource, long j, long j2, int i) { super(observableSource); this.b = j; this.c = j2; this.d = i; } @Override // io.reactivex.Observable public void subscribeActual(Observer> observer) { long j = this.b; long j2 = this.c; if (j == j2) { this.a.subscribe(new WindowExactObserver(observer, j, this.d)); } else { this.a.subscribe(new WindowSkipObserver(observer, j, j2, this.d)); } } }