package com.google.android.gms.internal.ads;

import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Level;
import java.util.logging.Logger;

/* JADX INFO: loaded from: classes2.dex */
public abstract class cu1 extends gu1 {
    /* JADX WARN: Multi-variable type inference failed */
    public static Object h(com.google.common.util.concurrent.w wVar) {
        Throwable thA;
        if (wVar instanceof au1) {
            Object wt1Var = ((cu1) wVar).f11887a;
            if (wt1Var instanceof wt1) {
                wt1 wt1Var2 = (wt1) wt1Var;
                if (wt1Var2.f18231a) {
                    Throwable th = wt1Var2.f18232b;
                    wt1Var = th != null ? new wt1(false, th) : wt1.f18230d;
                }
            }
            Objects.requireNonNull(wt1Var);
            return wt1Var;
        }
        if ((wVar instanceof pv1) && (thA = ((pv1) wVar).a()) != null) {
            return new yt1(thA);
        }
        boolean zIsCancelled = wVar.isCancelled();
        if ((!gu1.f11885f) && zIsCancelled) {
            wt1 wt1Var3 = wt1.f18230d;
            Objects.requireNonNull(wt1Var3);
            return wt1Var3;
        }
        try {
            Object objI = i(wVar);
            if (!zIsCancelled) {
                return objI == null ? gu1.f11883d : objI;
            }
            String strValueOf = String.valueOf(wVar);
            StringBuilder sb = new StringBuilder(strValueOf.length() + 84);
            sb.append("get() did not throw CancellationException, despite reporting isCancelled() == true: ");
            sb.append(strValueOf);
            return new wt1(false, new IllegalArgumentException(sb.toString()));
        } catch (Error e10) {
            e = e10;
            return new yt1(e);
        } catch (CancellationException e11) {
            return !zIsCancelled ? new yt1(new IllegalArgumentException("get() threw CancellationException, despite reporting isCancelled() == false: ".concat(String.valueOf(wVar)), e11)) : new wt1(false, e11);
        } catch (ExecutionException e12) {
            return zIsCancelled ? new wt1(false, new IllegalArgumentException("get() did not throw CancellationException, despite reporting isCancelled() == true: ".concat(String.valueOf(wVar)), e12)) : new yt1(e12.getCause());
        } catch (Exception e13) {
            e = e13;
            return new yt1(e);
        }
    }

    public static Object i(Future future) {
        Object obj;
        boolean z10 = false;
        while (true) {
            try {
                obj = future.get();
                break;
            } catch (InterruptedException unused) {
                z10 = true;
            } catch (Throwable th) {
                if (z10) {
                    Thread.currentThread().interrupt();
                }
                throw th;
            }
        }
        if (z10) {
            Thread.currentThread().interrupt();
        }
        return obj;
    }

    public static Object j(Object obj) throws ExecutionException {
        if (obj instanceof wt1) {
            Throwable th = ((wt1) obj).f18232b;
            CancellationException cancellationException = new CancellationException("Task was cancelled.");
            cancellationException.initCause(th);
            throw cancellationException;
        }
        if (obj instanceof yt1) {
            throw new ExecutionException(((yt1) obj).f18976a);
        }
        if (obj == gu1.f11883d) {
            return null;
        }
        return obj;
    }

    public static boolean k(Object obj) {
        return !(obj instanceof xt1);
    }

    public static void p(cu1 cu1Var, boolean z10) {
        zt1 zt1Var = null;
        while (true) {
            cu1Var.getClass();
            for (fu1 fu1VarW = gu1.f11886g.w(cu1Var); fu1VarW != null; fu1VarW = fu1VarW.f11393b) {
                Thread thread = fu1VarW.f11392a;
                if (thread != null) {
                    fu1VarW.f11392a = null;
                    LockSupport.unpark(thread);
                }
            }
            if (z10) {
                cu1Var.l();
            }
            cu1Var.e();
            zt1 zt1Var2 = zt1Var;
            zt1 zt1VarX = gu1.f11886g.x(cu1Var);
            zt1 zt1Var3 = zt1Var2;
            while (zt1VarX != null) {
                zt1 zt1Var4 = zt1VarX.f19395c;
                zt1VarX.f19395c = zt1Var3;
                zt1Var3 = zt1VarX;
                zt1VarX = zt1Var4;
            }
            while (zt1Var3 != null) {
                Runnable runnable = zt1Var3.f19393a;
                zt1Var = zt1Var3.f19395c;
                Objects.requireNonNull(runnable);
                if (runnable instanceof xt1) {
                    xt1 xt1Var = (xt1) runnable;
                    cu1Var = xt1Var.f18575a;
                    if (cu1Var.f11887a == xt1Var) {
                        if (gu1.f11886g.y(cu1Var, xt1Var, h(xt1Var.f18576b))) {
                            break;
                        }
                    } else {
                        continue;
                    }
                } else {
                    Executor executor = zt1Var3.f19394b;
                    Objects.requireNonNull(executor);
                    r(runnable, executor);
                }
                zt1Var3 = zt1Var;
            }
            return;
            z10 = false;
        }
    }

