Skip to content

Commit

Permalink
Mirror usage of @CheckReturnValue from Guava
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-manes committed Feb 8, 2021
1 parent 3aa6b55 commit f697f2b
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import com.google.errorprone.annotations.CheckReturnValue;

/**
* A semi-persistent mapping from keys to values. Cache entries are manually added using
* {@link #get(Object, Function)} or {@link #put(Object, CompletableFuture)}, and are stored in the
Expand Down Expand Up @@ -178,6 +180,7 @@ CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys,
*
* @return a thread-safe view of this cache supporting all of the optional {@link Map} operations
*/
@CheckReturnValue
ConcurrentMap<K, CompletableFuture<V>> asMap();

/**
Expand All @@ -188,5 +191,6 @@ CompletableFuture<Map<K, V>> getAll(Iterable<? extends K> keys,
*
* @return a thread-safe synchronous view of this cache
*/
@CheckReturnValue
Cache<K, V> synchronous();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import org.checkerframework.checker.nullness.qual.NonNull;

import com.google.errorprone.annotations.CheckReturnValue;

/**
* Computes or retrieves values asynchronously, based on a key, for use in populating a
* {@link AsyncLoadingCache}.
Expand Down Expand Up @@ -125,6 +127,7 @@ default CompletableFuture<? extends V> asyncReload(
* @return an asynchronous cache loader that delegates to the supplied {@code mappingFunction}
* @throws NullPointerException if the mappingFunction is null
*/
@CheckReturnValue
static <K extends Object, V extends Object> AsyncCacheLoader<K, V> bulk(
Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction) {
return CacheLoader.bulk(mappingFunction);
Expand All @@ -146,6 +149,7 @@ static <K extends Object, V extends Object> AsyncCacheLoader<K, V> bulk(
* @return an asynchronous cache loader that delegates to the supplied {@code mappingFunction}
* @throws NullPointerException if the mappingFunction is null
*/
@CheckReturnValue
static <K extends Object, V extends Object> AsyncCacheLoader<K, V> bulk(
BiFunction<? super Set<? extends K>, ? super Executor,
? extends CompletableFuture<? extends Map<? extends K, ? extends V>>> mappingFunction) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import org.checkerframework.checker.nullness.qual.NonNull;

import com.google.errorprone.annotations.CheckReturnValue;

/**
* A semi-persistent mapping from keys to values. Values are automatically loaded by the cache
* asynchronously, and are stored in the cache until either evicted or manually invalidated.
Expand Down Expand Up @@ -89,5 +91,6 @@ public interface AsyncLoadingCache<K extends @NonNull Object, V extends @NonNull
* @return a thread-safe synchronous view of this cache
*/
@Override
@CheckReturnValue
LoadingCache<K, V> synchronous();
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.checkerframework.checker.nullness.qual.PolyNull;

import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.google.errorprone.annotations.CheckReturnValue;

/**
* A semi-persistent mapping from keys to values. Cache entries are manually added using
Expand Down Expand Up @@ -180,6 +181,7 @@ Map<K, V> getAll(Iterable<? extends K> keys,
* @return the estimated number of mappings
*/
@NonNegative
@CheckReturnValue
long estimatedSize();

/**
Expand All @@ -191,6 +193,7 @@ Map<K, V> getAll(Iterable<? extends K> keys,
*
* @return the current snapshot of the statistics of this cache
*/
@CheckReturnValue
CacheStats stats();

/**
Expand All @@ -208,6 +211,7 @@ Map<K, V> getAll(Iterable<? extends K> keys,
*
* @return a thread-safe view of this cache supporting all of the optional {@link Map} operations
*/
@CheckReturnValue
ConcurrentMap<K, V> asMap();

/**
Expand All @@ -223,5 +227,6 @@ Map<K, V> getAll(Iterable<? extends K> keys,
*
* @return access to inspect and perform advanced operations based on the cache's characteristics
*/
@CheckReturnValue
Policy<K, V> policy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import com.google.errorprone.annotations.CheckReturnValue;

/**
* Computes or retrieves values, based on a key, for use in populating a {@link LoadingCache} or
* {@link AsyncLoadingCache}.
Expand Down Expand Up @@ -211,6 +213,7 @@ default CompletableFuture<? extends V> asyncReload(
* @return a cache loader that delegates to the supplied {@code mappingFunction}
* @throws NullPointerException if the mappingFunction is null
*/
@CheckReturnValue
static <K extends Object, V extends Object> CacheLoader<K, V> bulk(
Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction) {
requireNonNull(mappingFunction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.github.benmanes.caffeine.cache.stats.ConcurrentStatsCounter;
import com.github.benmanes.caffeine.cache.stats.StatsCounter;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.FormatMethod;

/**
Expand Down Expand Up @@ -224,6 +225,7 @@ static long ceilingPowerOfTwo(long x) {
*
* @return a new instance with default settings
*/
@CheckReturnValue
public static Caffeine<Object, Object> newBuilder() {
return new Caffeine<>();
}
Expand All @@ -234,6 +236,7 @@ public static Caffeine<Object, Object> newBuilder() {
* @param spec the specification to build from
* @return a new instance with the specification's settings
*/
@CheckReturnValue
public static Caffeine<Object, Object> from(CaffeineSpec spec) {
Caffeine<Object, Object> builder = spec.toBuilder();
builder.strictParsing = false;
Expand All @@ -246,6 +249,7 @@ public static Caffeine<Object, Object> from(CaffeineSpec spec) {
* @param spec a String in the format specified by {@link CaffeineSpec}
* @return a new instance with the specification's settings
*/
@CheckReturnValue
public static Caffeine<Object, Object> from(String spec) {
return from(CaffeineSpec.parse(spec));
}
Expand Down Expand Up @@ -1030,6 +1034,7 @@ boolean isBounded() {
* @param <V1> the value type of the cache
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
requireWeightWithWeigher();
requireNonLoadingCache();
Expand All @@ -1055,6 +1060,7 @@ public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
* @param <V1> the value type of the loader
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
CacheLoader<? super K1, V1> loader) {
requireWeightWithWeigher();
Expand Down Expand Up @@ -1086,6 +1092,7 @@ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
* @param <V1> the value type of the cache
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> AsyncCache<K1, V1> buildAsync() {
requireState(valueStrength == null, "Weak or soft values can not be combined with AsyncCache");
requireState(writer == null, "CacheWriter can not be combined with AsyncCache");
Expand Down Expand Up @@ -1117,6 +1124,7 @@ public <K1 extends K, V1 extends V> AsyncCache<K1, V1> buildAsync() {
* @param <V1> the value type of the loader
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> AsyncLoadingCache<K1, V1> buildAsync(
CacheLoader<? super K1, V1> loader) {
return buildAsync((AsyncCacheLoader<? super K1, V1>) loader);
Expand All @@ -1140,6 +1148,7 @@ public <K1 extends K, V1 extends V> AsyncLoadingCache<K1, V1> buildAsync(
* @param <V1> the value type of the loader
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> AsyncLoadingCache<K1, V1> buildAsync(
AsyncCacheLoader<? super K1, V1> loader) {
requireState(valueStrength == null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,16 @@ public void asyncReload() throws Exception {
assertThat(future.join(), is(-1));
}

@SuppressWarnings("CheckReturnValue")
@Test(expectedExceptions = NullPointerException.class)
public void bulk_function_null_() {
public void bulk_function_null() {
Function<Set<? extends Integer>, Map<Integer, Integer>> f = null;
AsyncCacheLoader.bulk(f);
}

@SuppressWarnings("CheckReturnValue")
@Test(expectedExceptions = NullPointerException.class)
public void bulk_bifunction_null_() {
public void bulk_bifunction_null() {
BiFunction<Set<? extends Integer>, Executor, CompletableFuture<Map<Integer, Integer>>> f = null;
AsyncCacheLoader.bulk(f);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*
* @author [email protected] (Ben Manes)
*/
@SuppressWarnings({"PreferJavaTimeOverload", "deprecation"})
@SuppressWarnings({"PreferJavaTimeOverload", "deprecation", "CheckReturnValue"})
public final class CaffeineTest {
@Mock StatsCounter statsCounter;
@Mock Expiry<Object, Object> expiry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ public void asyncReload() throws Exception {
assertThat(future.get(), is(-1));
}

@SuppressWarnings("CheckReturnValue")
@Test(expectedExceptions = NullPointerException.class)
public void bulk_null() {
CacheLoader.bulk(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.errorprone.annotations.CheckReturnValue;

/**
* An adapter to expose a Caffeine cache through the Guava interfaces.
Expand All @@ -39,6 +40,7 @@ private CaffeinatedGuava() {}
* @param builder the configured cache builder
* @return a cache exposed under the Guava APIs
*/
@CheckReturnValue
public static <K, V, K1 extends K, V1 extends V> Cache<K1, V1> build(Caffeine<K, V> builder) {
return new CaffeinatedGuavaCache<>(builder.build());
}
Expand All @@ -50,6 +52,7 @@ public static <K, V, K1 extends K, V1 extends V> Cache<K1, V1> build(Caffeine<K,
* @param loader the cache loader used to obtain new values
* @return a cache exposed under the Guava APIs
*/
@CheckReturnValue
public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
Caffeine<K, V> builder, CacheLoader<? super K1, V1> loader) {
@SuppressWarnings("unchecked")
Expand All @@ -66,6 +69,7 @@ public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
* @param loader the cache loader used to obtain new values
* @return a cache exposed under the Guava APIs
*/
@CheckReturnValue
public static <K, V, K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
Caffeine<K, V> builder,
com.github.benmanes.caffeine.cache.CacheLoader<? super K1, V1> loader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*
* @author Adam Winer
*/
@SuppressWarnings("PreferJavaTimeOverload")
@SuppressWarnings({"PreferJavaTimeOverload", "CheckReturnValue"})
public class CaffeineSpecGuavaTest extends TestCase {

public void testParse_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public void testTimeToLive_negative_duration() {
}
}

@SuppressWarnings("CheckReturnValue")
public void testTimeToLive_small() {
CaffeinatedGuava.build(Caffeine.newBuilder()
.expireAfterWrite(1, NANOSECONDS), identityLoader());
Expand Down Expand Up @@ -284,6 +285,7 @@ public void testTimeToIdle_negative_duration() {
}
}

@SuppressWarnings("CheckReturnValue")
public void testTimeToIdle_small() {
CaffeinatedGuava.build(Caffeine.newBuilder()
.expireAfterAccess(1, NANOSECONDS), identityLoader());
Expand Down Expand Up @@ -312,6 +314,7 @@ public void testTimeToIdle_setTwice_duration() {
}
}

@SuppressWarnings("CheckReturnValue")
public void testTimeToIdleAndToLive() {
CaffeinatedGuava.build(Caffeine.newBuilder()
.expireAfterWrite(1, NANOSECONDS)
Expand Down

0 comments on commit f697f2b

Please sign in to comment.