forked from plusone/plusone-commons
修复 SafeConcurrentHashMap 的 computeIfAbsent 与 ConcurrentHashMapUtil 的 computeIfAbsent 相互调用造成无限递归的问题
见 issue #6feature/net-util
commit
febfa73f8a
|
@ -102,6 +102,6 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
@Override
|
@Override
|
||||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||||
return ConcurrentHashMapUtil.computeIfAbsent(this, key, mappingFunction);
|
return ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,16 @@ import xyz.zhouxy.plusone.commons.base.JRE;
|
||||||
|
|
||||||
public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
||||||
|
|
||||||
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) {
|
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
|
||||||
if (JRE.isJava8()) {
|
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);
|
Objects.requireNonNull(mappingFunction);
|
||||||
V v = map.get(key);
|
V v = map.get(key);
|
||||||
if (null == v) {
|
if (null == v) {
|
||||||
|
@ -39,9 +47,6 @@ public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return v;
|
return v;
|
||||||
} else {
|
|
||||||
return map.computeIfAbsent(key, mappingFunction);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConcurrentHashMapUtil() {
|
private ConcurrentHashMapUtil() {
|
||||||
|
|
Loading…
Reference in New Issue