package io.reactivex.subjects; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.internal.util.AppendOnlyLinkedArrayList; import io.reactivex.internal.util.NotificationLite; import io.reactivex.plugins.RxJavaPlugins; /* loaded from: classes2.dex */ final class SerializedSubject extends Subject implements AppendOnlyLinkedArrayList.NonThrowingPredicate { final Subject a; boolean b; AppendOnlyLinkedArrayList c; volatile boolean d; SerializedSubject(Subject subject) { this.a = subject; } @Override // io.reactivex.internal.util.AppendOnlyLinkedArrayList.NonThrowingPredicate, io.reactivex.functions.Predicate public boolean a(Object obj) { return NotificationLite.acceptFull(obj, this.a); } void b() { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList; while (true) { synchronized (this) { appendOnlyLinkedArrayList = this.c; if (appendOnlyLinkedArrayList == null) { this.b = false; return; } this.c = null; } appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList.NonThrowingPredicate) this); } } @Override // io.reactivex.Observer public void onComplete() { if (this.d) { return; } synchronized (this) { if (this.d) { return; } this.d = true; if (!this.b) { this.b = true; this.a.onComplete(); return; } AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.c; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.c = appendOnlyLinkedArrayList; } appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList) NotificationLite.complete()); } } @Override // io.reactivex.Observer public void onError(Throwable th) { boolean z; if (this.d) { RxJavaPlugins.b(th); return; } synchronized (this) { if (this.d) { z = true; } else { this.d = true; if (this.b) { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.c; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.c = appendOnlyLinkedArrayList; } appendOnlyLinkedArrayList.b(NotificationLite.error(th)); return; } z = false; this.b = true; } if (z) { RxJavaPlugins.b(th); } else { this.a.onError(th); } } } @Override // io.reactivex.Observer public void onNext(T t) { if (this.d) { return; } synchronized (this) { if (this.d) { return; } if (!this.b) { this.b = true; this.a.onNext(t); b(); } else { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.c; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.c = appendOnlyLinkedArrayList; } appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList) NotificationLite.next(t)); } } } @Override // io.reactivex.Observer public void onSubscribe(Disposable disposable) { boolean z = true; if (!this.d) { synchronized (this) { if (!this.d) { if (this.b) { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.c; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.c = appendOnlyLinkedArrayList; } appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList) NotificationLite.disposable(disposable)); return; } this.b = true; z = false; } } } if (z) { disposable.dispose(); } else { this.a.onSubscribe(disposable); b(); } } @Override // io.reactivex.Observable protected void subscribeActual(Observer observer) { this.a.subscribe(observer); } }