From 2e6d32844e033399e63e42c88e0ea8b587879af3 Mon Sep 17 00:00:00 2001 From: Ben Manes Date: Wed, 30 May 2018 22:07:27 -0700 Subject: [PATCH] JSR-305 migrations (fixes #242) Migrate removed usages of @Nonnegative (prior commit) to @NonNegative. Add generic type @NonNull where not excessively verbose. --- .../caffeine/cache/AsyncCacheLoader.java | 4 +-- .../caffeine/cache/AsyncLoadingCache.java | 2 +- .../github/benmanes/caffeine/cache/Cache.java | 8 +++--- .../benmanes/caffeine/cache/CacheLoader.java | 5 ++-- .../benmanes/caffeine/cache/Caffeine.java | 13 +++++----- .../benmanes/caffeine/cache/CaffeineSpec.java | 3 ++- .../benmanes/caffeine/cache/Expiry.java | 7 +++-- .../caffeine/cache/FrequencySketch.java | 4 ++- .../benmanes/caffeine/cache/LoadingCache.java | 2 +- .../github/benmanes/caffeine/cache/Node.java | 7 +++-- .../benmanes/caffeine/cache/Policy.java | 26 +++++++++++-------- .../benmanes/caffeine/cache/TimerWheel.java | 9 ++++--- .../benmanes/caffeine/cache/Weigher.java | 2 ++ .../caffeine/cache/stats/CacheStats.java | 25 +++++++++++++++--- .../caffeine/cache/stats/StatsCounter.java | 11 ++++---- .../management/JCacheStatisticsMXBean.java | 12 +++++---- .../membership/bloom/BloomFilter.java | 7 +++-- 17 files changed, 95 insertions(+), 52 deletions(-) diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java index 4f494f44d8..3e32c95f80 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCacheLoader.java @@ -69,8 +69,8 @@ public interface AsyncCacheLoader { * that key; may not contain null values */ @NonNull - default CompletableFuture> asyncLoadAll( - @NonNull Iterable keys, @NonNull Executor executor) { + default CompletableFuture> asyncLoadAll( + @NonNull Iterable keys, @NonNull Executor executor) { throw new UnsupportedOperationException(); } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncLoadingCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncLoadingCache.java index eb92a70b9a..18fadfb4d3 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncLoadingCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncLoadingCache.java @@ -79,7 +79,7 @@ public interface AsyncLoadingCache extends AsyncCache { * future, in which case the mapping is left unestablished */ @NonNull - CompletableFuture> getAll(@NonNull Iterable keys); + CompletableFuture> getAll(@NonNull Iterable keys); /** * Returns a view of the entries stored in this cache as a synchronous {@link LoadingCache}. A diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Cache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Cache.java index ffd07922c2..5492831eab 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Cache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Cache.java @@ -19,6 +19,7 @@ import java.util.concurrent.ConcurrentMap; import java.util.function.Function; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -90,7 +91,7 @@ public interface Cache { * @throws NullPointerException if the specified collection is null or contains a null element */ @NonNull - Map getAllPresent(@NonNull Iterable keys); + Map<@NonNull K, @NonNull V> getAllPresent(@NonNull Iterable<@NonNull ?> keys); /** * Associates the {@code value} with the {@code key} in this cache. If the cache previously @@ -116,7 +117,7 @@ public interface Cache { * @throws NullPointerException if the specified map is null or the specified map contains null * keys or values */ - void putAll(@NonNull Map map); + void putAll(@NonNull Map map); /** * Discards any cached value for the {@code key}. The behavior of this operation is undefined for @@ -150,6 +151,7 @@ public interface Cache { * * @return the estimated number of mappings */ + @NonNegative long estimatedSize(); /** @@ -175,7 +177,7 @@ public interface Cache { * @return a thread-safe view of this cache supporting all of the optional {@link Map} operations */ @NonNull - ConcurrentMap asMap(); + ConcurrentMap<@NonNull K, @NonNull V> asMap(); /** * Performs any pending maintenance operations needed by the cache. Exactly which activities are diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java index 4194b12214..1bfae198b2 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CacheLoader.java @@ -83,7 +83,8 @@ public interface CacheLoader extends AsyncCacheLoader { * caught, the thread's interrupt status is set */ @NonNull - default Map loadAll(@NonNull Iterable keys) throws Exception { + default Map<@NonNull K, @NonNull V> loadAll( + @NonNull Iterable keys) throws Exception { throw new UnsupportedOperationException(); } @@ -128,7 +129,7 @@ default CompletableFuture asyncLoad(@NonNull K key, @NonNull Executor executo * that key; may not contain null values */ @Override @NonNull - default CompletableFuture> asyncLoadAll( + default CompletableFuture> asyncLoadAll( @NonNull Iterable keys, @NonNull Executor executor) { requireNonNull(keys); requireNonNull(executor); diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Caffeine.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Caffeine.java index 6e474d75c9..e83daa6a15 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Caffeine.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Caffeine.java @@ -34,6 +34,7 @@ import java.util.logging.Level; import java.util.logging.Logger; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -243,7 +244,7 @@ public static Caffeine from(String spec) { * @throws IllegalStateException if an initial capacity was already set */ @NonNull - public Caffeine initialCapacity(int initialCapacity) { + public Caffeine initialCapacity(@NonNegative int initialCapacity) { requireState(this.initialCapacity == UNSET_INT, "initial capacity was already set to %s", this.initialCapacity); requireArgument(initialCapacity >= 0); @@ -306,7 +307,7 @@ Executor getExecutor() { * @throws IllegalStateException if a maximum size or weight was already set */ @NonNull - public Caffeine maximumSize(long maximumSize) { + public Caffeine maximumSize(@NonNegative long maximumSize) { requireState(this.maximumSize == UNSET_INT, "maximum size was already set to %s", this.maximumSize); requireState(this.maximumWeight == UNSET_INT, @@ -342,7 +343,7 @@ public Caffeine maximumSize(long maximumSize) { * @throws IllegalStateException if a maximum weight or size was already set */ @NonNull - public Caffeine maximumWeight(long maximumWeight) { + public Caffeine maximumWeight(@NonNegative long maximumWeight) { requireState(this.maximumWeight == UNSET_INT, "maximum weight was already set to %s", this.maximumWeight); requireState(this.maximumSize == UNSET_INT, @@ -546,7 +547,7 @@ public Caffeine expireAfterWrite(@NonNull Duration duration) { * @throws IllegalStateException if the time to live or variable expiration was already set */ @NonNull - public Caffeine expireAfterWrite(long duration, @NonNull TimeUnit unit) { + public Caffeine expireAfterWrite(@NonNegative long duration, @NonNull TimeUnit unit) { requireState(expireAfterWriteNanos == UNSET_INT, "expireAfterWrite was already set to %s ns", expireAfterWriteNanos); requireState(expiry == null, "expireAfterAccess may not be used with variable expiration"); @@ -605,7 +606,7 @@ public Caffeine expireAfterAccess(@NonNull Duration duration) { * @throws IllegalStateException if the time to idle or variable expiration was already set */ @NonNull - public Caffeine expireAfterAccess(long duration, @NonNull TimeUnit unit) { + public Caffeine expireAfterAccess(@NonNegative long duration, @NonNull TimeUnit unit) { requireState(expireAfterAccessNanos == UNSET_INT, "expireAfterAccess was already set to %s ns", expireAfterAccessNanos); requireState(expiry == null, "expireAfterAccess may not be used with variable expiration"); @@ -710,7 +711,7 @@ public Caffeine refreshAfterWrite(@NonNull Duration duration) { * @throws IllegalStateException if the refresh interval was already set */ @NonNull - public Caffeine refreshAfterWrite(long duration, @NonNull TimeUnit unit) { + public Caffeine refreshAfterWrite(@NonNegative long duration, @NonNull TimeUnit unit) { requireNonNull(unit); requireState(refreshNanos == UNSET_INT, "refresh was already set to %s ns", refreshNanos); requireArgument(duration > 0, "duration must be positive: %s %s", duration, unit); diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java index 9086a541ec..4d6c345849 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java @@ -23,6 +23,7 @@ import java.util.Objects; import java.util.concurrent.TimeUnit; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import com.github.benmanes.caffeine.cache.Caffeine.Strength; @@ -145,7 +146,7 @@ Caffeine toBuilder() { * @return the parsed specification */ @SuppressWarnings("StringSplitter") - public static CaffeineSpec parse(String specification) { + public static @NonNull CaffeineSpec parse(@NonNull String specification) { CaffeineSpec spec = new CaffeineSpec(specification); for (String option : specification.split(SPLIT_OPTIONS)) { spec.parseOption(option.trim()); diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Expiry.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Expiry.java index c6017e6b73..824dee11f4 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Expiry.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Expiry.java @@ -15,6 +15,7 @@ */ package com.github.benmanes.caffeine.cache; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; /** @@ -57,7 +58,8 @@ public interface Expiry { * @param currentDuration the current duration, in nanoseconds * @return the length of time before the entry expires, in nanoseconds */ - long expireAfterUpdate(@NonNull K key, @NonNull V value, long currentTime, long currentDuration); + long expireAfterUpdate(@NonNull K key, @NonNull V value, + long currentTime, @NonNegative long currentDuration); /** * Specifies that the entry should be automatically removed from the cache once the duration has @@ -75,5 +77,6 @@ public interface Expiry { * @param currentDuration the current duration, in nanoseconds * @return the length of time before the entry expires, in nanoseconds */ - long expireAfterRead(@NonNull K key, @NonNull V value, long currentTime, long currentDuration); + long expireAfterRead(@NonNull K key, @NonNull V value, + long currentTime, @NonNegative long currentDuration); } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/FrequencySketch.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/FrequencySketch.java index 8d2e005d48..8b0230f0a9 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/FrequencySketch.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/FrequencySketch.java @@ -19,6 +19,7 @@ import java.util.concurrent.ThreadLocalRandom; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; /** @@ -87,7 +88,7 @@ public FrequencySketch() { * * @param maximumSize the maximum size of the cache */ - public void ensureCapacity(long maximumSize) { + public void ensureCapacity(@NonNegative long maximumSize) { requireArgument(maximumSize >= 0); int maximum = (int) Math.min(maximumSize, Integer.MAX_VALUE >>> 1); if ((table != null) && (table.length >= maximum)) { @@ -117,6 +118,7 @@ public boolean isNotInitialized() { * @param e the element to count occurrences of * @return the estimated number of occurrences of the element; possibly zero but never negative */ + @NonNegative public int frequency(@NonNull E e) { if (isNotInitialized()) { return 0; diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LoadingCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LoadingCache.java index c003265301..63dd489346 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LoadingCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/LoadingCache.java @@ -89,7 +89,7 @@ public interface LoadingCache extends Cache { * is left unestablished */ @NonNull - Map getAll(@NonNull Iterable keys); + Map<@NonNull K, @NonNull V> getAll(@NonNull Iterable keys); /** * Loads a new value for the {@code key}, asynchronously. While the new value is loading the diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Node.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Node.java index 1695b73363..f6d1c9939d 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Node.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Node.java @@ -17,6 +17,7 @@ import java.lang.ref.ReferenceQueue; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -69,6 +70,7 @@ abstract class Node implements AccessOrder>, WriteOrder coldest(int limit); + Map<@NonNull K, @NonNull V> coldest(@NonNegative int limit); /** * Returns an unmodifiable snapshot {@link Map} view of the cache with ordered traversal. The @@ -197,7 +199,7 @@ default OptionalInt weightOf(@NonNull K key) { * @return a snapshot view of the cache from hottest entry to the coldest */ @NonNull - Map hottest(int limit); + Map<@NonNull K, @NonNull V> hottest(@NonNegative int limit); } /** The low-level operations for a cache with a fixed expiration policy. */ @@ -251,6 +253,7 @@ default Optional ageOf(@NonNull K key) { * @param unit the unit that duration is expressed in * @return the length of time after which an entry should be automatically removed */ + @NonNegative long getExpiresAfter(@NonNull TimeUnit unit); /** @@ -277,7 +280,7 @@ default Duration getExpiresAfter() { * @param unit the unit that {@code duration} is expressed in * @throws IllegalArgumentException if {@code duration} is negative */ - void setExpiresAfter(long duration, @NonNull TimeUnit unit); + void setExpiresAfter(@NonNegative long duration, @NonNull TimeUnit unit); /** * Specifies that each entry should be automatically removed from the cache once a fixed @@ -306,7 +309,7 @@ default void setExpiresAfter(@NonNull Duration duration) { * @return a snapshot view of the cache from oldest entry to the youngest */ @NonNull - Map oldest(int limit); + Map<@NonNull K, @NonNull V> oldest(@NonNegative int limit); /** * Returns an unmodifiable snapshot {@link Map} view of the cache with ordered traversal. The @@ -323,7 +326,7 @@ default void setExpiresAfter(@NonNull Duration duration) { * @return a snapshot view of the cache from youngest entry to the oldest */ @NonNull - Map youngest(int limit); + Map<@NonNull K, @NonNull V> youngest(@NonNegative int limit); } /** The low-level operations for a cache with a variable expiration policy. */ @@ -368,7 +371,7 @@ default Optional getExpiresAfter(@NonNull K key) { * @throws IllegalArgumentException if {@code duration} is negative * @throws NullPointerException if the unit is null */ - void setExpiresAfter(@NonNull K key, long duration, @NonNull TimeUnit unit); + void setExpiresAfter(@NonNull K key, @NonNegative long duration, @NonNull TimeUnit unit); /** * Specifies that the entry should be automatically removed from the cache once the duration has @@ -399,7 +402,7 @@ default void setExpiresAfter(@NonNull K key, @NonNull Duration duration) { * @throws IllegalArgumentException if {@code duration} is negative */ default boolean putIfAbsent(@NonNull K key, @NonNull V value, - long duration, @NonNull TimeUnit unit) { + @NonNegative long duration, @NonNull TimeUnit unit) { // This method was added & implemented in version 2.6.0 throw new UnsupportedOperationException(); } @@ -436,7 +439,8 @@ default boolean putIfAbsent(@NonNull K key, @NonNull V value, @NonNull Duration * @throws IllegalArgumentException if {@code duration} is negative * @throws NullPointerException if the specified key or value is null */ - default void put(@NonNull K key, @NonNull V value, long duration, @NonNull TimeUnit unit) { + default void put(@NonNull K key, @NonNull V value, + @NonNegative long duration, @NonNull TimeUnit unit) { // This method was added & implemented in version 2.6.0 throw new UnsupportedOperationException(); } @@ -472,7 +476,7 @@ default void put(@NonNull K key, @NonNull V value, @NonNull Duration duration) { * @return a snapshot view of the cache from oldest entry to the youngest */ @NonNull - Map oldest(int limit); + Map<@NonNull K, @NonNull V> oldest(@NonNegative int limit); /** * Returns an unmodifiable snapshot {@link Map} view of the cache with ordered traversal. The @@ -489,6 +493,6 @@ default void put(@NonNull K key, @NonNull V value, @NonNull Duration duration) { * @return a snapshot view of the cache from youngest entry to the oldest */ @NonNull - Map youngest(int limit); + Map<@NonNull K, @NonNull V> youngest(@NonNegative int limit); } } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/TimerWheel.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/TimerWheel.java index d7268eb46c..7b78d23868 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/TimerWheel.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/TimerWheel.java @@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -165,7 +166,7 @@ void expire(int index, long previousTicks, long currentTicks) { * * @param node the entry in the cache */ - public void schedule(Node node) { + public void schedule(@NonNull Node node) { Node sentinel = findBucket(node.getVariableTime()); link(sentinel, node); } @@ -175,7 +176,7 @@ public void schedule(Node node) { * * @param node the entry in the cache */ - public void reschedule(Node node) { + public void reschedule(@NonNull Node node) { if (node.getNextInVariableOrder() != null) { unlink(node); schedule(node); @@ -187,7 +188,7 @@ public void reschedule(Node node) { * * @param node the entry in the cache */ - public void deschedule(Node node) { + public void deschedule(@NonNull Node node) { unlink(node); node.setNextInVariableOrder(null); node.setPreviousInVariableOrder(null); @@ -241,7 +242,7 @@ void unlink(Node node) { * @param transformer a function that unwraps the value * @return an unmodifiable snapshot in the desired order */ - public Map snapshot(boolean ascending, int limit, Function transformer) { + public Map snapshot(boolean ascending, int limit, @NonNull Function transformer) { requireArgument(limit >= 0); Map map = new LinkedHashMap<>(Math.min(limit, cache.size())); diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Weigher.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Weigher.java index 4064ecdd22..030ed96982 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Weigher.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Weigher.java @@ -19,6 +19,7 @@ import java.io.Serializable; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; /** @@ -40,6 +41,7 @@ public interface Weigher { * @param value the value to weigh * @return the weight of the entry; must be non-negative */ + @NonNegative int weigh(@NonNull K key, @NonNull V value); /** diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/CacheStats.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/CacheStats.java index f99a85feba..e11e7a5f46 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/CacheStats.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/CacheStats.java @@ -17,6 +17,7 @@ import java.util.Objects; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; import com.github.benmanes.caffeine.cache.Cache; @@ -79,8 +80,9 @@ public final class CacheStats { * @deprecated This constructor is scheduled for removal in version 3.0.0. */ @Deprecated - public CacheStats(long hitCount, long missCount, long loadSuccessCount, - long loadFailureCount, long totalLoadTime, long evictionCount) { + public CacheStats(@NonNegative long hitCount, @NonNegative long missCount, + @NonNegative long loadSuccessCount, @NonNegative long loadFailureCount, + @NonNegative long totalLoadTime, @NonNegative long evictionCount) { this(hitCount, missCount, loadSuccessCount, loadFailureCount, totalLoadTime, evictionCount, 0L); } @@ -98,8 +100,10 @@ public CacheStats(long hitCount, long missCount, long loadSuccessCount, * @param evictionCount the number of entries evicted from the cache * @param evictionWeight the sum of weights of entries evicted from the cache */ - public CacheStats(long hitCount, long missCount, long loadSuccessCount, long loadFailureCount, - long totalLoadTime, long evictionCount, long evictionWeight) { + public CacheStats(@NonNegative long hitCount, @NonNegative long missCount, + @NonNegative long loadSuccessCount, @NonNegative long loadFailureCount, + @NonNegative long totalLoadTime, @NonNegative long evictionCount, + @NonNegative long evictionWeight) { if ((hitCount < 0) || (missCount < 0) || (loadSuccessCount < 0) || (loadFailureCount < 0) || (totalLoadTime < 0) || (evictionCount < 0) || (evictionWeight < 0)) { throw new IllegalArgumentException(); @@ -129,6 +133,7 @@ public static CacheStats empty() { * * @return the {@code hitCount + missCount} */ + @NonNegative public long requestCount() { return hitCount + missCount; } @@ -138,6 +143,7 @@ public long requestCount() { * * @return the number of times {@link Cache} lookup methods have returned a cached value */ + @NonNegative public long hitCount() { return hitCount; } @@ -149,6 +155,7 @@ public long hitCount() { * * @return the ratio of cache requests which were hits */ + @NonNegative public double hitRate() { long requestCount = requestCount(); return (requestCount == 0) ? 1.0 : (double) hitCount / requestCount; @@ -163,6 +170,7 @@ public double hitRate() { * @return the number of times {@link Cache} lookup methods have returned an uncached (newly * loaded) value, or null */ + @NonNegative public long missCount() { return missCount; } @@ -178,6 +186,7 @@ public long missCount() { * * @return the ratio of cache requests which were misses */ + @NonNegative public double missRate() { long requestCount = requestCount(); return (requestCount == 0) ? 0.0 : (double) missCount / requestCount; @@ -190,6 +199,7 @@ public double missRate() { * * @return the {@code loadSuccessCount + loadFailureCount} */ + @NonNegative public long loadCount() { return loadSuccessCount + loadFailureCount; } @@ -203,6 +213,7 @@ public long loadCount() { * * @return the number of times {@link Cache} lookup methods have successfully loaded a new value */ + @NonNegative public long loadSuccessCount() { return loadSuccessCount; } @@ -216,6 +227,7 @@ public long loadSuccessCount() { * * @return the number of times {@link Cache} lookup methods failed to load a new value */ + @NonNegative public long loadFailureCount() { return loadFailureCount; } @@ -227,6 +239,7 @@ public long loadFailureCount() { * * @return the ratio of cache loading attempts which threw exceptions */ + @NonNegative public double loadFailureRate() { long totalLoadCount = loadSuccessCount + loadFailureCount; return (totalLoadCount == 0) @@ -241,6 +254,7 @@ public double loadFailureRate() { * * @return the total number of nanoseconds the cache has spent loading new values */ + @NonNegative public long totalLoadTime() { return totalLoadTime; } @@ -251,6 +265,7 @@ public long totalLoadTime() { * * @return the average time spent loading new values */ + @NonNegative public double averageLoadPenalty() { long totalLoadCount = loadSuccessCount + loadFailureCount; return (totalLoadCount == 0) @@ -264,6 +279,7 @@ public double averageLoadPenalty() { * * @return the number of times an entry has been evicted */ + @NonNegative public long evictionCount() { return evictionCount; } @@ -274,6 +290,7 @@ public long evictionCount() { * * @return the sum of weights of evicted entities */ + @NonNegative public long evictionWeight() { return evictionWeight; } diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/StatsCounter.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/StatsCounter.java index 3141b812ac..44438f36f3 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/StatsCounter.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/stats/StatsCounter.java @@ -17,6 +17,7 @@ import java.util.Map; +import org.checkerframework.checker.index.qual.NonNegative; import org.checkerframework.checker.nullness.qual.NonNull; import com.github.benmanes.caffeine.cache.Cache; @@ -34,7 +35,7 @@ public interface StatsCounter { * * @param count the number of hits to record */ - void recordHits(int count); + void recordHits(@NonNegative int count); /** * Records cache misses. This should be called when a cache request returns a value that was not @@ -46,7 +47,7 @@ public interface StatsCounter { * * @param count the number of misses to record */ - void recordMisses(int count); + void recordMisses(@NonNegative int count); /** * Records the successful load of a new entry. This method should be called when a cache request @@ -56,7 +57,7 @@ public interface StatsCounter { * * @param loadTime the number of nanoseconds the cache spent computing or retrieving the new value */ - void recordLoadSuccess(long loadTime); + void recordLoadSuccess(@NonNegative long loadTime); /** * Records the failed load of a new entry. This method should be called when a cache request @@ -67,7 +68,7 @@ public interface StatsCounter { * @param loadTime the number of nanoseconds the cache spent computing or retrieving the new value * prior to discovering the value doesn't exist or an exception being thrown */ - void recordLoadFailure(long loadTime); + void recordLoadFailure(@NonNegative long loadTime); /** * Records the eviction of an entry from the cache. This should only been called when an entry is @@ -87,7 +88,7 @@ public interface StatsCounter { * * @param weight the weight of the evicted entry */ - default void recordEviction(int weight) { + default void recordEviction(@NonNegative int weight) { // This method will be abstract in version 3.0.0 recordEviction(); } diff --git a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/management/JCacheStatisticsMXBean.java b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/management/JCacheStatisticsMXBean.java index bcdbd9627a..f45772c018 100644 --- a/jcache/src/main/java/com/github/benmanes/caffeine/jcache/management/JCacheStatisticsMXBean.java +++ b/jcache/src/main/java/com/github/benmanes/caffeine/jcache/management/JCacheStatisticsMXBean.java @@ -20,6 +20,8 @@ import javax.cache.management.CacheStatisticsMXBean; +import org.checkerframework.checker.index.qual.NonNegative; + /** * Caffeine JCache statistics. * @@ -79,7 +81,7 @@ public float getCacheHitPercentage() { * * @param count the number of hits to record */ - public void recordHits(long count) { + public void recordHits(@NonNegative long count) { if (enabled) { hits.add(count); } @@ -102,7 +104,7 @@ public float getCacheMissPercentage() { * * @param count the number of misses to record */ - public void recordMisses(long count) { + public void recordMisses(@NonNegative long count) { if (enabled) { misses.add(count); } @@ -123,7 +125,7 @@ public long getCachePuts() { * * @param count the number of writes to record */ - public void recordPuts(long count) { + public void recordPuts(@NonNegative long count) { if (enabled && (count != 0)) { puts.add(count); } @@ -139,7 +141,7 @@ public long getCacheRemovals() { * * @param count the number of removals to record */ - public void recordRemovals(long count) { + public void recordRemovals(@NonNegative long count) { if (enabled) { removals.add(count); } @@ -155,7 +157,7 @@ public long getCacheEvictions() { * * @param count the number of evictions to record */ - public void recordEvictions(long count) { + public void recordEvictions(@NonNegative long count) { if (enabled) { evictions.add(count); } diff --git a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/membership/bloom/BloomFilter.java b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/membership/bloom/BloomFilter.java index 4f3d645064..037ec1152e 100644 --- a/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/membership/bloom/BloomFilter.java +++ b/simulator/src/main/java/com/github/benmanes/caffeine/cache/simulator/membership/bloom/BloomFilter.java @@ -19,6 +19,8 @@ import java.util.Arrays; +import org.checkerframework.checker.index.qual.NonNegative; + import com.github.benmanes.caffeine.cache.simulator.membership.Membership; /** @@ -49,7 +51,8 @@ public final class BloomFilter implements Membership { * @param fpp the false positive probability, where 0.0 > fpp < 1.0 * @param randomSeed the smear to protect against hash flooding, adjusted to an odd value */ - public BloomFilter(long expectedInsertions, double fpp, int randomSeed) { + public BloomFilter(@NonNegative long expectedInsertions, + @NonNegative double fpp, int randomSeed) { this.randomSeed = ((randomSeed & 1) == 0) ? randomSeed + 1 : randomSeed; ensureCapacity(expectedInsertions, fpp); } @@ -62,7 +65,7 @@ public BloomFilter(long expectedInsertions, double fpp, int randomSeed) { * @param expectedInsertions the number of expected insertions * @param fpp the false positive probability, where 0.0 > fpp < 1.0 */ - void ensureCapacity(long expectedInsertions, double fpp) { + void ensureCapacity(@NonNegative long expectedInsertions, @NonNegative double fpp) { checkArgument(expectedInsertions >= 0); checkArgument(fpp > 0 && fpp < 1);