package n;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import o.AbstractC2628a;
import o.C2629b;
import x4.AbstractC3251q;

/* JADX INFO: renamed from: n.v, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public class C2594v {
    private int createCount;
    private int evictionCount;
    private int hitCount;
    private final C2629b lock;
    private final o.c map;
    private int maxSize;
    private int missCount;
    private int putCount;
    private int size;

    public C2594v(int i6) {
        this.maxSize = i6;
        if (i6 <= 0) {
            AbstractC2628a.c("maxSize <= 0");
            throw null;
        }
        this.map = new o.c();
        this.lock = new C2629b();
    }

    public final int a(Object obj, Object obj2) {
        int iSizeOf = sizeOf(obj, obj2);
        if (iSizeOf >= 0) {
            return iSizeOf;
        }
        String message = "Negative size: " + obj + '=' + obj2;
        kotlin.jvm.internal.o.h(message, "message");
        throw new IllegalStateException(message);
    }

    public Object create(Object key) {
        kotlin.jvm.internal.o.h(key, "key");
        return null;
    }

    public final int createCount() {
        int i6;
        synchronized (this.lock) {
            i6 = this.createCount;
        }
        return i6;
    }

    public void entryRemoved(boolean z6, Object key, Object oldValue, Object obj) {
        kotlin.jvm.internal.o.h(key, "key");
        kotlin.jvm.internal.o.h(oldValue, "oldValue");
    }

    public final void evictAll() {
        trimToSize(-1);
    }

    public final int evictionCount() {
        int i6;
        synchronized (this.lock) {
            i6 = this.evictionCount;
        }
        return i6;
    }

    public final Object get(Object key) {
        Object objPut;
        kotlin.jvm.internal.o.h(key, "key");
        synchronized (this.lock) {
            o.c cVar = this.map;
            cVar.getClass();
            Object obj = cVar.f21783a.get(key);
            if (obj != null) {
                this.hitCount++;
                return obj;
            }
            this.missCount++;
            Object objCreate = create(key);
            if (objCreate == null) {
                return null;
            }
            synchronized (this.lock) {
                try {
                    this.createCount++;
                    objPut = this.map.f21783a.put(key, objCreate);
                    if (objPut != null) {
                        this.map.f21783a.put(key, objPut);
                    } else {
                        this.size += a(key, objCreate);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            if (objPut != null) {
                entryRemoved(false, key, objCreate, objPut);
                return objPut;
            }
            trimToSize(this.maxSize);
            return objCreate;
        }
    }

    public final int hitCount() {
        int i6;
        synchronized (this.lock) {
            i6 = this.hitCount;
        }
        return i6;
    }

    public final int maxSize() {
        int i6;
        synchronized (this.lock) {
            i6 = this.maxSize;
        }
        return i6;
    }

    public final int missCount() {
        int i6;
        synchronized (this.lock) {
            i6 = this.missCount;
        }
        return i6;
    }

    public final Object put(Object key, Object value) {
        Object objPut;
        kotlin.jvm.internal.o.h(key, "key");
        kotlin.jvm.internal.o.h(value, "value");
        synchronized (this.lock) {
            this.putCount++;
            this.size += a(key, value);
            objPut = this.map.f21783a.put(key, value);
            if (objPut != null) {
                this.size -= a(key, objPut);
            }
        }
        if (objPut != null) {
            entryRemoved(false, key, objPut, value);
        }
        trimToSize(this.maxSize);
        return objPut;
    }

    public final int putCount() {
        int i6;
        synchronized (this.lock) {
            i6 = this.putCount;
        }
        return i6;
    }

    public final Object remove(Object key) {
        Object objRemove;
        kotlin.jvm.internal.o.h(key, "key");
        synchronized (this.lock) {
            o.c cVar = this.map;
            cVar.getClass();
            objRemove = cVar.f21783a.remove(key);
            if (objRemove != null) {
                this.size -= a(key, objRemove);
            }
        }
        if (objRemove != null) {
            entryRemoved(false, key, objRemove, null);
        }
        return objRemove;
    }

    public void resize(int i6) {
        if (!(i6 > 0)) {
            AbstractC2628a.c("maxSize <= 0");
            throw null;
        }
        synchronized (this.lock) {
            this.maxSize = i6;
        }
        trimToSize(i6);
    }

    public final int size() {
        int i6;
        synchronized (this.lock) {
            i6 = this.size;
        }
        return i6;
    }

    public int sizeOf(Object key, Object value) {
        kotlin.jvm.internal.o.h(key, "key");
        kotlin.jvm.internal.o.h(value, "value");
        return 1;
    }

    public final Map<Object, Object> snapshot() {
        LinkedHashMap linkedHashMap;
        synchronized (this.lock) {
            Set setEntrySet = this.map.f21783a.entrySet();
            kotlin.jvm.internal.o.g(setEntrySet, "<get-entries>(...)");
            linkedHashMap = new LinkedHashMap(setEntrySet.size());
            Set<Map.Entry> setEntrySet2 = this.map.f21783a.entrySet();
            kotlin.jvm.internal.o.g(setEntrySet2, "<get-entries>(...)");
            for (Map.Entry entry : setEntrySet2) {
                linkedHashMap.put(entry.getKey(), entry.getValue());
            }
        }
        return linkedHashMap;
    }

    public String toString() {
        String str;
        synchronized (this.lock) {
            try {
                int i6 = this.hitCount;
                int i7 = this.missCount + i6;
                str = "LruCache[maxSize=" + this.maxSize + ",hits=" + this.hitCount + ",misses=" + this.missCount + ",hitRate=" + (i7 != 0 ? (i6 * 100) / i7 : 0) + "%]";
            } catch (Throwable th) {
                throw th;
            }
        }
        return str;
    }

    /* JADX WARN: Code restructure failed: missing block: B:29:0x0072, code lost:
    
        return;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void trimToSize(int i6) {
        Object key;
        Object value;
        while (true) {
            synchronized (this.lock) {
                try {
                    if (!(this.size >= 0 && (!this.map.f21783a.isEmpty() || this.size == 0))) {
                        throw new IllegalStateException("LruCache.sizeOf() is reporting inconsistent results!");
                    }
                    if (this.size <= i6 || this.map.f21783a.isEmpty()) {
                        break;
                    }
                    Set setEntrySet = this.map.f21783a.entrySet();
                    kotlin.jvm.internal.o.g(setEntrySet, "<get-entries>(...)");
                    Map.Entry entry = (Map.Entry) AbstractC3251q.f0(setEntrySet);
                    if (entry == null) {
                        return;
                    }
                    key = entry.getKey();
                    value = entry.getValue();
                    o.c cVar = this.map;
                    cVar.getClass();
                    kotlin.jvm.internal.o.h(key, "key");
                    cVar.f21783a.remove(key);
                    this.size -= a(key, value);
                    this.evictionCount++;
                } catch (Throwable th) {
                    throw th;
                }
            }
            entryRemoved(true, key, value, null);
        }
    }
}
