package com.bytedance.adsdk.ro;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class onz<K, V> {
    private int ajl;
    private final LinkedHashMap<K, V> fm;
    private int jnr;

    /* JADX INFO: renamed from: lb, reason: collision with root package name */
    private int f6284lb;
    private int ro;
    private int wsy;
    private int wu;
    private int yz;

    public onz(int i10) {
        if (i10 <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        this.f6284lb = i10;
        this.fm = new LinkedHashMap<>(0, 0.75f, true);
    }

    private int lb(K k6, V v10) {
        int iRo = ro(k6, v10);
        if (iRo >= 0) {
            return iRo;
        }
        throw new IllegalStateException("Negative size: " + k6 + "=" + v10);
    }

    public final V fm(K k6) {
        V vPut;
        if (k6 == null) {
            throw new NullPointerException("key == null");
        }
        synchronized (this) {
            try {
                V v10 = this.fm.get(k6);
                if (v10 != null) {
                    this.wsy++;
                    return v10;
                }
                this.wu++;
                V vRo = ro(k6);
                if (vRo == null) {
                    return null;
                }
                synchronized (this) {
                    try {
                        this.jnr++;
                        vPut = this.fm.put(k6, vRo);
                        if (vPut != null) {
                            this.fm.put(k6, vPut);
                        } else {
                            this.ro += lb(k6, vRo);
                        }
                    } catch (Throwable th) {
                        throw th;
                    }
                }
                if (vPut != null) {
                    return vPut;
                }
                fm(this.f6284lb);
                return vRo;
            } finally {
            }
        }
    }

    public int ro(K k6, V v10) {
        return 1;
    }

    public final synchronized String toString() {
        int i10;
        int i11;
        try {
            i10 = this.wsy;
            i11 = this.wu + i10;
        } catch (Throwable th) {
            throw th;
        }
        return String.format(Locale.US, "LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]", Integer.valueOf(this.f6284lb), Integer.valueOf(this.wsy), Integer.valueOf(this.wu), Integer.valueOf(i11 != 0 ? (i10 * 100) / i11 : 0));
    }

    public V ro(K k6) {
        return null;
    }

    public final V fm(K k6, V v10) {
        V vPut;
        if (k6 != null && v10 != null) {
            synchronized (this) {
                try {
                    this.yz++;
                    this.ro += lb(k6, v10);
                    vPut = this.fm.put(k6, v10);
                    if (vPut != null) {
                        this.ro -= lb(k6, vPut);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
            fm(this.f6284lb);
            return vPut;
        }
        throw new NullPointerException("key == null || value == null");
    }

    /* JADX WARN: Code restructure failed: missing block: B:23:0x006f, code lost:
    
        throw new java.lang.IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!");
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void fm(int i10) {
        while (true) {
            synchronized (this) {
                try {
                    if (this.ro < 0 || (this.fm.isEmpty() && this.ro != 0)) {
                        break;
                    }
                    if (this.ro <= i10 || this.fm.isEmpty()) {
                        break;
                    }
                    Map.Entry<K, V> next = this.fm.entrySet().iterator().next();
                    K key = next.getKey();
                    V value = next.getValue();
                    this.fm.remove(key);
                    this.ro -= lb(key, value);
                    this.ajl++;
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }
}
