what-the-bank/sources/com/google/common/cache/AbstractCache.java

155 lines
5.3 KiB
Java

package com.google.common.cache;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
/* loaded from: classes2.dex */
public abstract class AbstractCache<K, V> implements Cache<K, V> {
/* loaded from: classes2.dex */
public interface StatsCounter {
void recordEviction();
void recordHits(int i);
void recordLoadException(long j);
void recordLoadSuccess(long j);
void recordMisses(int i);
CacheStats snapshot();
}
@Override // com.google.common.cache.Cache
public void cleanUp() {
}
@Override // com.google.common.cache.Cache
public V get(K k, Callable<? extends V> callable) throws ExecutionException {
throw new UnsupportedOperationException();
}
@Override // com.google.common.cache.Cache
public ImmutableMap<K, V> getAllPresent(Iterable<?> iterable) {
V ifPresent;
LinkedHashMap newLinkedHashMap = Maps.newLinkedHashMap();
for (Object obj : iterable) {
if (!newLinkedHashMap.containsKey(obj) && (ifPresent = getIfPresent(obj)) != null) {
newLinkedHashMap.put(obj, ifPresent);
}
}
return ImmutableMap.copyOf((Map) newLinkedHashMap);
}
@Override // com.google.common.cache.Cache
public void put(K k, V v) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.cache.Cache
public void putAll(Map<? extends K, ? extends V> map) {
for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override // com.google.common.cache.Cache
public long size() {
throw new UnsupportedOperationException();
}
@Override // com.google.common.cache.Cache
public void invalidate(Object obj) {
throw new UnsupportedOperationException();
}
@Override // com.google.common.cache.Cache
public void invalidateAll(Iterable<?> iterable) {
Iterator<?> it = iterable.iterator();
while (it.hasNext()) {
invalidate(it.next());
}
}
@Override // com.google.common.cache.Cache
public void invalidateAll() {
throw new UnsupportedOperationException();
}
@Override // com.google.common.cache.Cache
public CacheStats stats() {
throw new UnsupportedOperationException();
}
@Override // com.google.common.cache.Cache
public ConcurrentMap<K, V> asMap() {
throw new UnsupportedOperationException();
}
/* loaded from: classes2.dex */
public static final class SimpleStatsCounter implements StatsCounter {
private final LongAddable hitCount = LongAddables.create();
private final LongAddable missCount = LongAddables.create();
private final LongAddable loadSuccessCount = LongAddables.create();
private final LongAddable loadExceptionCount = LongAddables.create();
private final LongAddable totalLoadTime = LongAddables.create();
private final LongAddable evictionCount = LongAddables.create();
private static long negativeToMaxValue(long j) {
if (j < 0) {
return Long.MAX_VALUE;
}
return j;
}
@Override // com.google.common.cache.AbstractCache.StatsCounter
public final void recordHits(int i) {
this.hitCount.add(i);
}
@Override // com.google.common.cache.AbstractCache.StatsCounter
public final void recordMisses(int i) {
this.missCount.add(i);
}
@Override // com.google.common.cache.AbstractCache.StatsCounter
public final void recordLoadSuccess(long j) {
this.loadSuccessCount.increment();
this.totalLoadTime.add(j);
}
@Override // com.google.common.cache.AbstractCache.StatsCounter
public final void recordLoadException(long j) {
this.loadExceptionCount.increment();
this.totalLoadTime.add(j);
}
@Override // com.google.common.cache.AbstractCache.StatsCounter
public final void recordEviction() {
this.evictionCount.increment();
}
@Override // com.google.common.cache.AbstractCache.StatsCounter
public final CacheStats snapshot() {
return new CacheStats(negativeToMaxValue(this.hitCount.sum()), negativeToMaxValue(this.missCount.sum()), negativeToMaxValue(this.loadSuccessCount.sum()), negativeToMaxValue(this.loadExceptionCount.sum()), negativeToMaxValue(this.totalLoadTime.sum()), negativeToMaxValue(this.evictionCount.sum()));
}
public final void incrementBy(StatsCounter statsCounter) {
CacheStats snapshot = statsCounter.snapshot();
this.hitCount.add(snapshot.hitCount());
this.missCount.add(snapshot.missCount());
this.loadSuccessCount.add(snapshot.loadSuccessCount());
this.loadExceptionCount.add(snapshot.loadExceptionCount());
this.totalLoadTime.add(snapshot.totalLoadTime());
this.evictionCount.add(snapshot.evictionCount());
}
}
}