Skip to content

Commit

Permalink
Allow async loads to return null.
Browse files Browse the repository at this point in the history
This makes them match what `CacheLoader` already allows for synchronous
loading and reloading via `@Nullable V load(K key)` and `@Nullable V
reload(K key, V oldValue)`.
  • Loading branch information
cpovirk committed Dec 3, 2024
1 parent 7b93d04 commit 1b767fb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -62,7 +63,7 @@ public interface AsyncCacheLoader<K, V> {
* treated like any other {@code Exception} in all respects except that, when it is
* caught, the thread's interrupt status is set
*/
CompletableFuture<? extends V> asyncLoad(K key, Executor executor) throws Exception;
CompletableFuture<? extends @Nullable V> asyncLoad(K key, Executor executor) throws Exception;

/**
* Asynchronously computes or retrieves the values corresponding to {@code keys}. This method is
Expand Down Expand Up @@ -114,7 +115,7 @@ public interface AsyncCacheLoader<K, V> {
* treated like any other {@code Exception} in all respects except that, when it is
* caught, the thread's interrupt status is set
*/
default CompletableFuture<? extends V> asyncReload(
default CompletableFuture<? extends @Nullable V> asyncReload(
K key, V oldValue, Executor executor) throws Exception {
return asyncLoad(key, executor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public interface CacheLoader<K, V> extends AsyncCacheLoader<K, V> {
* @return the future value associated with {@code key}
*/
@Override
default CompletableFuture<? extends V> asyncLoad(K key, Executor executor) throws Exception {
default CompletableFuture<? extends @Nullable V> asyncLoad(K key, Executor executor) throws Exception {
requireNonNull(key);
requireNonNull(executor);
return CompletableFuture.supplyAsync(() -> {
Expand Down Expand Up @@ -193,7 +193,7 @@ default CompletableFuture<? extends V> asyncLoad(K key, Executor executor) throw
* {@code null} if the mapping is to be removed
*/
@Override
default CompletableFuture<? extends V> asyncReload(
default CompletableFuture<? extends @Nullable V> asyncReload(
K key, V oldValue, Executor executor) throws Exception {
requireNonNull(key);
requireNonNull(executor);
Expand Down

0 comments on commit 1b767fb

Please sign in to comment.