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} */
|
||||
@Override
|
||||
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
|
||||
return ConcurrentHashMapUtil.computeIfAbsent(this, key, mappingFunction);
|
||||
return ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,24 +24,29 @@ 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()) {
|
||||
Objects.requireNonNull(mappingFunction);
|
||||
V v = map.get(key);
|
||||
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) {
|
||||
v = mappingFunction.apply(key);
|
||||
if (null == v) {
|
||||
v = mappingFunction.apply(key);
|
||||
if (null == v) {
|
||||
return null;
|
||||
}
|
||||
final V res = map.putIfAbsent(key, v);
|
||||
if (null != res) {
|
||||
return res;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
final V res = map.putIfAbsent(key, v);
|
||||
if (null != res) {
|
||||
return res;
|
||||
}
|
||||
return v;
|
||||
} else {
|
||||
return map.computeIfAbsent(key, mappingFunction);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
private ConcurrentHashMapUtil() {
|
||||
|
|
Loading…
Reference in New Issue