修复 SafeConcurrentHashMap 的 computeIfAbsent 与 ConcurrentHashMapUtil 的 computeIfAbsent 相互调用造成无限递归的问题

见 issue #6
feature/net-util
ZhouXY108 2023-09-09 13:58:26 +08:00
commit febfa73f8a
2 changed files with 21 additions and 16 deletions

View File

@ -102,6 +102,6 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
/** {@inheritDoc} */
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
return ConcurrentHashMapUtil.computeIfAbsent(this, key, mappingFunction);
return ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction);
}
}

View File

@ -24,8 +24,16 @@ import xyz.zhouxy.plusone.commons.base.JRE;
public class ConcurrentHashMapUtil { // TODO 添加文档注释
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) {
if (JRE.isJava8()) {
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
final Function<? super K, ? extends V> mappingFunction) {
return JRE.isJava8()
? computeIfAbsentForJava8(map, key, mappingFunction)
: map.computeIfAbsent(key, mappingFunction);
}
public static <K, V> V computeIfAbsentForJava8(ConcurrentHashMap<K, V> map, final K key,
final Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v = map.get(key);
if (null == v) {
@ -39,9 +47,6 @@ public class ConcurrentHashMapUtil { // TODO 添加文档注释
}
}
return v;
} else {
return map.computeIfAbsent(key, mappingFunction);
}
}
private ConcurrentHashMapUtil() {