Skip to content

Commit

Permalink
Add @CheckReturnValue to some com.google.common.cache APIs.
Browse files Browse the repository at this point in the history
RELNOTES=Add `@CheckReturnValue` to some `com.google.common.cache` APIs.
PiperOrigin-RevId: 356028145
  • Loading branch information
kluever authored and Google Java Core Libraries committed Feb 6, 2021
1 parent c9f4b9f commit a5ef129
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,13 @@ public void testEqualsAndHashCode() {
.testEquals();
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumWeight_withWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000"));
builder.weigher(constantWeigher(42)).build(CacheLoader.from(Suppliers.ofInstance(null)));
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumWeight_withoutWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000"));
try {
Expand All @@ -482,11 +484,13 @@ public void testMaximumWeight_withoutWeigher() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumSize_withWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumSize=9000"));
builder.weigher(constantWeigher(42)).build(CacheLoader.from(Suppliers.ofInstance(null)));
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumSize_withoutWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumSize=9000"));
builder.build(CacheLoader.from(Suppliers.ofInstance(null)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public void testTimeToLive_negative() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testTimeToLive_small() {
CacheBuilder.newBuilder().expireAfterWrite(1, NANOSECONDS).build(identityLoader());
// well, it didn't blow up.
Expand All @@ -310,6 +311,7 @@ public void testTimeToIdle_negative() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testTimeToIdle_small() {
CacheBuilder.newBuilder().expireAfterAccess(1, NANOSECONDS).build(identityLoader());
// well, it didn't blow up.
Expand All @@ -326,6 +328,7 @@ public void testTimeToIdle_setTwice() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testTimeToIdleAndToLive() {
CacheBuilder.newBuilder()
.expireAfterWrite(1, NANOSECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testInvalidateAll() {

public void testSize() {
when(mock.size()).thenReturn(0L);
forward.size();
long unused = forward.size();
}

public void testStats() {
Expand Down
4 changes: 4 additions & 0 deletions android/guava/src/com/google/common/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.CompatibleWith;
import com.google.errorprone.annotations.DoNotMock;
import java.util.Map;
Expand Down Expand Up @@ -141,6 +142,7 @@ public interface Cache<K, V> {
void invalidateAll();

/** Returns the approximate number of entries in this cache. */
@CheckReturnValue
long size();

/**
Expand All @@ -154,6 +156,7 @@ public interface Cache<K, V> {
* all values is returned.
*
*/
@CheckReturnValue
CacheStats stats();

/**
Expand All @@ -169,6 +172,7 @@ public interface Cache<K, V> {
* {@code ConcurrentMap} documentation. They will not function correctly and it is impossible for
* Guava to fix them until Guava is ready to <i>require</i> Java 8 for all users.
*/
@CheckReturnValue
ConcurrentMap<K, V> asMap();

/**
Expand Down
5 changes: 5 additions & 0 deletions android/guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ private CacheBuilder() {}
* <p>Note that while this return type is {@code CacheBuilder<Object, Object>}, type parameters on
* the {@link #build} methods allow you to create a cache of any key and value type desired.
*/
@CheckReturnValue
public static CacheBuilder<Object, Object> newBuilder() {
return new CacheBuilder<>();
}
Expand All @@ -271,6 +272,7 @@ public static CacheBuilder<Object, Object> newBuilder() {
* @since 12.0
*/
@GwtIncompatible // To be supported
@CheckReturnValue
public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
return spec.toCacheBuilder().lenientParsing();
}
Expand All @@ -283,6 +285,7 @@ public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
* @since 12.0
*/
@GwtIncompatible // To be supported
@CheckReturnValue
public static CacheBuilder<Object, Object> from(String spec) {
return from(CacheBuilderSpec.parse(spec));
}
Expand Down Expand Up @@ -845,6 +848,7 @@ Supplier<? extends StatsCounter> getStatsCounterSupplier() {
* @param loader the cache loader used to obtain new values
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
CacheLoader<? super K1, V1> loader) {
checkWeightWithWeigher();
Expand All @@ -863,6 +867,7 @@ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
* @return a cache having the requested features
* @since 11.0
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
checkWeightWithWeigher();
checkNonLoadingCache();
Expand Down
4 changes: 4 additions & 0 deletions android/guava/src/com/google/common/cache/CacheLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.errorprone.annotations.CheckReturnValue;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -136,6 +137,7 @@ public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception {
* @param function the function to be used for loading values; must never return {@code null}
* @return a cache loader that loads values by passing each key to {@code function}
*/
@CheckReturnValue
public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
return new FunctionToCacheLoader<>(function);
}
Expand All @@ -149,6 +151,7 @@ public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
* @return a cache loader that loads values by calling {@link Supplier#get}, irrespective of the
* key
*/
@CheckReturnValue
public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) {
return new SupplierToCacheLoader<V>(supplier);
}
Expand Down Expand Up @@ -178,6 +181,7 @@ public V load(K key) {
*
* @since 17.0
*/
@CheckReturnValue
@GwtIncompatible // Executor + Futures
public static <K, V> CacheLoader<K, V> asyncReloading(
final CacheLoader<K, V> loader, final Executor executor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,13 @@ public void testEqualsAndHashCode() {
.testEquals();
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumWeight_withWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000"));
builder.weigher(constantWeigher(42)).build(CacheLoader.from(Suppliers.ofInstance(null)));
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumWeight_withoutWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000"));
try {
Expand All @@ -482,11 +484,13 @@ public void testMaximumWeight_withoutWeigher() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumSize_withWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumSize=9000"));
builder.weigher(constantWeigher(42)).build(CacheLoader.from(Suppliers.ofInstance(null)));
}

@SuppressWarnings("ReturnValueIgnored")
public void testMaximumSize_withoutWeigher() {
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumSize=9000"));
builder.build(CacheLoader.from(Suppliers.ofInstance(null)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ public void testTimeToLive_negative_duration() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testTimeToLive_small() {
CacheBuilder.newBuilder().expireAfterWrite(1, NANOSECONDS).build(identityLoader());
// well, it didn't blow up.
Expand Down Expand Up @@ -352,6 +353,7 @@ public void testTimeToIdle_negative_duration() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testTimeToIdle_small() {
CacheBuilder.newBuilder().expireAfterAccess(1, NANOSECONDS).build(identityLoader());
// well, it didn't blow up.
Expand Down Expand Up @@ -380,6 +382,7 @@ public void testTimeToIdle_setTwice_duration() {
}
}

@SuppressWarnings("ReturnValueIgnored")
public void testTimeToIdleAndToLive() {
CacheBuilder.newBuilder()
.expireAfterWrite(1, NANOSECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void testInvalidateAll() {

public void testSize() {
when(mock.size()).thenReturn(0L);
forward.size();
long unused = forward.size();
}

public void testStats() {
Expand Down
4 changes: 4 additions & 0 deletions guava/src/com/google/common/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ExecutionError;
import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.errorprone.annotations.CheckReturnValue;
import com.google.errorprone.annotations.CompatibleWith;
import com.google.errorprone.annotations.DoNotMock;
import java.util.Map;
Expand Down Expand Up @@ -141,6 +142,7 @@ public interface Cache<K, V> {
void invalidateAll();

/** Returns the approximate number of entries in this cache. */
@CheckReturnValue
long size();

/**
Expand All @@ -154,6 +156,7 @@ public interface Cache<K, V> {
* all values is returned.
*
*/
@CheckReturnValue
CacheStats stats();

/**
Expand All @@ -164,6 +167,7 @@ public interface Cache<K, V> {
* concurrent use, but if the cache is modified (including by eviction) after the iterator is
* created, it is undefined which of the changes (if any) will be reflected in that iterator.
*/
@CheckReturnValue
ConcurrentMap<K, V> asMap();

/**
Expand Down
5 changes: 5 additions & 0 deletions guava/src/com/google/common/cache/CacheBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ private CacheBuilder() {}
* <p>Note that while this return type is {@code CacheBuilder<Object, Object>}, type parameters on
* the {@link #build} methods allow you to create a cache of any key and value type desired.
*/
@CheckReturnValue
public static CacheBuilder<Object, Object> newBuilder() {
return new CacheBuilder<>();
}
Expand All @@ -270,6 +271,7 @@ public static CacheBuilder<Object, Object> newBuilder() {
* @since 12.0
*/
@GwtIncompatible // To be supported
@CheckReturnValue
public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
return spec.toCacheBuilder().lenientParsing();
}
Expand All @@ -282,6 +284,7 @@ public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
* @since 12.0
*/
@GwtIncompatible // To be supported
@CheckReturnValue
public static CacheBuilder<Object, Object> from(String spec) {
return from(CacheBuilderSpec.parse(spec));
}
Expand Down Expand Up @@ -945,6 +948,7 @@ Supplier<? extends StatsCounter> getStatsCounterSupplier() {
* @param loader the cache loader used to obtain new values
* @return a cache having the requested features
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
CacheLoader<? super K1, V1> loader) {
checkWeightWithWeigher();
Expand All @@ -963,6 +967,7 @@ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
* @return a cache having the requested features
* @since 11.0
*/
@CheckReturnValue
public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
checkWeightWithWeigher();
checkNonLoadingCache();
Expand Down
4 changes: 4 additions & 0 deletions guava/src/com/google/common/cache/CacheLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListenableFutureTask;
import com.google.errorprone.annotations.CheckReturnValue;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -135,6 +136,7 @@ public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception {
* @param function the function to be used for loading values; must never return {@code null}
* @return a cache loader that loads values by passing each key to {@code function}
*/
@CheckReturnValue
public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
return new FunctionToCacheLoader<>(function);
}
Expand All @@ -148,6 +150,7 @@ public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
* @return a cache loader that loads values by calling {@link Supplier#get}, irrespective of the
* key
*/
@CheckReturnValue
public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) {
return new SupplierToCacheLoader<V>(supplier);
}
Expand Down Expand Up @@ -177,6 +180,7 @@ public V load(K key) {
*
* @since 17.0
*/
@CheckReturnValue
@GwtIncompatible // Executor + Futures
public static <K, V> CacheLoader<K, V> asyncReloading(
final CacheLoader<K, V> loader, final Executor executor) {
Expand Down

0 comments on commit a5ef129

Please sign in to comment.