aboutsummaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/cache/AbstractCache.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava/src/com/google/common/cache/AbstractCache.java')
-rw-r--r--guava/src/com/google/common/cache/AbstractCache.java110
1 files changed, 52 insertions, 58 deletions
diff --git a/guava/src/com/google/common/cache/AbstractCache.java b/guava/src/com/google/common/cache/AbstractCache.java
index a8af810..8ce941d 100644
--- a/guava/src/com/google/common/cache/AbstractCache.java
+++ b/guava/src/com/google/common/cache/AbstractCache.java
@@ -20,21 +20,22 @@ import com.google.common.annotations.Beta;
import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
+import com.google.common.util.concurrent.UncheckedExecutionException;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicLong;
/**
* This class provides a skeletal implementation of the {@code Cache} interface to minimize the
* effort required to implement this interface.
*
* <p>To implement a cache, the programmer needs only to extend this class and provide an
- * implementation for the {@link #put} and {@link #getIfPresent} methods. {@link #getAllPresent} is
- * implemented in terms of {@link #getIfPresent}; {@link #putAll} is implemented in terms of
- * {@link #put}, {@link #invalidateAll(Iterable)} is implemented in terms of {@link #invalidate}.
- * The method {@link #cleanUp} is a no-op. All other methods throw an
+ * implementation for the {@link #getIfPresent} method. {@link #getAllPresent} is implemented in
+ * terms of {@code getIfPresent}; {@link #invalidateAll(Iterable)} is implemented in terms of
+ * {@link #invalidate}. The method {@link #cleanUp} is a no-op. All other methods throw an
* {@link UnsupportedOperationException}.
*
* @author Charles Fry
@@ -56,22 +57,14 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
}
/**
- * This implementation of {@code getAllPresent} lacks any insight into the internal cache data
- * structure, and is thus forced to return the query keys instead of the cached keys. This is only
- * possible with an unsafe cast which requires {@code keys} to actually be of type {@code K}.
- *
- * {@inheritDoc}
- *
* @since 11.0
*/
@Override
- public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
+ public ImmutableMap<K, V> getAllPresent(Iterable<? extends K> keys) {
Map<K, V> result = Maps.newLinkedHashMap();
- for (Object key : keys) {
+ for (K key : keys) {
if (!result.containsKey(key)) {
- @SuppressWarnings("unchecked")
- K castKey = (K) key;
- result.put(castKey, getIfPresent(key));
+ result.put(key, getIfPresent(key));
}
}
return ImmutableMap.copyOf(result);
@@ -85,16 +78,6 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
throw new UnsupportedOperationException();
}
- /**
- * @since 12.0
- */
- @Override
- public void putAll(Map<? extends K, ? extends V> m) {
- for (Map.Entry<? extends K, ? extends V> entry : m.entrySet()) {
- put(entry.getKey(), entry.getValue());
- }
- }
-
@Override
public void cleanUp() {}
@@ -133,6 +116,22 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
throw new UnsupportedOperationException();
}
+ @Deprecated
+ @Override
+ public V getUnchecked(K key) {
+ try {
+ return get(key);
+ } catch (ExecutionException e) {
+ throw new UncheckedExecutionException(e.getCause());
+ }
+ }
+
+ @Deprecated
+ @Override
+ public V apply(K key) {
+ return getUnchecked(key);
+ }
+
/**
* Accumulates statistics during the operation of a {@link Cache} for presentation by {@link
* Cache#stats}. This is solely intended for consumption by {@code Cache} implementors.
@@ -165,7 +164,7 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
/**
* Records the successful load of a new entry. This should be called when a cache request
* causes an entry to be loaded, and the loading completes successfully. In contrast to
- * {@link #recordMisses}, this method should only be called by the loading thread.
+ * {@link #recordConcurrentMiss}, this method should only be called by the loading thread.
*
* @param loadTime the number of nanoseconds the cache spent computing or retrieving the new
* value
@@ -175,7 +174,7 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
/**
* Records the failed load of a new entry. This should be called when a cache request causes
* an entry to be loaded, but an exception is thrown while loading the entry. In contrast to
- * {@link #recordMisses}, this method should only be called by the loading thread.
+ * {@link #recordConcurrentMiss}, this method should only be called by the loading thread.
*
* @param loadTime the number of nanoseconds the cache spent computing or retrieving the new
* value prior to an exception being thrown
@@ -202,25 +201,20 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
* @since 10.0
*/
@Beta
- 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();
-
- /**
- * Constructs an instance with all counts initialized to zero.
- */
- public SimpleStatsCounter() {}
+ public static class SimpleStatsCounter implements StatsCounter {
+ private final AtomicLong hitCount = new AtomicLong();
+ private final AtomicLong missCount = new AtomicLong();
+ private final AtomicLong loadSuccessCount = new AtomicLong();
+ private final AtomicLong loadExceptionCount = new AtomicLong();
+ private final AtomicLong totalLoadTime = new AtomicLong();
+ private final AtomicLong evictionCount = new AtomicLong();
/**
* @since 11.0
*/
@Override
public void recordHits(int count) {
- hitCount.add(count);
+ hitCount.addAndGet(count);
}
/**
@@ -228,35 +222,35 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
*/
@Override
public void recordMisses(int count) {
- missCount.add(count);
+ missCount.addAndGet(count);
}
@Override
public void recordLoadSuccess(long loadTime) {
- loadSuccessCount.increment();
- totalLoadTime.add(loadTime);
+ loadSuccessCount.incrementAndGet();
+ totalLoadTime.addAndGet(loadTime);
}
@Override
public void recordLoadException(long loadTime) {
- loadExceptionCount.increment();
- totalLoadTime.add(loadTime);
+ loadExceptionCount.incrementAndGet();
+ totalLoadTime.addAndGet(loadTime);
}
@Override
public void recordEviction() {
- evictionCount.increment();
+ evictionCount.incrementAndGet();
}
@Override
public CacheStats snapshot() {
return new CacheStats(
- hitCount.sum(),
- missCount.sum(),
- loadSuccessCount.sum(),
- loadExceptionCount.sum(),
- totalLoadTime.sum(),
- evictionCount.sum());
+ hitCount.get(),
+ missCount.get(),
+ loadSuccessCount.get(),
+ loadExceptionCount.get(),
+ totalLoadTime.get(),
+ evictionCount.get());
}
/**
@@ -264,12 +258,12 @@ public abstract class AbstractCache<K, V> implements Cache<K, V> {
*/
public void incrementBy(StatsCounter other) {
CacheStats otherStats = other.snapshot();
- hitCount.add(otherStats.hitCount());
- missCount.add(otherStats.missCount());
- loadSuccessCount.add(otherStats.loadSuccessCount());
- loadExceptionCount.add(otherStats.loadExceptionCount());
- totalLoadTime.add(otherStats.totalLoadTime());
- evictionCount.add(otherStats.evictionCount());
+ hitCount.addAndGet(otherStats.hitCount());
+ missCount.addAndGet(otherStats.missCount());
+ loadSuccessCount.addAndGet(otherStats.loadSuccessCount());
+ loadExceptionCount.addAndGet(otherStats.loadExceptionCount());
+ totalLoadTime.addAndGet(otherStats.totalLoadTime());
+ evictionCount.addAndGet(otherStats.evictionCount());
}
}
}