PyUtil: use computeIfAbsent (in particular to avoid optional is compared with null

This commit is contained in:
Tagir Valeev
2018-10-08 15:12:00 +07:00
parent 466eb27523
commit f5b5e6ff55
@@ -814,11 +814,7 @@ public class PyUtil {
// Don't use ConcurrentHashMap#computeIfAbsent(), it blocks if the function tries to update the cache recursively for the same key
// during computation. We can accept here that some values will be computed several times due to non-atomic updates.
final Optional<P> wrappedParam = Optional.ofNullable(param);
Optional<T> value = cache.get(wrappedParam);
if (value == null) {
value = Optional.ofNullable(f.fun(param));
cache.put(wrappedParam, value);
}
Optional<T> value = cache.computeIfAbsent(wrappedParam, k -> Optional.ofNullable(f.fun(param)));
return value.orElse(null);
}