package j0.l.a; import j0.l.e.h; import j0.l.e.j; import j0.l.e.m.g; import j0.l.e.n.n; import j0.l.e.n.y; import java.util.ArrayList; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.atomic.AtomicLong; import rx.Observable; import rx.Producer; import rx.Subscriber; import rx.exceptions.CompositeException; import rx.exceptions.MissingBackpressureException; import rx.exceptions.OnErrorThrowable; import rx.subscriptions.CompositeSubscription; /* compiled from: OperatorMerge */ public final class x0 implements Observable.b> { public final boolean i; /* compiled from: OperatorMerge */ public static final class a { public static final x0 a = new x0<>(true, Integer.MAX_VALUE); } /* compiled from: OperatorMerge */ public static final class b { public static final x0 a = new x0<>(false, Integer.MAX_VALUE); } /* compiled from: OperatorMerge */ public static final class c extends Subscriber { public static final int i = (h.i / 4); public final e j; public final long k; public volatile boolean l; public volatile h m; public int n; public c(e eVar, long j) { this.j = eVar; this.k = j; } public void a(long j) { int i2 = this.n - ((int) j); if (i2 > i) { this.n = i2; return; } int i3 = h.i; this.n = i3; int i4 = i3 - i2; if (i4 > 0) { request((long) i4); } } @Override // j0.g public void onCompleted() { this.l = true; this.j.b(); } @Override // j0.g public void onError(Throwable th) { this.j.d().offer(th); this.l = true; this.j.b(); } @Override // j0.g public void onNext(T t) { boolean z2; Throwable th; e eVar = this.j; long j = eVar.m.get(); boolean z3 = true; if (j != 0) { synchronized (eVar) { j = eVar.m.get(); if (eVar.r || j == 0) { z2 = false; } else { eVar.r = true; z2 = true; } } } else { z2 = false; } if (z2) { h hVar = this.m; if (hVar != null) { Queue queue = hVar.j; if (!(queue == null || queue.isEmpty())) { eVar.f(this, t); eVar.c(); return; } } try { eVar.j.onNext(t); } catch (Throwable th2) { th = th2; } if (j != Long.MAX_VALUE) { eVar.m.a(1); } a(1); synchronized (eVar) { if (!eVar.f2836s) { eVar.r = false; return; } eVar.f2836s = false; eVar.c(); return; } } eVar.f(this, t); eVar.b(); return; if (!z3) { synchronized (eVar) { eVar.r = false; } } throw th; } @Override // rx.Subscriber public void onStart() { int i2 = h.i; this.n = i2; request((long) i2); } } /* compiled from: OperatorMerge */ public static final class d extends AtomicLong implements Producer { private static final long serialVersionUID = -1214379189873595503L; public final e subscriber; public d(e eVar) { this.subscriber = eVar; } public long a(int i) { return addAndGet((long) (-i)); } @Override // rx.Producer public void j(long j) { int i = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i > 0) { if (get() != Long.MAX_VALUE) { c.q.a.k.a.p(this, j); this.subscriber.b(); } } else if (i < 0) { throw new IllegalArgumentException("n >= 0 required"); } } } /* compiled from: OperatorMerge */ public static final class e extends Subscriber> { public static final c[] i = new c[0]; public final Subscriber j; public final boolean k; public final int l; public d m; public volatile Queue n; public volatile CompositeSubscription o; public volatile ConcurrentLinkedQueue p; public volatile boolean q; public boolean r; /* renamed from: s reason: collision with root package name */ public boolean f2836s; public final Object t = new Object(); public volatile c[] u = i; public long v; public long w; /* renamed from: x reason: collision with root package name */ public int f2837x; /* renamed from: y reason: collision with root package name */ public final int f2838y; /* renamed from: z reason: collision with root package name */ public int f2839z; public e(Subscriber subscriber, boolean z2, int i2) { this.j = subscriber; this.k = z2; this.l = i2; if (i2 == Integer.MAX_VALUE) { this.f2838y = Integer.MAX_VALUE; request(Long.MAX_VALUE); return; } this.f2838y = Math.max(1, i2 >> 1); request((long) i2); } public boolean a() { if (this.j.isUnsubscribed()) { return true; } ConcurrentLinkedQueue concurrentLinkedQueue = this.p; if (this.k || concurrentLinkedQueue == null || concurrentLinkedQueue.isEmpty()) { return false; } try { h(); return true; } finally { unsubscribe(); } } public void b() { synchronized (this) { if (this.r) { this.f2836s = true; return; } this.r = true; c(); } } /* JADX WARNING: Code restructure failed: missing block: B:121:0x0167, code lost: if ((r4 == null || r4.isEmpty()) != false) goto L_0x0169; */ /* JADX WARNING: Code restructure failed: missing block: B:157:0x01b4, code lost: r0 = th; */ /* JADX WARNING: Removed duplicated region for block: B:162:0x01bc */ public void c() { boolean z2; Throwable th; Subscriber subscriber; long j; int i2; Subscriber subscriber2; boolean z3; Throwable th2; int i3; long j2; try { subscriber = this.j; return; } catch (Throwable th3) { th = th3; z2 = false; if (!z2) { } throw th; } while (!a()) { Queue queue = this.n; long j3 = this.m.get(); boolean z4 = j3 == Long.MAX_VALUE; if (queue != null) { int i4 = 0; while (true) { j = j3; int i5 = 0; i2 = i4; Object obj = null; while (true) { if (j <= 0) { break; } Object poll = queue.poll(); if (!a()) { if (poll == null) { obj = poll; break; } try { subscriber.onNext((Object) e.b(poll)); } catch (Throwable th4) { th = th4; z2 = true; if (!z2) { synchronized (this) { this.r = false; } } throw th; } i2++; i5++; j--; obj = poll; } else { return; } } if (i5 > 0) { j = z4 ? Long.MAX_VALUE : this.m.a(i5); } if (j == 0 || obj == null) { break; } i4 = i2; j3 = j; } } else { j = j3; i2 = 0; } boolean z5 = this.q; Queue queue2 = this.n; c[] cVarArr = this.u; int length = cVarArr.length; if (!z5 || ((queue2 != null && !queue2.isEmpty()) || length != 0)) { if (length > 0) { long j4 = this.w; int i6 = this.f2837x; if (length <= i6 || cVarArr[i6].k != j4) { if (length <= i6) { i6 = 0; } for (int i7 = 0; i7 < length && cVarArr[i6].k != j4; i7++) { i6++; if (i6 == length) { i6 = 0; } } this.f2837x = i6; this.w = cVarArr[i6].k; } int i8 = 0; z3 = false; while (true) { if (i8 >= length) { subscriber2 = subscriber; break; } else if (!a()) { c cVar = cVarArr[i6]; Object obj2 = null; while (true) { int i9 = 0; while (j > 0) { if (!a()) { h hVar = cVar.m; if (hVar == null || (obj2 = hVar.c()) == null) { break; } try { subscriber.onNext((Object) e.b(obj2)); j--; i9++; } catch (Throwable th5) { unsubscribe(); throw th5; } } else { return; } } if (i9 > 0) { if (!z4) { j2 = this.m.a(i9); subscriber2 = subscriber; } else { subscriber2 = subscriber; j2 = Long.MAX_VALUE; } cVar.a((long) i9); j = j2; } else { subscriber2 = subscriber; } i3 = (j > 0 ? 1 : (j == 0 ? 0 : -1)); if (i3 == 0 || obj2 == null) { break; } subscriber = subscriber2; } boolean z6 = cVar.l; h hVar2 = cVar.m; if (z6) { if (hVar2 != null) { Queue queue3 = hVar2.j; } g(cVar); if (!a()) { i2++; z3 = true; } else { return; } } if (i3 == 0) { break; } i6++; if (i6 == length) { i6 = 0; } i8++; subscriber = subscriber2; } else { return; } } this.f2837x = i6; this.w = cVarArr[i6].k; } else { subscriber2 = subscriber; z3 = false; } if (i2 > 0) { request((long) i2); } if (!z3) { synchronized (this) { try { if (!this.f2836s) { try { this.r = false; return; } catch (Throwable th6) { th2 = th6; z2 = true; while (true) { try { break; } catch (Throwable th7) { th2 = th7; } } throw th2; } } else { this.f2836s = false; } } catch (Throwable th8) { th2 = th8; z2 = false; while (true) { break; } throw th2; } } } subscriber = subscriber2; } else { ConcurrentLinkedQueue concurrentLinkedQueue = this.p; if (concurrentLinkedQueue == null || concurrentLinkedQueue.isEmpty()) { subscriber.onCompleted(); return; } else { h(); return; } } } } public Queue d() { ConcurrentLinkedQueue concurrentLinkedQueue = this.p; if (concurrentLinkedQueue == null) { synchronized (this) { concurrentLinkedQueue = this.p; if (concurrentLinkedQueue == null) { concurrentLinkedQueue = new ConcurrentLinkedQueue<>(); this.p = concurrentLinkedQueue; } } } return concurrentLinkedQueue; } public void e(T t) { Queue queue = this.n; if (queue == null) { int i2 = this.l; if (i2 == Integer.MAX_VALUE) { queue = new g<>(h.i); } else { queue = ((i2 + -1) & i2) == 0 ? y.b() ? new n<>(i2) : new j0.l.e.m.c<>(i2) : new j0.l.e.m.d<>(i2); } this.n = queue; } if (!queue.offer(t == null ? e.b : t)) { unsubscribe(); d().offer(OnErrorThrowable.a(new MissingBackpressureException(), t)); this.q = true; b(); } } public void f(c cVar, T t) { h hVar = cVar.m; if (hVar == null) { hVar = y.b() ? new h(false, h.i) : new h(); cVar.add(hVar); cVar.m = hVar; } if (t == null) { try { t = (T) e.b; } catch (MissingBackpressureException e) { cVar.unsubscribe(); cVar.onError(e); return; } catch (IllegalStateException e2) { if (!cVar.isUnsubscribed()) { cVar.unsubscribe(); cVar.onError(e2); return; } return; } } hVar.a(t); } public void g(c cVar) { h hVar = cVar.m; if (hVar != null) { synchronized (hVar) { } } this.o.c(cVar); synchronized (this.t) { c[] cVarArr = this.u; int length = cVarArr.length; int i2 = 0; while (true) { if (i2 >= length) { i2 = -1; break; } else if (cVar.equals(cVarArr[i2])) { break; } else { i2++; } } if (i2 >= 0) { if (length == 1) { this.u = i; return; } c[] cVarArr2 = new c[(length - 1)]; System.arraycopy(cVarArr, 0, cVarArr2, 0, i2); System.arraycopy(cVarArr, i2 + 1, cVarArr2, i2, (length - i2) - 1); this.u = cVarArr2; } } } public final void h() { ArrayList arrayList = new ArrayList(this.p); if (arrayList.size() == 1) { this.j.onError((Throwable) arrayList.get(0)); } else { this.j.onError(new CompositeException(arrayList)); } } @Override // j0.g public void onCompleted() { this.q = true; b(); } @Override // j0.g public void onError(Throwable th) { d().offer(th); this.q = true; b(); } @Override // j0.g public void onNext(Object obj) { boolean z2; boolean z3; Throwable th; Observable observable = (Observable) obj; if (observable != null) { z2 = true; if (observable == c.j) { int i2 = this.f2839z + 1; if (i2 == this.f2838y) { this.f2839z = 0; request((long) i2); return; } this.f2839z = i2; return; } else if (observable instanceof j) { T t = ((j) observable).k; long j = this.m.get(); if (j != 0) { synchronized (this) { j = this.m.get(); if (this.r || j == 0) { z3 = false; } else { this.r = true; z3 = true; } } } else { z3 = false; } if (z3) { Queue queue = this.n; if (queue == null || queue.isEmpty()) { try { this.j.onNext(t); } catch (Throwable th2) { th = th2; } if (j != Long.MAX_VALUE) { this.m.a(1); } int i3 = this.f2839z + 1; if (i3 == this.f2838y) { this.f2839z = 0; request((long) i3); } else { this.f2839z = i3; } synchronized (this) { if (!this.f2836s) { this.r = false; return; } this.f2836s = false; c(); return; } } e(t); c(); return; } e(t); b(); return; } else { long j2 = this.v; this.v = 1 + j2; c cVar = new c<>(this, j2); CompositeSubscription compositeSubscription = this.o; if (compositeSubscription == null) { synchronized (this) { compositeSubscription = this.o; if (compositeSubscription == null) { compositeSubscription = new CompositeSubscription(); this.o = compositeSubscription; } else { z2 = false; } } if (z2) { add(compositeSubscription); } } compositeSubscription.a(cVar); synchronized (this.t) { c[] cVarArr = this.u; int length = cVarArr.length; c[] cVarArr2 = new c[(length + 1)]; System.arraycopy(cVarArr, 0, cVarArr2, 0, length); cVarArr2[length] = cVar; this.u = cVarArr2; } observable.i0(cVar); b(); return; } } else { return; } if (!z2) { synchronized (this) { this.r = false; } } throw th; } } public x0(boolean z2, int i) { this.i = z2; } @Override // j0.k.b public Object call(Object obj) { Subscriber subscriber = (Subscriber) obj; e eVar = new e(subscriber, this.i, Integer.MAX_VALUE); d dVar = new d<>(eVar); eVar.m = dVar; subscriber.add(eVar); subscriber.setProducer(dVar); return eVar; } }