fix(storage): make computeIfAbsent atomic (#1699)

This commit is contained in:
Jason van Zyl authored and GitHub committed 2026-07-05 09:32:43 -05:00
1 parent f8e8984c16
commit 71ee94d8b0
2 files changed
+89

No files matched your search

@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
/**
* A mutable {@link Map} facade over {@link StorageBackend}.
@@ -55,6 +56,24 @@ public class StorageBackedMap<V>
return storage.get(stringKey).isPresent();
}
@Override
public synchronized V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction)
{
Objects.requireNonNull(key, "key is null");
Objects.requireNonNull(mappingFunction, "mappingFunction is null");
V value = get(key);
if (value != null) {
return value;
}
V newValue = mappingFunction.apply(key);
if (newValue != null) {
storage.put(key, newValue);
}
return newValue;
}
@Override
public V remove(Object key)
{