package io.reactivex.observers; import io.reactivex.Observer; import io.reactivex.disposables.Disposable; import io.reactivex.internal.disposables.DisposableHelper; import io.reactivex.internal.util.AppendOnlyLinkedArrayList; import io.reactivex.internal.util.NotificationLite; import io.reactivex.plugins.RxJavaPlugins; /* loaded from: classes2.dex */ public final class SerializedObserver implements Observer, Disposable { final Observer a; final boolean b; Disposable c; boolean d; AppendOnlyLinkedArrayList e; volatile boolean f; public SerializedObserver(Observer observer) { this(observer, false); } void a() { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList; do { synchronized (this) { appendOnlyLinkedArrayList = this.e; if (appendOnlyLinkedArrayList == null) { this.d = false; return; } this.e = null; } } while (!appendOnlyLinkedArrayList.a((Observer) this.a)); } @Override // io.reactivex.disposables.Disposable public void dispose() { this.c.dispose(); } @Override // io.reactivex.Observer public void onComplete() { if (this.f) { return; } synchronized (this) { if (this.f) { return; } if (!this.d) { this.f = true; this.d = true; this.a.onComplete(); } else { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.e; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.e = appendOnlyLinkedArrayList; } appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList) NotificationLite.complete()); } } } @Override // io.reactivex.Observer public void onError(Throwable th) { if (this.f) { RxJavaPlugins.b(th); return; } synchronized (this) { boolean z = true; if (!this.f) { if (this.d) { this.f = true; AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.e; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.e = appendOnlyLinkedArrayList; } Object error = NotificationLite.error(th); if (this.b) { appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList) error); } else { appendOnlyLinkedArrayList.b(error); } return; } this.f = true; this.d = true; z = false; } if (z) { RxJavaPlugins.b(th); } else { this.a.onError(th); } } } @Override // io.reactivex.Observer public void onNext(T t) { if (this.f) { return; } if (t == null) { this.c.dispose(); onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources.")); return; } synchronized (this) { if (this.f) { return; } if (!this.d) { this.d = true; this.a.onNext(t); a(); } else { AppendOnlyLinkedArrayList appendOnlyLinkedArrayList = this.e; if (appendOnlyLinkedArrayList == null) { appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4); this.e = appendOnlyLinkedArrayList; } appendOnlyLinkedArrayList.a((AppendOnlyLinkedArrayList) NotificationLite.next(t)); } } } @Override // io.reactivex.Observer public void onSubscribe(Disposable disposable) { if (DisposableHelper.validate(this.c, disposable)) { this.c = disposable; this.a.onSubscribe(this); } } public SerializedObserver(Observer observer, boolean z) { this.a = observer; this.b = z; } }