package f.a.w.e.d;

import f.a.o;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: compiled from: ObservableTimeoutTimed.java */
/* JADX INFO: loaded from: classes.dex */
public final class j0<T> extends f.a.w.e.d.a<T, T> {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final long f3811e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final TimeUnit f3812f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final f.a.o f3813g;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final f.a.l<? extends T> f3814h;

    /* JADX INFO: compiled from: ObservableTimeoutTimed.java */
    public static final class a<T> implements f.a.n<T> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final f.a.n<? super T> f3815b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final AtomicReference<f.a.t.b> f3816e;

        public a(f.a.n<? super T> nVar, AtomicReference<f.a.t.b> atomicReference) {
            this.f3815b = nVar;
            this.f3816e = atomicReference;
        }

        @Override // f.a.n
        public void a(Throwable th) {
            this.f3815b.a(th);
        }

        @Override // f.a.n
        public void b(f.a.t.b bVar) {
            f.a.w.a.b.replace(this.f3816e, bVar);
        }

        @Override // f.a.n
        public void c(T t) {
            this.f3815b.c(t);
        }

        @Override // f.a.n
        public void onComplete() {
            this.f3815b.onComplete();
        }
    }

    /* JADX INFO: compiled from: ObservableTimeoutTimed.java */
    public static final class b<T> extends AtomicReference<f.a.t.b> implements f.a.n<T>, f.a.t.b, d {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final f.a.n<? super T> f3817b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final long f3818e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final TimeUnit f3819f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public final o.c f3820g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public final f.a.w.a.f f3821h = new f.a.w.a.f();

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public final AtomicLong f3822i = new AtomicLong();

        /* JADX INFO: renamed from: j, reason: collision with root package name */
        public final AtomicReference<f.a.t.b> f3823j = new AtomicReference<>();

        /* JADX INFO: renamed from: k, reason: collision with root package name */
        public f.a.l<? extends T> f3824k;

        public b(f.a.n<? super T> nVar, long j2, TimeUnit timeUnit, o.c cVar, f.a.l<? extends T> lVar) {
            this.f3817b = nVar;
            this.f3818e = j2;
            this.f3819f = timeUnit;
            this.f3820g = cVar;
            this.f3824k = lVar;
        }

        @Override // f.a.n
        public void a(Throwable th) {
            if (this.f3822i.getAndSet(Long.MAX_VALUE) == Long.MAX_VALUE) {
                f.a.y.a.C0(th);
                return;
            }
            f.a.w.a.f fVar = this.f3821h;
            Objects.requireNonNull(fVar);
            f.a.w.a.b.dispose(fVar);
            this.f3817b.a(th);
            this.f3820g.dispose();
        }

        @Override // f.a.n
        public void b(f.a.t.b bVar) {
            f.a.w.a.b.setOnce(this.f3823j, bVar);
        }

        @Override // f.a.n
        public void c(T t) {
            long j2 = this.f3822i.get();
            if (j2 != Long.MAX_VALUE) {
                long j3 = 1 + j2;
                if (this.f3822i.compareAndSet(j2, j3)) {
                    this.f3821h.get().dispose();
                    this.f3817b.c(t);
                    e(j3);
                }
            }
        }

        @Override // f.a.w.e.d.j0.d
        public void d(long j2) {
            if (this.f3822i.compareAndSet(j2, Long.MAX_VALUE)) {
                f.a.w.a.b.dispose(this.f3823j);
                f.a.l<? extends T> lVar = this.f3824k;
                this.f3824k = null;
                lVar.d(new a(this.f3817b, this));
                this.f3820g.dispose();
            }
        }

        @Override // f.a.t.b
        public void dispose() {
            f.a.w.a.b.dispose(this.f3823j);
            f.a.w.a.b.dispose(this);
            this.f3820g.dispose();
        }

        public void e(long j2) {
            f.a.w.a.f fVar = this.f3821h;
            f.a.t.b bVarC = this.f3820g.c(new e(j2, this), this.f3818e, this.f3819f);
            Objects.requireNonNull(fVar);
            f.a.w.a.b.replace(fVar, bVarC);
        }

        @Override // f.a.n
        public void onComplete() {
            if (this.f3822i.getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
                f.a.w.a.f fVar = this.f3821h;
                Objects.requireNonNull(fVar);
                f.a.w.a.b.dispose(fVar);
                this.f3817b.onComplete();
                this.f3820g.dispose();
            }
        }
    }

    /* JADX INFO: compiled from: ObservableTimeoutTimed.java */
    public static final class c<T> extends AtomicLong implements f.a.n<T>, f.a.t.b, d {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final f.a.n<? super T> f3825b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final long f3826e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        public final TimeUnit f3827f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        public final o.c f3828g;

        /* JADX INFO: renamed from: h, reason: collision with root package name */
        public final f.a.w.a.f f3829h = new f.a.w.a.f();

        /* JADX INFO: renamed from: i, reason: collision with root package name */
        public final AtomicReference<f.a.t.b> f3830i = new AtomicReference<>();

        public c(f.a.n<? super T> nVar, long j2, TimeUnit timeUnit, o.c cVar) {
            this.f3825b = nVar;
            this.f3826e = j2;
            this.f3827f = timeUnit;
            this.f3828g = cVar;
        }

        @Override // f.a.n
        public void a(Throwable th) {
            if (getAndSet(Long.MAX_VALUE) == Long.MAX_VALUE) {
                f.a.y.a.C0(th);
                return;
            }
            f.a.w.a.f fVar = this.f3829h;
            Objects.requireNonNull(fVar);
            f.a.w.a.b.dispose(fVar);
            this.f3825b.a(th);
            this.f3828g.dispose();
        }

        @Override // f.a.n
        public void b(f.a.t.b bVar) {
            f.a.w.a.b.setOnce(this.f3830i, bVar);
        }

        @Override // f.a.n
        public void c(T t) {
            long j2 = get();
            if (j2 != Long.MAX_VALUE) {
                long j3 = 1 + j2;
                if (compareAndSet(j2, j3)) {
                    this.f3829h.get().dispose();
                    this.f3825b.c(t);
                    e(j3);
                }
            }
        }

        @Override // f.a.w.e.d.j0.d
        public void d(long j2) {
            if (compareAndSet(j2, Long.MAX_VALUE)) {
                f.a.w.a.b.dispose(this.f3830i);
                f.a.n<? super T> nVar = this.f3825b;
                long j3 = this.f3826e;
                TimeUnit timeUnit = this.f3827f;
                Throwable th = f.a.w.h.c.a;
                nVar.a(new TimeoutException("The source did not signal an event for " + j3 + " " + timeUnit.toString().toLowerCase() + " and has been terminated."));
                this.f3828g.dispose();
            }
        }

        @Override // f.a.t.b
        public void dispose() {
            f.a.w.a.b.dispose(this.f3830i);
            this.f3828g.dispose();
        }

        public void e(long j2) {
            f.a.w.a.f fVar = this.f3829h;
            f.a.t.b bVarC = this.f3828g.c(new e(j2, this), this.f3826e, this.f3827f);
            Objects.requireNonNull(fVar);
            f.a.w.a.b.replace(fVar, bVarC);
        }

        @Override // f.a.n
        public void onComplete() {
            if (getAndSet(Long.MAX_VALUE) != Long.MAX_VALUE) {
                f.a.w.a.f fVar = this.f3829h;
                Objects.requireNonNull(fVar);
                f.a.w.a.b.dispose(fVar);
                this.f3825b.onComplete();
                this.f3828g.dispose();
            }
        }
    }

    /* JADX INFO: compiled from: ObservableTimeoutTimed.java */
    public interface d {
        void d(long j2);
    }

    /* JADX INFO: compiled from: ObservableTimeoutTimed.java */
    public static final class e implements Runnable {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public final d f3831b;

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        public final long f3832e;

        public e(long j2, d dVar) {
            this.f3832e = j2;
            this.f3831b = dVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            this.f3831b.d(this.f3832e);
        }
    }

    public j0(f.a.i<T> iVar, long j2, TimeUnit timeUnit, f.a.o oVar, f.a.l<? extends T> lVar) {
        super(iVar);
        this.f3811e = j2;
        this.f3812f = timeUnit;
        this.f3813g = oVar;
        this.f3814h = lVar;
    }

    @Override // f.a.i
    public void v(f.a.n<? super T> nVar) {
        if (this.f3814h == null) {
            c cVar = new c(nVar, this.f3811e, this.f3812f, this.f3813g.a());
            nVar.b(cVar);
            cVar.e(0L);
            this.f3744b.d(cVar);
            return;
        }
        b bVar = new b(nVar, this.f3811e, this.f3812f, this.f3813g.a(), this.f3814h);
        nVar.b(bVar);
        bVar.e(0L);
        this.f3744b.d(bVar);
    }
}
