package com.bytedance.sdk.component.jnr.ro.lb.fm;

import java.lang.ref.SoftReference;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/* JADX INFO: loaded from: classes.dex */
public class lb<K, V> {
    private final LinkedHashMap<K, SoftReference<V>> fm = new LinkedHashMap<>(0, 0.75f, true);
    private int ro;

    public lb(int i10) {
        this.ro = i10;
    }

    public synchronized V fm(K k6) {
        V v10 = null;
        if (k6 == null) {
            return null;
        }
        if (this.ro <= 0) {
            return null;
        }
        SoftReference<V> softReference = this.fm.get(k6);
        if (softReference != null) {
            v10 = softReference.get();
            if (v10 != null) {
                return v10;
            }
            this.fm.remove(k6);
        }
        return v10;
    }

    public final synchronized String toString() {
        return String.format("LruCache[maxCount=%d,size=%d]", Integer.valueOf(this.ro), Integer.valueOf(this.fm.size()));
    }

    public synchronized void fm(K k6, V v10) {
        if (this.ro <= 0) {
            return;
        }
        if (k6 == null || v10 == null) {
            return;
        }
        this.fm.put(k6, new SoftReference<>(v10));
        int size = this.fm.size();
        int i10 = this.ro;
        if (size > i10) {
            fm((int) (((double) i10) * 0.7d));
        }
    }

    public synchronized void fm(int i10) {
        int size = this.fm.size() - i10;
        if (size > 0) {
            Iterator<Map.Entry<K, SoftReference<V>>> it = this.fm.entrySet().iterator();
            for (int i11 = 0; i11 < size; i11++) {
                it.next();
                it.remove();
            }
        }
        if (i10 == 0) {
            return;
        }
        Iterator<Map.Entry<K, SoftReference<V>>> it2 = this.fm.entrySet().iterator();
        while (it2.hasNext()) {
            if (it2.next().getValue().get() == null) {
                it2.remove();
            }
        }
    }
}
