package b.f.j.c; import android.os.SystemClock; import androidx.annotation.VisibleForTesting; import b.c.a.a0.d; import b.f.d.d.j; import b.f.d.h.f; import b.f.j.a.c.c; import b.f.j.c.m; import b.f.j.c.w; import com.facebook.common.internal.Supplier; import com.facebook.common.references.CloseableReference; import com.facebook.imagepipeline.cache.MemoryCacheParams; import java.util.ArrayList; import java.util.Iterator; import java.util.Objects; import java.util.WeakHashMap; /* compiled from: LruCountingMemoryCache */ public class v implements m, w { public final m.b a; @VisibleForTesting /* renamed from: b reason: collision with root package name */ public final l> f557b; @VisibleForTesting public final l> c; public final b0 d; public final Supplier e; public MemoryCacheParams f; public long g = SystemClock.uptimeMillis(); public final boolean h; public final boolean i; /* compiled from: LruCountingMemoryCache */ public class a implements f { public final /* synthetic */ m.a a; public a(m.a aVar) { this.a = aVar; } /* JADX WARNING: Removed duplicated region for block: B:25:0x003d */ @Override // b.f.d.h.f public void release(V v) { boolean z2; m.b bVar; v vVar = v.this; m.a aVar = this.a; Objects.requireNonNull(vVar); Objects.requireNonNull(aVar); synchronized (vVar) { synchronized (vVar) { z2 = false; d.B(aVar.c > 0); aVar.c--; } CloseableReference.s(vVar.o(aVar)); if (!z2) { aVar = null; } if (!(aVar == null || (bVar = aVar.e) == null)) { ((c.a) bVar).a(aVar.a, true); } vVar.m(); vVar.j(); } synchronized (vVar) { if (!aVar.d && aVar.c == 0) { vVar.f557b.d(aVar.a, aVar); z2 = true; } } CloseableReference.s(vVar.o(aVar)); if (!z2) { } ((c.a) bVar).a(aVar.a, true); vVar.m(); vVar.j(); } } public v(b0 b0Var, w.a aVar, Supplier supplier, m.b bVar, boolean z2, boolean z3) { new WeakHashMap(); this.d = b0Var; this.f557b = new l<>(new u(this, b0Var)); this.c = new l<>(new u(this, b0Var)); this.e = supplier; MemoryCacheParams memoryCacheParams = supplier.get(); d.y(memoryCacheParams, "mMemoryCacheParamsSupplier returned null"); this.f = memoryCacheParams; this.a = bVar; this.h = z2; this.i = z3; } public static void k(m.a aVar) { m.b bVar; if (aVar != null && (bVar = aVar.e) != null) { ((c.a) bVar).a(aVar.a, false); } } @Override // b.f.j.c.w public CloseableReference a(K k, CloseableReference closeableReference) { return c(k, closeableReference, this.a); } @Override // b.f.j.c.m public CloseableReference b(K k) { m.a e; boolean z2; CloseableReference closeableReference; Objects.requireNonNull(k); synchronized (this) { e = this.f557b.e(k); z2 = false; if (e != null) { m.a e2 = this.c.e(k); Objects.requireNonNull(e2); if (e2.c == 0) { z2 = true; } d.B(z2); closeableReference = e2.f554b; z2 = true; } else { closeableReference = null; } } if (z2) { k(e); } return closeableReference; } /* JADX WARNING: Removed duplicated region for block: B:27:0x006f */ @Override // b.f.j.c.m public CloseableReference c(K k, CloseableReference closeableReference, m.b bVar) { CloseableReference closeableReference2; CloseableReference closeableReference3; int a2; boolean z2; Objects.requireNonNull(k); Objects.requireNonNull(closeableReference); m(); synchronized (this) { m.a e = this.f557b.e(k); m.a e2 = this.c.e(k); closeableReference2 = null; if (e2 != null) { g(e2); closeableReference3 = o(e2); } else { closeableReference3 = null; } a2 = this.d.a(closeableReference.u()); synchronized (this) { z2 = true; if (a2 > this.f.e || e() > this.f.f2866b - 1 || f() > this.f.a - a2) { z2 = false; } } if (closeableReference3 != null) { closeableReference3.close(); } k(e); j(); return closeableReference2; } if (z2) { m.a aVar = this.h ? new m.a<>(k, closeableReference, bVar, a2) : new m.a<>(k, closeableReference, bVar, -1); this.c.d(k, aVar); closeableReference2 = n(aVar); } if (closeableReference3 != null) { } k(e); j(); return closeableReference2; } @Override // b.f.j.c.w public synchronized boolean contains(K k) { boolean containsKey; l> lVar = this.c; synchronized (lVar) { containsKey = lVar.f553b.containsKey(k); } return containsKey; } @Override // b.f.j.c.w public int d(j jVar) { ArrayList> f; ArrayList> f2; synchronized (this) { f = this.f557b.f(jVar); f2 = this.c.f(jVar); h(f2); } i(f2); l(f); m(); j(); return f2.size(); } public synchronized int e() { return this.c.a() - this.f557b.a(); } public synchronized int f() { return this.c.b() - this.f557b.b(); } public final synchronized void g(m.a aVar) { Objects.requireNonNull(aVar); d.B(!aVar.d); aVar.d = true; } @Override // b.f.j.c.w public CloseableReference get(K k) { m.a e; m.a aVar; Objects.requireNonNull(k); CloseableReference closeableReference = null; synchronized (this) { e = this.f557b.e(k); l> lVar = this.c; synchronized (lVar) { aVar = lVar.f553b.get(k); } m.a aVar2 = aVar; if (aVar2 != null) { closeableReference = n(aVar2); } } k(e); m(); j(); return closeableReference; } public final synchronized void h(ArrayList> arrayList) { if (arrayList != null) { Iterator> it = arrayList.iterator(); while (it.hasNext()) { g(it.next()); } } } public final void i(ArrayList> arrayList) { if (arrayList != null) { Iterator> it = arrayList.iterator(); while (it.hasNext()) { CloseableReference.s(o(it.next())); } } } public void j() { ArrayList> p; synchronized (this) { MemoryCacheParams memoryCacheParams = this.f; int min = Math.min(memoryCacheParams.d, memoryCacheParams.f2866b - e()); MemoryCacheParams memoryCacheParams2 = this.f; p = p(min, Math.min(memoryCacheParams2.c, memoryCacheParams2.a - f())); h(p); } i(p); l(p); } public final void l(ArrayList> arrayList) { if (arrayList != null) { Iterator> it = arrayList.iterator(); while (it.hasNext()) { k(it.next()); } } } public final synchronized void m() { if (this.g + this.f.f <= SystemClock.uptimeMillis()) { this.g = SystemClock.uptimeMillis(); MemoryCacheParams memoryCacheParams = this.e.get(); d.y(memoryCacheParams, "mMemoryCacheParamsSupplier returned null"); this.f = memoryCacheParams; } } public final synchronized CloseableReference n(m.a aVar) { synchronized (this) { d.B(!aVar.d); aVar.c++; } return CloseableReference.D(aVar.f554b.u(), new a(aVar)); return CloseableReference.D(aVar.f554b.u(), new a(aVar)); } public final synchronized CloseableReference o(m.a aVar) { Objects.requireNonNull(aVar); return (!aVar.d || aVar.c != 0) ? null : aVar.f554b; } public final synchronized ArrayList> p(int i, int i2) { K next; int max = Math.max(i, 0); int max2 = Math.max(i2, 0); if (this.f557b.a() <= max && this.f557b.b() <= max2) { return null; } ArrayList> arrayList = new ArrayList<>(); while (true) { if (this.f557b.a() <= max && this.f557b.b() <= max2) { break; } l> lVar = this.f557b; synchronized (lVar) { next = lVar.f553b.isEmpty() ? null : lVar.f553b.keySet().iterator().next(); } if (next != null) { this.f557b.e(next); arrayList.add(this.c.e(next)); } else if (this.i) { l> lVar2 = this.f557b; synchronized (lVar2) { if (lVar2.f553b.isEmpty()) { lVar2.c = 0; } } } else { throw new IllegalStateException(String.format("key is null, but exclusiveEntries count: %d, size: %d", Integer.valueOf(this.f557b.a()), Integer.valueOf(this.f557b.b()))); } } return arrayList; } }