Skip to content

Commit a5ef129

Browse files
klueverGoogle Java Core Libraries
authored andcommitted
Add @CheckReturnValue to some com.google.common.cache APIs.
RELNOTES=Add `@CheckReturnValue` to some `com.google.common.cache` APIs. PiperOrigin-RevId: 356028145
1 parent c9f4b9f commit a5ef129

File tree

12 files changed

+42
-2
lines changed

12 files changed

+42
-2
lines changed

android/guava-tests/test/com/google/common/cache/CacheBuilderSpecTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,13 @@ public void testEqualsAndHashCode() {
468468
.testEquals();
469469
}
470470

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

477+
@SuppressWarnings("ReturnValueIgnored")
476478
public void testMaximumWeight_withoutWeigher() {
477479
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000"));
478480
try {
@@ -482,11 +484,13 @@ public void testMaximumWeight_withoutWeigher() {
482484
}
483485
}
484486

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

493+
@SuppressWarnings("ReturnValueIgnored")
490494
public void testMaximumSize_withoutWeigher() {
491495
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumSize=9000"));
492496
builder.build(CacheLoader.from(Suppliers.ofInstance(null)));

android/guava-tests/test/com/google/common/cache/CacheBuilderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ public void testTimeToLive_negative() {
285285
}
286286
}
287287

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

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

331+
@SuppressWarnings("ReturnValueIgnored")
329332
public void testTimeToIdleAndToLive() {
330333
CacheBuilder.newBuilder()
331334
.expireAfterWrite(1, NANOSECONDS)

android/guava-tests/test/com/google/common/cache/ForwardingLoadingCacheTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testInvalidateAll() {
9090

9191
public void testSize() {
9292
when(mock.size()).thenReturn(0L);
93-
forward.size();
93+
long unused = forward.size();
9494
}
9595

9696
public void testStats() {

android/guava/src/com/google/common/cache/Cache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableMap;
1919
import com.google.common.util.concurrent.ExecutionError;
2020
import com.google.common.util.concurrent.UncheckedExecutionException;
21+
import com.google.errorprone.annotations.CheckReturnValue;
2122
import com.google.errorprone.annotations.CompatibleWith;
2223
import com.google.errorprone.annotations.DoNotMock;
2324
import java.util.Map;
@@ -141,6 +142,7 @@ public interface Cache<K, V> {
141142
void invalidateAll();
142143

143144
/** Returns the approximate number of entries in this cache. */
145+
@CheckReturnValue
144146
long size();
145147

146148
/**
@@ -154,6 +156,7 @@ public interface Cache<K, V> {
154156
* all values is returned.
155157
*
156158
*/
159+
@CheckReturnValue
157160
CacheStats stats();
158161

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

174178
/**

android/guava/src/com/google/common/cache/CacheBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ private CacheBuilder() {}
261261
* <p>Note that while this return type is {@code CacheBuilder<Object, Object>}, type parameters on
262262
* the {@link #build} methods allow you to create a cache of any key and value type desired.
263263
*/
264+
@CheckReturnValue
264265
public static CacheBuilder<Object, Object> newBuilder() {
265266
return new CacheBuilder<>();
266267
}
@@ -271,6 +272,7 @@ public static CacheBuilder<Object, Object> newBuilder() {
271272
* @since 12.0
272273
*/
273274
@GwtIncompatible // To be supported
275+
@CheckReturnValue
274276
public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
275277
return spec.toCacheBuilder().lenientParsing();
276278
}
@@ -283,6 +285,7 @@ public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
283285
* @since 12.0
284286
*/
285287
@GwtIncompatible // To be supported
288+
@CheckReturnValue
286289
public static CacheBuilder<Object, Object> from(String spec) {
287290
return from(CacheBuilderSpec.parse(spec));
288291
}
@@ -845,6 +848,7 @@ Supplier<? extends StatsCounter> getStatsCounterSupplier() {
845848
* @param loader the cache loader used to obtain new values
846849
* @return a cache having the requested features
847850
*/
851+
@CheckReturnValue
848852
public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
849853
CacheLoader<? super K1, V1> loader) {
850854
checkWeightWithWeigher();
@@ -863,6 +867,7 @@ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
863867
* @return a cache having the requested features
864868
* @since 11.0
865869
*/
870+
@CheckReturnValue
866871
public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
867872
checkWeightWithWeigher();
868873
checkNonLoadingCache();

android/guava/src/com/google/common/cache/CacheLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.util.concurrent.Futures;
2424
import com.google.common.util.concurrent.ListenableFuture;
2525
import com.google.common.util.concurrent.ListenableFutureTask;
26+
import com.google.errorprone.annotations.CheckReturnValue;
2627
import java.io.Serializable;
2728
import java.util.Map;
2829
import java.util.concurrent.Callable;
@@ -136,6 +137,7 @@ public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception {
136137
* @param function the function to be used for loading values; must never return {@code null}
137138
* @return a cache loader that loads values by passing each key to {@code function}
138139
*/
140+
@CheckReturnValue
139141
public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
140142
return new FunctionToCacheLoader<>(function);
141143
}
@@ -149,6 +151,7 @@ public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
149151
* @return a cache loader that loads values by calling {@link Supplier#get}, irrespective of the
150152
* key
151153
*/
154+
@CheckReturnValue
152155
public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) {
153156
return new SupplierToCacheLoader<V>(supplier);
154157
}
@@ -178,6 +181,7 @@ public V load(K key) {
178181
*
179182
* @since 17.0
180183
*/
184+
@CheckReturnValue
181185
@GwtIncompatible // Executor + Futures
182186
public static <K, V> CacheLoader<K, V> asyncReloading(
183187
final CacheLoader<K, V> loader, final Executor executor) {

guava-tests/test/com/google/common/cache/CacheBuilderSpecTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,13 @@ public void testEqualsAndHashCode() {
468468
.testEquals();
469469
}
470470

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

477+
@SuppressWarnings("ReturnValueIgnored")
476478
public void testMaximumWeight_withoutWeigher() {
477479
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumWeight=9000"));
478480
try {
@@ -482,11 +484,13 @@ public void testMaximumWeight_withoutWeigher() {
482484
}
483485
}
484486

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

493+
@SuppressWarnings("ReturnValueIgnored")
490494
public void testMaximumSize_withoutWeigher() {
491495
CacheBuilder<Object, Object> builder = CacheBuilder.from(parse("maximumSize=9000"));
492496
builder.build(CacheLoader.from(Suppliers.ofInstance(null)));

guava-tests/test/com/google/common/cache/CacheBuilderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ public void testTimeToLive_negative_duration() {
305305
}
306306
}
307307

308+
@SuppressWarnings("ReturnValueIgnored")
308309
public void testTimeToLive_small() {
309310
CacheBuilder.newBuilder().expireAfterWrite(1, NANOSECONDS).build(identityLoader());
310311
// well, it didn't blow up.
@@ -352,6 +353,7 @@ public void testTimeToIdle_negative_duration() {
352353
}
353354
}
354355

356+
@SuppressWarnings("ReturnValueIgnored")
355357
public void testTimeToIdle_small() {
356358
CacheBuilder.newBuilder().expireAfterAccess(1, NANOSECONDS).build(identityLoader());
357359
// well, it didn't blow up.
@@ -380,6 +382,7 @@ public void testTimeToIdle_setTwice_duration() {
380382
}
381383
}
382384

385+
@SuppressWarnings("ReturnValueIgnored")
383386
public void testTimeToIdleAndToLive() {
384387
CacheBuilder.newBuilder()
385388
.expireAfterWrite(1, NANOSECONDS)

guava-tests/test/com/google/common/cache/ForwardingLoadingCacheTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void testInvalidateAll() {
9090

9191
public void testSize() {
9292
when(mock.size()).thenReturn(0L);
93-
forward.size();
93+
long unused = forward.size();
9494
}
9595

9696
public void testStats() {

guava/src/com/google/common/cache/Cache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableMap;
1919
import com.google.common.util.concurrent.ExecutionError;
2020
import com.google.common.util.concurrent.UncheckedExecutionException;
21+
import com.google.errorprone.annotations.CheckReturnValue;
2122
import com.google.errorprone.annotations.CompatibleWith;
2223
import com.google.errorprone.annotations.DoNotMock;
2324
import java.util.Map;
@@ -141,6 +142,7 @@ public interface Cache<K, V> {
141142
void invalidateAll();
142143

143144
/** Returns the approximate number of entries in this cache. */
145+
@CheckReturnValue
144146
long size();
145147

146148
/**
@@ -154,6 +156,7 @@ public interface Cache<K, V> {
154156
* all values is returned.
155157
*
156158
*/
159+
@CheckReturnValue
157160
CacheStats stats();
158161

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

169173
/**

guava/src/com/google/common/cache/CacheBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ private CacheBuilder() {}
260260
* <p>Note that while this return type is {@code CacheBuilder<Object, Object>}, type parameters on
261261
* the {@link #build} methods allow you to create a cache of any key and value type desired.
262262
*/
263+
@CheckReturnValue
263264
public static CacheBuilder<Object, Object> newBuilder() {
264265
return new CacheBuilder<>();
265266
}
@@ -270,6 +271,7 @@ public static CacheBuilder<Object, Object> newBuilder() {
270271
* @since 12.0
271272
*/
272273
@GwtIncompatible // To be supported
274+
@CheckReturnValue
273275
public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
274276
return spec.toCacheBuilder().lenientParsing();
275277
}
@@ -282,6 +284,7 @@ public static CacheBuilder<Object, Object> from(CacheBuilderSpec spec) {
282284
* @since 12.0
283285
*/
284286
@GwtIncompatible // To be supported
287+
@CheckReturnValue
285288
public static CacheBuilder<Object, Object> from(String spec) {
286289
return from(CacheBuilderSpec.parse(spec));
287290
}
@@ -945,6 +948,7 @@ Supplier<? extends StatsCounter> getStatsCounterSupplier() {
945948
* @param loader the cache loader used to obtain new values
946949
* @return a cache having the requested features
947950
*/
951+
@CheckReturnValue
948952
public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
949953
CacheLoader<? super K1, V1> loader) {
950954
checkWeightWithWeigher();
@@ -963,6 +967,7 @@ public <K1 extends K, V1 extends V> LoadingCache<K1, V1> build(
963967
* @return a cache having the requested features
964968
* @since 11.0
965969
*/
970+
@CheckReturnValue
966971
public <K1 extends K, V1 extends V> Cache<K1, V1> build() {
967972
checkWeightWithWeigher();
968973
checkNonLoadingCache();

guava/src/com/google/common/cache/CacheLoader.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.util.concurrent.Futures;
2424
import com.google.common.util.concurrent.ListenableFuture;
2525
import com.google.common.util.concurrent.ListenableFutureTask;
26+
import com.google.errorprone.annotations.CheckReturnValue;
2627
import java.io.Serializable;
2728
import java.util.Map;
2829
import java.util.concurrent.Callable;
@@ -135,6 +136,7 @@ public Map<K, V> loadAll(Iterable<? extends K> keys) throws Exception {
135136
* @param function the function to be used for loading values; must never return {@code null}
136137
* @return a cache loader that loads values by passing each key to {@code function}
137138
*/
139+
@CheckReturnValue
138140
public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
139141
return new FunctionToCacheLoader<>(function);
140142
}
@@ -148,6 +150,7 @@ public static <K, V> CacheLoader<K, V> from(Function<K, V> function) {
148150
* @return a cache loader that loads values by calling {@link Supplier#get}, irrespective of the
149151
* key
150152
*/
153+
@CheckReturnValue
151154
public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) {
152155
return new SupplierToCacheLoader<V>(supplier);
153156
}
@@ -177,6 +180,7 @@ public V load(K key) {
177180
*
178181
* @since 17.0
179182
*/
183+
@CheckReturnValue
180184
@GwtIncompatible // Executor + Futures
181185
public static <K, V> CacheLoader<K, V> asyncReloading(
182186
final CacheLoader<K, V> loader, final Executor executor) {

0 commit comments

Comments
 (0)