package com.bytedance.adsdk.ro;

import android.os.Handler;
import android.os.Looper;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/* JADX INFO: loaded from: classes.dex */
public class dsz<T> {
    public static Executor fm = Executors.newCachedThreadPool();
    private volatile duv<T> jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private final Set<ku<Throwable>> f6194lb;
    private final Set<ku<T>> ro;
    private final Handler yz;

    public class fm extends FutureTask<duv<T>> {
        public fm(Callable<duv<T>> callable) {
            super(callable);
        }

        @Override // java.util.concurrent.FutureTask
        public void done() {
            if (isCancelled()) {
                return;
            }
            try {
                dsz.this.fm((duv) get());
            } catch (InterruptedException | ExecutionException e10) {
                dsz.this.fm(new duv(e10));
            }
        }
    }

    public dsz(Callable<duv<T>> callable) {
        this(callable, false);
    }

    public synchronized dsz<T> lb(ku<Throwable> kuVar) {
        try {
            duv<T> duvVar = this.jnr;
            if (duvVar != null && duvVar.ro() != null) {
                kuVar.fm(duvVar.ro());
            }
            this.f6194lb.add(kuVar);
        } catch (Throwable th) {
            throw th;
        }
        return this;
    }

    public synchronized dsz<T> ro(ku<T> kuVar) {
        this.ro.remove(kuVar);
        return this;
    }

    public synchronized dsz<T> yz(ku<Throwable> kuVar) {
        this.f6194lb.remove(kuVar);
        return this;
    }

    public dsz(Callable<duv<T>> callable, boolean z10) {
        this.ro = new LinkedHashSet(1);
        this.f6194lb = new LinkedHashSet(1);
        this.yz = new Handler(Looper.getMainLooper());
        this.jnr = null;
        if (!z10) {
            fm.execute(new fm(callable));
            return;
        }
        try {
            fm((duv) callable.call());
        } catch (Throwable th) {
            fm((duv) new duv<>(th));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void fm(duv<T> duvVar) {
        if (this.jnr == null) {
            this.jnr = duvVar;
            fm();
            return;
        }
        throw new IllegalStateException("A task may only be set once.");
    }

    public synchronized dsz<T> fm(ku<T> kuVar) {
        try {
            duv<T> duvVar = this.jnr;
            if (duvVar != null && duvVar.fm() != null) {
                kuVar.fm(duvVar.fm());
            }
            this.ro.add(kuVar);
        } catch (Throwable th) {
            throw th;
        }
        return this;
    }

    private void fm() {
        this.yz.post(new Runnable() { // from class: com.bytedance.adsdk.ro.dsz.1
            @Override // java.lang.Runnable
            public void run() {
                duv duvVar = dsz.this.jnr;
                if (duvVar == null) {
                    return;
                }
                if (duvVar.fm() != null) {
                    dsz.this.fm(duvVar.fm());
                } else {
                    dsz.this.fm(duvVar.ro());
                }
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void fm(T t10) {
        Iterator it = new ArrayList(this.ro).iterator();
        while (it.hasNext()) {
            ((ku) it.next()).fm(t10);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void fm(Throwable th) {
        ArrayList arrayList = new ArrayList(this.f6194lb);
        if (arrayList.isEmpty()) {
            return;
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            ((ku) it.next()).fm(th);
        }
    }
}