    public static void r(Runnable runnable, Executor executor) {
        try {
            executor.execute(runnable);
        } catch (Exception e10) {
            Logger loggerB = gu1.f11884e.b();
            Level level = Level.SEVERE;
            String strValueOf = String.valueOf(runnable);
            String strValueOf2 = String.valueOf(executor);
            loggerB.logp(level, "com.google.common.util.concurrent.AbstractFuture", "executeListener", com.bytedance.sdk.component.adexpress.dynamic.dynamicview.b.h(new StringBuilder(strValueOf.length() + 57 + strValueOf2.length()), "RuntimeException while executing runnable ", strValueOf, " with executor ", strValueOf2), (Throwable) e10);
        }
    }

    @Override // com.google.android.gms.internal.ads.pv1
    public final Throwable a() {
        if (!(this instanceof au1)) {
            return null;
        }
        Object obj = this.f11887a;
        if (obj instanceof yt1) {
            return ((yt1) obj).f18976a;
        }
        return null;
    }

    public boolean c(Object obj) {
        if (obj == null) {
            obj = gu1.f11883d;
        }
        if (!gu1.f11886g.y(this, null, obj)) {
            return false;
        }
        p(this, false);
        return true;
    }

    @Override // java.util.concurrent.Future
    public boolean cancel(boolean z10) {
        wt1 wt1Var;
        Object obj = this.f11887a;
        if (!(obj instanceof xt1) && !(obj == null)) {
            return false;
        }
        if (gu1.f11885f) {
            wt1Var = new wt1(z10, new CancellationException("Future.cancel() was called."));
        } else {
            wt1Var = z10 ? wt1.f18229c : wt1.f18230d;
            Objects.requireNonNull(wt1Var);
        }
        cu1 cu1Var = this;
        boolean z11 = false;
        while (true) {
            if (gu1.f11886g.y(cu1Var, obj, wt1Var)) {
                p(cu1Var, z10);
                if (!(obj instanceof xt1)) {
                    break;
                }
                com.google.common.util.concurrent.w wVar = ((xt1) obj).f18576b;
                if (!(wVar instanceof au1)) {
                    wVar.cancel(z10);
                    break;
                }
                cu1Var = (cu1) wVar;
                obj = cu1Var.f11887a;
                if (!(obj == null) && !(obj instanceof xt1)) {
                    break;
                }
                z11 = true;
            } else {
                obj = cu1Var.f11887a;
                if (k(obj)) {
                    return z11;
                }
            }
        }
        return true;
    }

    public boolean d(Throwable th) {
        th.getClass();
        if (!gu1.f11886g.y(this, null, new yt1(th))) {
            return false;
        }
        p(this, false);
        return true;
    }

    public void e() {
    }

    /* JADX WARN: Multi-variable type inference failed */
    public String f() {
        if (!(this instanceof ScheduledFuture)) {
            return null;
        }
        long delay = ((ScheduledFuture) this).getDelay(TimeUnit.MILLISECONDS);
        StringBuilder sb = new StringBuilder(String.valueOf(delay).length() + 21);
        sb.append("remaining delay=[");
        sb.append(delay);
        sb.append(" ms]");
        return sb.toString();
    }

    @Override // com.google.common.util.concurrent.w
    public void g(Runnable runnable, Executor executor) {
        zt1 zt1Var;
        zt1 zt1Var2;
        if (runnable == null) {
            throw new NullPointerException("Runnable was null.");
        }
        if (executor == null) {
            throw new NullPointerException("Executor was null.");
        }
        if (!isDone() && (zt1Var = this.f11888b) != (zt1Var2 = zt1.f19392d)) {
            zt1 zt1Var3 = new zt1(runnable, executor);
            do {
                zt1Var3.f19395c = zt1Var;
                if (gu1.f11886g.v(this, zt1Var, zt1Var3)) {
                    return;
                } else {
                    zt1Var = this.f11888b;
                }
            } while (zt1Var != zt1Var2);
        }
        r(runnable, executor);
    }

