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 a8dff17fb4..b470946604 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 @@ -226,14 +226,20 @@ default CompletableFuture asyncReload( * @throws NullPointerException if the mappingFunction is null */ @SuppressWarnings("FunctionalInterfaceClash") - static CacheLoader bulk(Function, - ? extends Map> mappingFunction) { + static CacheLoader bulk(Function, + ? extends Map> mappingFunction) { requireNonNull(mappingFunction); return new CacheLoader<>() { - @Override public @Nullable V load(K key) { + /* + * If the caller passes a mapping function that may ever return partial results, then calls to + * load() may return null. In that case, the caller should type the return value of bulk(...) + * as a CacheLoader, rather than a CacheLoader. + */ + @SuppressWarnings("NullAway") + @Override public V load(K key) { return loadAll(Set.of(key)).get(key); } - @Override public Map loadAll(Set keys) { + @Override public Map loadAll(Set keys) { return mappingFunction.apply(keys); } };