package i.b.a.l;

import android.os.Process;
import android.util.SparseArray;
import i.b.a.l.a;
import java.lang.ref.WeakReference;

/* JADX INFO: compiled from: AbstractQueryData.java */
/* JADX INFO: loaded from: classes.dex */
abstract class b<T, Q extends a<T>> {
    final String a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    final i.b.a.a<T, ?> f4296b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    final String[] f4297c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    final SparseArray<WeakReference<Q>> f4298d = new SparseArray<>();

    b(i.b.a.a<T, ?> aVar, String str, String[] strArr) {
        this.f4296b = aVar;
        this.a = str;
        this.f4297c = strArr;
    }

    protected abstract Q a();

    Q b() {
        Q q;
        int iMyTid = Process.myTid();
        if (iMyTid == 0) {
            long id = Thread.currentThread().getId();
            if (id < 0 || id > 2147483647L) {
                throw new RuntimeException("Cannot handle thread ID: " + id);
            }
            iMyTid = (int) id;
        }
        synchronized (this.f4298d) {
            WeakReference<Q> weakReference = this.f4298d.get(iMyTid);
            q = weakReference != null ? weakReference.get() : null;
            if (q == null) {
                d();
                q = (Q) a();
                this.f4298d.put(iMyTid, new WeakReference<>(q));
            } else {
                System.arraycopy(this.f4297c, 0, q.f4294d, 0, this.f4297c.length);
            }
        }
        return q;
    }

    Q c(Q q) {
        if (Thread.currentThread() != q.f4295e) {
            return (Q) b();
        }
        String[] strArr = this.f4297c;
        System.arraycopy(strArr, 0, q.f4294d, 0, strArr.length);
        return q;
    }

    void d() {
        synchronized (this.f4298d) {
            for (int size = this.f4298d.size() - 1; size >= 0; size--) {
                if (this.f4298d.valueAt(size).get() == null) {
                    this.f4298d.remove(this.f4298d.keyAt(size));
                }
            }
        }
    }
}