    @Override // java.util.concurrent.Future
    public Object get() throws InterruptedException {
        Object obj;
        if (Thread.interrupted()) {
            throw new InterruptedException();
        }
        Object obj2 = this.f11887a;
        if ((obj2 != null) && k(obj2)) {
            return j(obj2);
        }
        fu1 fu1Var = this.f11889c;
        fu1 fu1Var2 = fu1.f11391c;
        if (fu1Var != fu1Var2) {
            fu1 fu1Var3 = new fu1();
            do {
                rb rbVar = gu1.f11886g;
                rbVar.s(fu1Var3, fu1Var);
                if (rbVar.u(this, fu1Var, fu1Var3)) {
                    do {
                        LockSupport.park(this);
                        if (Thread.interrupted()) {
                            b(fu1Var3);
                            throw new InterruptedException();
                        }
                        obj = this.f11887a;
                    } while (!((obj != null) & k(obj)));
                    return j(obj);
                }
                fu1Var = this.f11889c;
            } while (fu1Var != fu1Var2);
        }
        Object obj3 = this.f11887a;
        Objects.requireNonNull(obj3);
        return j(obj3);
    }

    @Override // java.util.concurrent.Future
    public boolean isCancelled() {
        return this.f11887a instanceof wt1;
    }

    @Override // java.util.concurrent.Future
    public boolean isDone() {
        Object obj = this.f11887a;
        return (obj != null) & k(obj);
    }

    public void l() {
    }

    public final boolean m() {
        Object obj = this.f11887a;
        return (obj instanceof wt1) && ((wt1) obj).f18231a;
    }

    public final void n(com.google.common.util.concurrent.w wVar) {
        yt1 yt1Var;
        wVar.getClass();
        Object obj = this.f11887a;
        if (obj == null) {
            if (wVar.isDone()) {
                if (gu1.f11886g.y(this, null, h(wVar))) {
                    p(this, false);
                    return;
                }
                return;
            }
            xt1 xt1Var = new xt1(this, wVar);
            if (gu1.f11886g.y(this, null, xt1Var)) {
                try {
                    wVar.g(xt1Var, zzgzd.f19813a);
                    return;
                } catch (Throwable th) {
                    try {
                        yt1Var = new yt1(th);
                    } catch (Error | Exception unused) {
                        yt1Var = yt1.f18975b;
                    }
                    gu1.f11886g.y(this, xt1Var, yt1Var);
                    return;
                }
            }
            obj = this.f11887a;
        }
        if (obj instanceof wt1) {
            wVar.cancel(((wt1) obj).f18231a);
        }
    }

    public final void o(com.google.common.util.concurrent.w wVar) {
        if ((wVar != null) && (this.f11887a instanceof wt1)) {
            wVar.cancel(m());
        }
    }

    public final void q(StringBuilder sb) {
        try {
            Object objI = i(this);
            sb.append("SUCCESS, result=[");
            if (objI == null) {
                sb.append("null");
            } else if (objI == this) {
                sb.append("this future");
            } else {
                sb.append(objI.getClass().getName());
                sb.append("@");
                sb.append(Integer.toHexString(System.identityHashCode(objI)));
            }
            sb.append("]");
        } catch (CancellationException unused) {
            sb.append("CANCELLED");
        } catch (ExecutionException e10) {
            sb.append("FAILURE, cause=[");
            sb.append(e10.getCause());
            sb.append("]");
        } catch (Exception e11) {
            sb.append("UNKNOWN, cause=[");
            sb.append(e11.getClass());
            sb.append(" thrown from get()]");
        }
    }

    public String toString() {
        String strConcat;
        StringBuilder sb = new StringBuilder();
        if (getClass().getName().startsWith("com.google.common.util.concurrent.")) {
            sb.append(getClass().getSimpleName());
        } else {
            sb.append(getClass().getName());
        }
        sb.append('@');
        sb.append(Integer.toHexString(System.identityHashCode(this)));
        sb.append("[status=");
        if (isCancelled()) {
            sb.append("CANCELLED");
        } else if (isDone()) {
            q(sb);
        } else {
            int length = sb.length();
            sb.append("PENDING");
            Object obj = this.f11887a;
            if (obj instanceof xt1) {
                sb.append(", setFuture=[");
                com.google.common.util.concurrent.w wVar = ((xt1) obj).f18576b;
                try {
                    if (wVar == this) {
                        sb.append("this future");
                    } else {
                        sb.append(wVar);
                    }
                } catch (Throwable th) {
                    if ((th instanceof Error) && !(th instanceof StackOverflowError)) {
                        throw th;
                    }
                    sb.append("Exception thrown from implementation: ");
                    sb.append(th.getClass());
                }
                sb.append("]");
            } else {
                try {
                    strConcat = f();
                    if (ih1.B(strConcat)) {
                        strConcat = null;
                    }
                } catch (Throwable th2) {
                    if ((th2 instanceof Error) && !(th2 instanceof StackOverflowError)) {
                        throw th2;
                    }
                    strConcat = "Exception thrown from implementation: ".concat(String.valueOf(th2.getClass()));
                }
                if (strConcat != null) {
                    sb.append(", info=[");
                    sb.append(strConcat);
                    sb.append("]");
                }
            }
            if (isDone()) {
                sb.delete(length, sb.length());
                q(sb);
            }
        }
        sb.append("]");
        return sb.toString();
    }

