|
14 | 14 |
|
15 | 15 | package com.google.common.cache; |
16 | 16 |
|
| 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 | + |
17 | 25 | import com.google.common.annotations.GwtCompatible; |
18 | 26 | import com.google.common.collect.ImmutableMap; |
19 | 27 | import com.google.common.util.concurrent.ExecutionError; |
20 | 28 | import com.google.common.util.concurrent.UncheckedExecutionException; |
21 | 29 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
22 | 30 | import com.google.errorprone.annotations.CompatibleWith; |
23 | 31 | 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; |
29 | 32 |
|
30 | 33 | /** |
31 | 34 | * A semi-persistent mapping from keys to values. Cache entries are manually added using {@link |
@@ -180,4 +183,23 @@ public interface Cache<K, V> { |
180 | 183 | * performed -- if any -- is implementation-dependent. |
181 | 184 | */ |
182 | 185 | 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); |
183 | 205 | } |
0 commit comments