package i.a.o;

import i.a.g;
import i.a.k.b;
import i.a.n.h.d;

/* JADX INFO: compiled from: SerializedObserver.java */
/* JADX INFO: loaded from: classes.dex */
public final class a<T> implements g<T>, b {
    final g<? super T> b;
    final boolean d;
    b e;
    boolean f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    i.a.n.h.a<Object> f876g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    volatile boolean f877h;

    public a(g<? super T> gVar) {
        this(gVar, false);
    }

    @Override // i.a.g
    public void a(Throwable th) {
        if (this.f877h) {
            i.a.p.a.l(th);
            return;
        }
        synchronized (this) {
            boolean z = true;
            if (!this.f877h) {
                if (this.f) {
                    this.f877h = true;
                    i.a.n.h.a<Object> aVar = this.f876g;
                    if (aVar == null) {
                        aVar = new i.a.n.h.a<>(4);
                        this.f876g = aVar;
                    }
                    Object objError = d.error(th);
                    if (this.d) {
                        aVar.b(objError);
                    } else {
                        aVar.c(objError);
                    }
                    return;
                }
                this.f877h = true;
                this.f = true;
                z = false;
            }
            if (z) {
                i.a.p.a.l(th);
            } else {
                this.b.a(th);
            }
        }
    }

    @Override // i.a.g
    public void b() {
        if (this.f877h) {
            return;
        }
        synchronized (this) {
            if (this.f877h) {
                return;
            }
            if (!this.f) {
                this.f877h = true;
                this.f = true;
                this.b.b();
            } else {
                i.a.n.h.a<Object> aVar = this.f876g;
                if (aVar == null) {
                    aVar = new i.a.n.h.a<>(4);
                    this.f876g = aVar;
                }
                aVar.b(d.complete());
            }
        }
    }

    @Override // i.a.g
    public void c(b bVar) {
        if (i.a.n.a.b.validate(this.e, bVar)) {
            this.e = bVar;
            this.b.c(this);
        }
    }

    @Override // i.a.g
    public void d(T t) {
        if (this.f877h) {
            return;
        }
        if (t == null) {
            this.e.dispose();
            a(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
            return;
        }
        synchronized (this) {
            if (this.f877h) {
                return;
            }
            if (!this.f) {
                this.f = true;
                this.b.d(t);
                e();
            } else {
                i.a.n.h.a<Object> aVar = this.f876g;
                if (aVar == null) {
                    aVar = new i.a.n.h.a<>(4);
                    this.f876g = aVar;
                }
                aVar.b(d.next(t));
            }
        }
    }

    @Override // i.a.k.b
    public void dispose() {
        this.e.dispose();
    }

    void e() {
        i.a.n.h.a<Object> aVar;
        do {
            synchronized (this) {
                aVar = this.f876g;
                if (aVar == null) {
                    this.f = false;
                    return;
                }
                this.f876g = null;
            }
        } while (!aVar.a(this.b));
    }

    @Override // i.a.k.b
    public boolean isDisposed() {
        return this.e.isDisposed();
    }

    public a(g<? super T> gVar, boolean z) {
        this.b = gVar;
        this.d = z;
    }
}
