package u2;

import java.util.HashMap;
import java.util.LinkedHashSet;
import kotlin.jvm.internal.v0;
import rm.n0;
import sm.c0;
import y2.s;
import y2.t;

/* loaded from: classes.dex */
public class b {

    /* renamed from: a, reason: collision with root package name */
    private final t f36779a = s.a();

    /* renamed from: b, reason: collision with root package name */
    private final HashMap f36780b;

    /* renamed from: c, reason: collision with root package name */
    private final LinkedHashSet f36781c;

    /* renamed from: d, reason: collision with root package name */
    private int f36782d;

    /* renamed from: e, reason: collision with root package name */
    private int f36783e;

    /* renamed from: f, reason: collision with root package name */
    private int f36784f;

    /* renamed from: g, reason: collision with root package name */
    private int f36785g;

    /* renamed from: h, reason: collision with root package name */
    private int f36786h;

    /* renamed from: i, reason: collision with root package name */
    private int f36787i;

    /* renamed from: j, reason: collision with root package name */
    private int f36788j;

    public b(int i10) {
        if (!(i10 > 0)) {
            throw new IllegalArgumentException("maxSize <= 0".toString());
        }
        this.f36783e = i10;
        this.f36780b = new HashMap(0, 0.75f);
        this.f36781c = new LinkedHashSet();
    }

    private final int g(Object obj, Object obj2) {
        int i10 = i(obj, obj2);
        if (i10 >= 0) {
            return i10;
        }
        throw new IllegalStateException(("Negative size: " + obj + '=' + obj2).toString());
    }

    protected Object b(Object obj) {
        return null;
    }

    protected void c(boolean z10, Object obj, Object obj2, Object obj3) {
    }

    public final Object d(Object obj) {
        synchronized (this.f36779a) {
            Object obj2 = this.f36780b.get(obj);
            if (obj2 != null) {
                this.f36781c.remove(obj);
                this.f36781c.add(obj);
                this.f36787i++;
                return obj2;
            }
            this.f36788j++;
            Object b10 = b(obj);
            if (b10 == null) {
                return null;
            }
            synchronized (this.f36779a) {
                this.f36785g++;
                Object put = this.f36780b.put(obj, b10);
                this.f36781c.remove(obj);
                this.f36781c.add(obj);
                if (put != null) {
                    this.f36780b.put(obj, put);
                    obj2 = put;
                } else {
                    this.f36782d = h() + g(obj, b10);
                }
                n0 n0Var = n0.f34904a;
            }
            if (obj2 != null) {
                c(false, obj, b10, obj2);
                return obj2;
            }
            j(this.f36783e);
            return b10;
        }
    }

    public final Object e(Object obj, Object obj2) {
        Object put;
        if (obj == null || obj2 == null) {
            throw null;
        }
        synchronized (this.f36779a) {
            this.f36784f++;
            this.f36782d = h() + g(obj, obj2);
            put = this.f36780b.put(obj, obj2);
            if (put != null) {
                this.f36782d = h() - g(obj, put);
            }
            if (this.f36781c.contains(obj)) {
                this.f36781c.remove(obj);
            }
            this.f36781c.add(obj);
        }
        if (put != null) {
            c(false, obj, put, obj2);
        }
        j(this.f36783e);
        return put;
    }

    public final Object f(Object obj) {
        Object remove;
        obj.getClass();
        synchronized (this.f36779a) {
            remove = this.f36780b.remove(obj);
            this.f36781c.remove(obj);
            if (remove != null) {
                this.f36782d = h() - g(obj, remove);
            }
            n0 n0Var = n0.f34904a;
        }
        if (remove != null) {
            c(false, obj, remove, null);
        }
        return remove;
    }

    public final int h() {
        int i10;
        synchronized (this.f36779a) {
            i10 = this.f36782d;
        }
        return i10;
    }

    protected int i(Object obj, Object obj2) {
        return 1;
    }

    /* JADX WARN: Code restructure failed: missing block: B:12:0x0090, code lost:
    
        throw new java.lang.IllegalStateException("map/keySet size inconsistency");
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void j(int i10) {
        Object obj;
        Object obj2;
        while (true) {
            synchronized (this.f36779a) {
                if (h() < 0 || ((this.f36780b.isEmpty() && h() != 0) || this.f36780b.isEmpty() != this.f36781c.isEmpty())) {
                    break;
                }
                if (h() <= i10 || this.f36780b.isEmpty()) {
                    obj = null;
                    obj2 = null;
                } else {
                    obj = c0.b0(this.f36781c);
                    obj2 = this.f36780b.get(obj);
                    if (obj2 == null) {
                        throw new IllegalStateException("inconsistent state");
                    }
                    v0.c(this.f36780b).remove(obj);
                    v0.a(this.f36781c).remove(obj);
                    int h10 = h();
                    kotlin.jvm.internal.t.d(obj);
                    kotlin.jvm.internal.t.d(obj2);
                    this.f36782d = h10 - g(obj, obj2);
                    this.f36786h++;
                }
                n0 n0Var = n0.f34904a;
            }
            if (obj == null && obj2 == null) {
                return;
            }
            kotlin.jvm.internal.t.d(obj);
            kotlin.jvm.internal.t.d(obj2);
            c(true, obj, obj2, null);
        }
    }

    public String toString() {
        String str;
        synchronized (this.f36779a) {
            int i10 = this.f36787i;
            int i11 = this.f36788j + i10;
            str = "LruCache[maxSize=" + this.f36783e + ",hits=" + this.f36787i + ",misses=" + this.f36788j + ",hitRate=" + (i11 != 0 ? (i10 * 100) / i11 : 0) + "%]";
        }
        return str;
    }
}
