Skip to content

Commit 2b1c39e

Browse files
committed
Add Javadoc documentation to Cache.compute method
1 parent 34f3253 commit 2b1c39e

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414

1515
package com.google.common.cache;
1616

17+
import java.util.Map;
18+
import java.util.concurrent.Callable;
19+
import java.util.concurrent.ConcurrentMap;
20+
import java.util.concurrent.ExecutionException;
21+
import java.util.function.BiFunction;
22+
23+
import org.jspecify.annotations.Nullable;
24+
1725
import com.google.common.annotations.GwtCompatible;
1826
import com.google.common.collect.ImmutableMap;
1927
import com.google.common.util.concurrent.ExecutionError;
2028
import com.google.common.util.concurrent.UncheckedExecutionException;
2129
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2230
import com.google.errorprone.annotations.CompatibleWith;
2331
import com.google.errorprone.annotations.DoNotMock;
24-
import java.util.Map;
25-
import java.util.concurrent.Callable;
26-
import java.util.concurrent.ConcurrentMap;
27-
import java.util.concurrent.ExecutionException;
28-
import org.jspecify.annotations.Nullable;
2932

3033
/**
3134
* A semi-persistent mapping from keys to values. Cache entries are manually added using {@link
@@ -180,4 +183,23 @@ public interface Cache<K, V> {
180183
* performed -- if any -- is implementation-dependent.
181184
*/
182185
void cleanUp();
186+
187+
/**
188+
* Attempts to compute a mapping for the specified key and its current mapped value (or {@code
189+
* null} if there is no current mapping). The entire method invocation is performed atomically.
190+
* Some attempted update operations on this cache by other threads may be blocked while
191+
* computation is in progress, so the computation should be short and simple, and must not
192+
* attempt to update any other mappings of this cache.
193+
*
194+
* <p>The {@code remappingFunction} may throw an (unchecked) exception. The exception is
195+
* rethrown, and the current mapping is left unchanged.
196+
*
197+
* @param key key with which the specified value is to be associated
198+
* @param remappingFunction the function to compute a value
199+
* @return the new value associated with the specified key, or {@code null} if the mapping was
200+
* removed
201+
* @since 23.0
202+
*/
203+
@CanIgnoreReturnValue
204+
@Nullable V compute(K key, BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> remappingFunction);
183205
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.concurrent.Callable;
2323
import java.util.concurrent.ConcurrentMap;
2424
import java.util.concurrent.ExecutionException;
25+
import java.util.function.BiFunction;
26+
import java.util.function.Function;
2527
import org.jspecify.annotations.Nullable;
2628

2729
/**
@@ -124,6 +126,26 @@ public void cleanUp() {
124126
delegate().cleanUp();
125127
}
126128

129+
@Override
130+
public @Nullable V compute(K key, BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
131+
return delegate().compute(key, remappingFunction);
132+
}
133+
134+
@Override
135+
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
136+
return delegate().computeIfAbsent(key, mappingFunction);
137+
}
138+
139+
@Override
140+
public @Nullable V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
141+
return delegate().computeIfPresent(key, remappingFunction);
142+
}
143+
144+
@Override
145+
public @Nullable V merge(K key, V value, BiFunction<? super V, ? super V, ? extends @Nullable V> remappingFunction) {
146+
return delegate().merge(key, value, remappingFunction);
147+
}
148+
127149
/**
128150
* A simplified version of {@link ForwardingCache} where subclasses can pass in an already
129151
* constructed {@link Cache} as the delegate.

0 commit comments

Comments
 (0)