mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 14:30:28 -05:00
fix(storage): make computeIfAbsent atomic (#1699)
This commit is contained in:
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)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user