Fix bug.
parent
6c89c4be14
commit
b178be9a6f
|
@ -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,24 +24,29 @@ 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) {
|
||||||
Objects.requireNonNull(mappingFunction);
|
|
||||||
V v = map.get(key);
|
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) {
|
if (null == v) {
|
||||||
v = mappingFunction.apply(key);
|
return null;
|
||||||
if (null == v) {
|
}
|
||||||
return null;
|
final V res = map.putIfAbsent(key, v);
|
||||||
}
|
if (null != res) {
|
||||||
final V res = map.putIfAbsent(key, v);
|
return res;
|
||||||
if (null != res) {
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return v;
|
|
||||||
} else {
|
|
||||||
return map.computeIfAbsent(key, mappingFunction);
|
|
||||||
}
|
}
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConcurrentHashMapUtil() {
|
private ConcurrentHashMapUtil() {
|
||||||
|
|
Loading…
Reference in New Issue