diff --git a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCache.java b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCache.java index a7e5d37af0..90ae3d7c4e 100644 --- a/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCache.java +++ b/caffeine/src/main/java/com/github/benmanes/caffeine/cache/AsyncCache.java @@ -23,7 +23,9 @@ import java.util.function.BiFunction; import java.util.function.Function; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.NullUnmarked; import org.jspecify.annotations.Nullable; /** @@ -71,7 +73,9 @@ public interface AsyncCache { * @return the current (existing or computed) future value associated with the specified key * @throws NullPointerException if the specified key or mappingFunction is null */ - CompletableFuture get(K key, Function mappingFunction); + @NullUnmarked + @NonNull CompletableFuture get( + @NonNull K key, @NonNull Function mappingFunction); /** * Returns the future associated with the {@code key} in this cache, obtaining that value from @@ -96,8 +100,15 @@ public interface AsyncCache { * @throws RuntimeException or Error if the mappingFunction does when constructing the future, * in which case the mapping is left unestablished */ - CompletableFuture get(K key, BiFunction> mappingFunction); + @NullUnmarked + @NonNull CompletableFuture get( + @NonNull K key, + @NonNull + BiFunction< + ? super @NonNull K, + ? super @NonNull Executor, + ? extends @NonNull CompletableFuture> + mappingFunction); /** * Returns the future of a map of the values associated with the {@code keys}, creating or @@ -170,7 +181,7 @@ CompletableFuture> getAll(Iterable keys, * @param valueFuture the value to be associated with the specified key * @throws NullPointerException if the specified key or value is null */ - void put(K key, CompletableFuture valueFuture); + void put(K key, CompletableFuture valueFuture); /** * Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to @@ -187,7 +198,8 @@ CompletableFuture> getAll(Iterable keys, * * @return a thread-safe view of this cache supporting all of the optional {@link Map} operations */ - ConcurrentMap> asMap(); + @NullUnmarked + @NonNull ConcurrentMap<@NonNull K, @NonNull CompletableFuture> asMap(); /** * Returns a view of the entries stored in this cache as a synchronous {@link Cache}. A mapping is 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 745922807a..8abff8d7e1 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 @@ -25,6 +25,7 @@ import java.util.function.Function; import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.Nullable; /** * Computes or retrieves values asynchronously based on a key, for use in populating a @@ -62,7 +63,7 @@ public interface AsyncCacheLoader { * treated like any other {@code Exception} in all respects except that, when it is * caught, the thread's interrupt status is set */ - CompletableFuture asyncLoad(K key, Executor executor) throws Exception; + CompletableFuture asyncLoad(K key, Executor executor) throws Exception; /** * Asynchronously computes or retrieves the values corresponding to {@code keys}. This method is @@ -114,7 +115,7 @@ public interface AsyncCacheLoader { * treated like any other {@code Exception} in all respects except that, when it is * caught, the thread's interrupt status is set */ - default CompletableFuture asyncReload( + default CompletableFuture asyncReload( K key, V oldValue, Executor executor) throws Exception { return asyncLoad(key, executor); } 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 8b60c3168d..f714203e92 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 @@ -18,7 +18,9 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.NullUnmarked; /** * A semi-persistent mapping from keys to values. Values are automatically loaded by the cache @@ -50,7 +52,8 @@ public interface AsyncLoadingCache extends AsyncCache { * @throws RuntimeException or Error if the {@link AsyncCacheLoader} does when constructing the * future, in which case the mapping is left unestablished */ - CompletableFuture get(K key); + @NullUnmarked + @NonNull CompletableFuture get(@NonNull K key); /** * Returns the future of a map of the values associated with {@code keys}, creating or retrieving 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 cddcd68179..8f825ed90a 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 @@ -20,7 +20,9 @@ import java.util.concurrent.ConcurrentMap; import java.util.function.Function; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.NullUnmarked; import org.jspecify.annotations.Nullable; import com.github.benmanes.caffeine.cache.stats.CacheStats; @@ -77,7 +79,8 @@ public interface Cache { * @throws RuntimeException or Error if the mappingFunction does so, in which case the mapping is * left unestablished */ - V get(K key, Function mappingFunction); + @NullUnmarked + V get(@NonNull K key, Function mappingFunction); /** * Returns a map of the values associated with the {@code keys} in this cache. The returned map 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 217c81a868..3d8b9ca642 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 @@ -101,7 +101,7 @@ public interface CacheLoader extends AsyncCacheLoader { * @return the future value associated with {@code key} */ @Override - default CompletableFuture asyncLoad(K key, Executor executor) throws Exception { + default CompletableFuture asyncLoad(K key, Executor executor) throws Exception { requireNonNull(key); requireNonNull(executor); return CompletableFuture.supplyAsync(() -> { @@ -193,7 +193,7 @@ default CompletableFuture asyncLoad(K key, Executor executor) throw * {@code null} if the mapping is to be removed */ @Override - default CompletableFuture asyncReload( + default CompletableFuture asyncReload( K key, V oldValue, Executor executor) throws Exception { requireNonNull(key); requireNonNull(executor); 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 c06aeb2116..f354e84fb9 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 @@ -19,7 +19,9 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; +import org.jspecify.annotations.NonNull; import org.jspecify.annotations.NullMarked; +import org.jspecify.annotations.NullUnmarked; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -62,7 +64,8 @@ public interface LoadingCache extends Cache { * @throws RuntimeException or Error if the {@link CacheLoader} does so, in which case the mapping * is left unestablished */ - V get(K key); + @NullUnmarked + V get(@NonNull K key); /** * Returns a map of the values associated with the {@code keys}, creating or retrieving those @@ -110,7 +113,8 @@ public interface LoadingCache extends Cache { * @throws NullPointerException if the specified key is null */ @CanIgnoreReturnValue - CompletableFuture refresh(K key); + @NullUnmarked + @NonNull CompletableFuture refresh(@NonNull K key); /** * Loads a new value for each {@code key}, asynchronously. While the new value is loading the