package j0.l.a; import androidx.recyclerview.widget.RecyclerView; import j0.l.a.e; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import rx.Observable; import rx.Producer; import rx.Subscriber; import rx.Subscription; import rx.exceptions.OnErrorThrowable; import rx.functions.Action1; import rx.functions.Func0; /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public final class j1 extends j0.m.b implements Subscription { public static final Func0 k = new a(); public final Observable l; public final AtomicReference> m; public final Func0> n; /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static class a implements Func0 { @Override // rx.functions.Func0, java.util.concurrent.Callable public Object call() { return new h(16); } } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static class b extends AtomicReference implements e { private static final long serialVersionUID = 2346567790059478686L; public long index; public int size; public d tail; public b() { d dVar = new d(null, 0L); this.tail = dVar; set(dVar); } public void a() { } @Override // j0.l.a.j1.e public final void complete() { Object obj = e.a; long j = this.index + 1; this.index = j; d dVar = new d(obj, j); this.tail.set(dVar); this.tail = dVar; this.size++; } @Override // j0.l.a.j1.e public final void g(T t) { if (t == null) { t = (T) e.f3753b; } long j = this.index + 1; this.index = j; d dVar = new d(t, j); this.tail.set(dVar); this.tail = dVar; this.size++; a(); } @Override // j0.l.a.j1.e public final void h(c cVar) { Subscriber subscriber; d dVar; synchronized (cVar) { if (cVar.emitting) { cVar.missed = true; return; } cVar.emitting = true; while (!cVar.isUnsubscribed()) { d dVar2 = (d) cVar.index; if (dVar2 == null) { dVar2 = get(); cVar.index = dVar2; cVar.a(dVar2.index); } if (!cVar.isUnsubscribed() && (subscriber = cVar.child) != null) { long j = cVar.get(); long j2 = 0; while (j2 != j && (dVar = dVar2.get()) != null) { Object obj = dVar.value; try { if (e.a(subscriber, obj)) { cVar.index = null; return; } j2++; if (!cVar.isUnsubscribed()) { dVar2 = dVar; } else { return; } } catch (Throwable th) { cVar.index = null; b.i.a.f.e.o.f.o1(th); cVar.unsubscribe(); if (!(obj instanceof e.c) && !e.c(obj)) { subscriber.onError(OnErrorThrowable.a(th, e.b(obj))); return; } return; } } if (j2 != 0) { cVar.index = dVar2; if (j != RecyclerView.FOREVER_NS) { cVar.b(j2); } } synchronized (cVar) { if (!cVar.missed) { cVar.emitting = false; return; } cVar.missed = false; } } else { return; } } } } @Override // j0.l.a.j1.e public final void i(Throwable th) { e.c cVar = new e.c(th); long j = this.index + 1; this.index = j; d dVar = new d(cVar, j); this.tail.set(dVar); this.tail = dVar; this.size++; } } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static final class c extends AtomicLong implements Producer, Subscription { private static final long serialVersionUID = -4453897557930727610L; public Subscriber child; public boolean emitting; public Object index; public boolean missed; public final f parent; public final AtomicLong totalRequested = new AtomicLong(); public c(f fVar, Subscriber subscriber) { this.parent = fVar; this.child = subscriber; } public void a(long j) { long j2; long j3; do { j2 = this.totalRequested.get(); j3 = j2 + j; if (j3 < 0) { j3 = RecyclerView.FOREVER_NS; } } while (!this.totalRequested.compareAndSet(j2, j3)); } public long b(long j) { long j2; long j3; if (j > 0) { do { j2 = get(); if (j2 == Long.MIN_VALUE) { return Long.MIN_VALUE; } j3 = j2 - j; if (j3 < 0) { StringBuilder sb = new StringBuilder(); sb.append("More produced ("); sb.append(j); sb.append(") than requested ("); throw new IllegalStateException(b.d.b.a.a.B(sb, j2, ")")); } } while (!compareAndSet(j2, j3)); return j3; } throw new IllegalArgumentException("Cant produce zero or less"); } @Override // rx.Subscription public boolean isUnsubscribed() { return get() == Long.MIN_VALUE; } @Override // rx.Producer public void j(long j) { long j2; long j3; int i = (j > 0L ? 1 : (j == 0L ? 0 : -1)); if (i >= 0) { do { j2 = get(); if (j2 != Long.MIN_VALUE) { if (j2 < 0 || i != 0) { j3 = j2 + j; if (j3 < 0) { j3 = RecyclerView.FOREVER_NS; } } else { return; } } else { return; } } while (!compareAndSet(j2, j3)); a(j); this.parent.c(this); this.parent.k.h(this); } } @Override // rx.Subscription public void unsubscribe() { if (get() != Long.MIN_VALUE && getAndSet(Long.MIN_VALUE) != Long.MIN_VALUE) { f fVar = this.parent; if (!fVar.m) { synchronized (fVar.n) { if (!fVar.m) { j0.l.e.g> gVar = fVar.n; c[] cVarArr = gVar.d; int i = gVar.a; int b2 = j0.l.e.g.b(hashCode()) & i; c cVar = cVarArr[b2]; boolean z2 = true; if (cVar != null) { if (!cVar.equals(this)) { while (true) { b2 = (b2 + 1) & i; c cVar2 = cVarArr[b2]; if (cVar2 != null) { if (cVar2.equals(this)) { gVar.c(b2, cVarArr, i); break; } } else { break; } } } else { gVar.c(b2, cVarArr, i); } } if (fVar.n.f3776b != 0) { z2 = false; } if (z2) { fVar.o = f.j; } fVar.p++; } } } this.parent.c(this); this.child = null; } } } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static final class d extends AtomicReference { private static final long serialVersionUID = 245354315435971818L; public final long index; public final Object value; public d(Object obj, long j) { this.value = obj; this.index = j; } } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public interface e { void complete(); void g(T t); void h(c cVar); void i(Throwable th); } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static final class f extends Subscriber implements Subscription { public static final c[] j = new c[0]; public final e k; public boolean l; public volatile boolean m; public volatile long p; public long q; /* renamed from: s reason: collision with root package name */ public boolean f3756s; public boolean t; public long u; public long v; public volatile Producer w; /* renamed from: x reason: collision with root package name */ public List> f3757x; /* renamed from: y reason: collision with root package name */ public boolean f3758y; public final j0.l.e.g> n = new j0.l.e.g<>(); public c[] o = j; public final AtomicBoolean r = new AtomicBoolean(); public f(e eVar) { this.k = eVar; request(0L); } public c[] a() { c[] cVarArr; synchronized (this.n) { c[] cVarArr2 = this.n.d; int length = cVarArr2.length; cVarArr = new c[length]; System.arraycopy(cVarArr2, 0, cVarArr, 0, length); } return cVarArr; } public void b(long j2, long j3) { long j4 = this.v; Producer producer = this.w; long j5 = j2 - j3; if (j5 != 0) { this.u = j2; if (producer == null) { long j6 = j4 + j5; if (j6 < 0) { j6 = RecyclerView.FOREVER_NS; } this.v = j6; } else if (j4 != 0) { this.v = 0L; producer.j(j4 + j5); } else { producer.j(j5); } } else if (j4 != 0 && producer != null) { this.v = 0L; producer.j(j4); } } public void c(c cVar) { long j2; List> list; boolean z2; long j3; c[] a; c[] a2; if (!isUnsubscribed()) { synchronized (this) { if (this.f3756s) { if (cVar != null) { List list2 = this.f3757x; if (list2 == null) { list2 = new ArrayList(); this.f3757x = list2; } list2.add(cVar); } else { this.f3758y = true; } this.t = true; return; } this.f3756s = true; long j4 = this.u; if (cVar != null) { j2 = Math.max(j4, cVar.totalRequested.get()); } else { long j5 = j4; for (c cVar2 : a()) { if (cVar2 != null) { j5 = Math.max(j5, cVar2.totalRequested.get()); } } j2 = j5; } b(j2, j4); while (!isUnsubscribed()) { synchronized (this) { if (!this.t) { this.f3756s = false; return; } this.t = false; list = this.f3757x; this.f3757x = null; z2 = this.f3758y; this.f3758y = false; } long j6 = this.u; if (list != null) { j3 = j6; for (c cVar3 : list) { j3 = Math.max(j3, cVar3.totalRequested.get()); } } else { j3 = j6; } if (z2) { for (c cVar4 : a()) { if (cVar4 != null) { j3 = Math.max(j3, cVar4.totalRequested.get()); } } } b(j3, j6); } } } } public void d() { c[] cVarArr = this.o; if (this.q != this.p) { synchronized (this.n) { cVarArr = this.o; c[] cVarArr2 = this.n.d; int length = cVarArr2.length; if (cVarArr.length != length) { cVarArr = new c[length]; this.o = cVarArr; } System.arraycopy(cVarArr2, 0, cVarArr, 0, length); this.q = this.p; } } e eVar = this.k; for (c cVar : cVarArr) { if (cVar != null) { eVar.h(cVar); } } } @Override // j0.g public void onCompleted() { if (!this.l) { this.l = true; try { this.k.complete(); d(); } finally { unsubscribe(); } } } @Override // j0.g public void onError(Throwable th) { if (!this.l) { this.l = true; try { this.k.i(th); d(); } finally { unsubscribe(); } } } @Override // j0.g public void onNext(T t) { if (!this.l) { this.k.g(t); d(); } } @Override // rx.Subscriber public void setProducer(Producer producer) { if (this.w == null) { this.w = producer; c(null); d(); return; } throw new IllegalStateException("Only a single producer can be set on a Subscriber."); } } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static final class g extends b { private static final long serialVersionUID = -5898283885385201806L; public final int limit; public g(int i) { this.limit = i; } @Override // j0.l.a.j1.b public void a() { if (this.size > this.limit) { d dVar = get().get(); if (dVar != null) { this.size--; set(dVar); return; } throw new IllegalStateException("Empty list!"); } } } /* compiled from: OperatorReplay.java */ /* loaded from: classes3.dex */ public static final class h extends ArrayList implements e { private static final long serialVersionUID = 7063189396499112664L; public volatile int size; public h(int i) { super(i); } @Override // j0.l.a.j1.e public void complete() { add(e.a); this.size++; } @Override // j0.l.a.j1.e public void g(T t) { if (t == null) { t = (T) e.f3753b; } add(t); this.size++; } @Override // j0.l.a.j1.e public void h(c cVar) { synchronized (cVar) { if (cVar.emitting) { cVar.missed = true; return; } cVar.emitting = true; while (!cVar.isUnsubscribed()) { int i = this.size; Integer num = (Integer) cVar.index; int intValue = num != null ? num.intValue() : 0; Subscriber subscriber = cVar.child; if (subscriber != null) { long j = cVar.get(); long j2 = 0; while (j2 != j && intValue < i) { Object obj = get(intValue); try { if (!e.a(subscriber, obj) && !cVar.isUnsubscribed()) { intValue++; j2++; } else { return; } } catch (Throwable th) { b.i.a.f.e.o.f.o1(th); cVar.unsubscribe(); if (!(obj instanceof e.c) && !e.c(obj)) { subscriber.onError(OnErrorThrowable.a(th, e.b(obj))); return; } return; } } if (j2 != 0) { cVar.index = Integer.valueOf(intValue); if (j != RecyclerView.FOREVER_NS) { cVar.b(j2); } } synchronized (cVar) { if (!cVar.missed) { cVar.emitting = false; return; } cVar.missed = false; } } else { return; } } } } @Override // j0.l.a.j1.e public void i(Throwable th) { add(new e.c(th)); this.size++; } } public j1(Observable.a aVar, Observable observable, AtomicReference> atomicReference, Func0> func0) { super(aVar); this.l = observable; this.m = atomicReference; this.n = func0; } @Override // rx.Subscription public boolean isUnsubscribed() { f fVar = this.m.get(); return fVar == null || fVar.isUnsubscribed(); } @Override // j0.m.b public void k0(Action1 action1) { f fVar; while (true) { fVar = this.m.get(); if (fVar != null && !fVar.isUnsubscribed()) { break; } f fVar2 = new f<>((e) this.n.call()); fVar2.add(new j0.r.a(new m1(fVar2))); if (this.m.compareAndSet(fVar, fVar2)) { fVar = fVar2; break; } } boolean z2 = true; if (fVar.r.get() || !fVar.r.compareAndSet(false, true)) { z2 = false; } ((z) action1).call(fVar); if (z2) { this.l.i0(fVar); } } @Override // rx.Subscription public void unsubscribe() { this.m.lazySet(null); } }