    /* JADX WARN: Removed duplicated region for block: B:45:0x00a2  */
    /* JADX WARN: Removed duplicated region for block: B:58:0x00c8  */
    /* JADX WARN: Unsupported multi-entry loop pattern (BACK_EDGE: B:55:0x00bb -> B:34:0x007f). Please report as a decompilation issue!!! */
    @Override // java.util.concurrent.Future
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object get(long j10, TimeUnit timeUnit) throws InterruptedException, TimeoutException {
        long j11;
        long nanos = timeUnit.toNanos(j10);
        if (!Thread.interrupted()) {
            Object obj = this.f11887a;
            if ((obj != null) & k(obj)) {
                return j(obj);
            }
            long jNanoTime = nanos > 0 ? System.nanoTime() + nanos : 0L;
            if (nanos >= 1000) {
                fu1 fu1Var = this.f11889c;
                fu1 fu1Var2 = fu1.f11391c;
                if (fu1Var != fu1Var2) {
                    fu1 fu1Var3 = new fu1();
                    do {
                        rb rbVar = gu1.f11886g;
                        rbVar.s(fu1Var3, fu1Var);
                        if (rbVar.u(this, fu1Var, fu1Var3)) {
                            do {
                                LockSupport.parkNanos(this, Math.min(nanos, 2147483647999999999L));
                                if (!Thread.interrupted()) {
                                    Object obj2 = this.f11887a;
                                    if ((obj2 != null) & k(obj2)) {
                                        return j(obj2);
                                    }
                                    nanos = jNanoTime - System.nanoTime();
                                } else {
                                    b(fu1Var3);
                                    throw new InterruptedException();
                                }
                            } while (nanos >= 1000);
                            b(fu1Var3);
                            j11 = 0;
                            if (nanos > j11) {
                                Object obj3 = this.f11887a;
                                if ((obj3 != null) & k(obj3)) {
                                    return j(obj3);
                                }
                                if (!Thread.interrupted()) {
                                    nanos = jNanoTime - System.nanoTime();
                                    j11 = 0;
                                    if (nanos > j11) {
                                    }
                                } else {
                                    throw new InterruptedException();
                                }
                            } else {
                                String string = toString();
                                String string2 = timeUnit.toString();
                                Locale locale = Locale.ROOT;
                                String lowerCase = string2.toLowerCase(locale);
                                String lowerCase2 = timeUnit.toString().toLowerCase(locale);
                                StringBuilder sb = new StringBuilder(String.valueOf(j10).length() + 8 + String.valueOf(lowerCase2).length());
                                defpackage.e.B(sb, "Waited ", j10, " ");
                                sb.append(lowerCase2);
                                String string3 = sb.toString();
                                if (nanos + 1000 < 0) {
                                    String strConcat = string3.concat(" (plus ");
                                    long j12 = -nanos;
                                    long jConvert = timeUnit.convert(j12, TimeUnit.NANOSECONDS);
                                    long nanos2 = j12 - timeUnit.toNanos(jConvert);
                                    boolean z10 = jConvert == 0 || nanos2 > 1000;
                                    if (jConvert > 0) {
                                        StringBuilder sb2 = new StringBuilder(String.valueOf(jConvert).length() + strConcat.length() + 1 + String.valueOf(lowerCase).length());
                                        defpackage.e.B(sb2, strConcat, jConvert, " ");
                                        sb2.append(lowerCase);
                                        String string4 = sb2.toString();
                                        if (z10) {
                                            string4 = string4.concat(",");
                                        }
                                        strConcat = string4.concat(" ");
                                    }
                                    if (z10) {
                                        StringBuilder sb3 = new StringBuilder(String.valueOf(nanos2).length() + strConcat.length() + 13);
                                        sb3.append(strConcat);
                                        sb3.append(nanos2);
                                        sb3.append(" nanoseconds ");
                                        strConcat = sb3.toString();
                                    }
                                    string3 = strConcat.concat("delay)");
                                }
                                if (isDone()) {
                                    throw new TimeoutException(string3.concat(" but future completed as timeout expired"));
                                }
                                throw new TimeoutException(defpackage.e.p(new StringBuilder(string3.length() + 5 + String.valueOf(string).length()), string3, " for ", string));
                            }
                        } else {
                            fu1Var = this.f11889c;
                        }
                    } while (fu1Var != fu1Var2);
                }
                Object obj4 = this.f11887a;
                Objects.requireNonNull(obj4);
                return j(obj4);
            }
            j11 = 0;
            if (nanos > j11) {
            }
        } else {
            throw new InterruptedException();
        }
    }
}
