forked from plusone/plusone-commons
完善文档注释。
parent
94d34faffd
commit
ce62bdcdc6
|
@ -22,9 +22,21 @@ import java.util.function.Function;
|
||||||
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||||
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
|
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil;
|
||||||
|
|
||||||
// TODO 添加文档注释
|
/**
|
||||||
|
* SafeConcurrentHashMap
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||||
|
* 使用 Java 8 时,可使用这个类进行替换。
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
* @see ConcurrentHashMap
|
||||||
|
* @see ConcurrentHashMapUtil#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function)
|
||||||
|
*/
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
|
||||||
|
|
||||||
|
@ -102,6 +114,8 @@ 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.computeIfAbsentForJava8(this, key, mappingFunction);
|
return JRE.isJava8()
|
||||||
|
? ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction)
|
||||||
|
: super.computeIfAbsent(key, mappingFunction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,12 +21,28 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.commons.base.JRE;
|
import xyz.zhouxy.plusone.commons.base.JRE;
|
||||||
|
import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap;
|
||||||
|
|
||||||
public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
/**
|
||||||
|
* ConcurrentHashMapUtil
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug,
|
||||||
|
* 可使用这个工具类的 {@link computeIfAbsentForJava8} 进行替换。
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* <b>NOTE: 方法来自Dubbo,见:issues#2349</b>
|
||||||
|
*
|
||||||
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
|
* @since 1.0
|
||||||
|
* @see ConcurrentHashMap
|
||||||
|
* @see SafeConcurrentHashMap
|
||||||
|
*/
|
||||||
|
public class ConcurrentHashMapUtil {
|
||||||
|
|
||||||
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
|
public static <K, V> V computeIfAbsent(ConcurrentHashMap<K, V> map, final K key,
|
||||||
final Function<? super K, ? extends V> mappingFunction) {
|
final Function<? super K, ? extends V> mappingFunction) {
|
||||||
|
Objects.requireNonNull(map, "map");
|
||||||
return JRE.isJava8()
|
return JRE.isJava8()
|
||||||
? computeIfAbsentForJava8(map, key, mappingFunction)
|
? computeIfAbsentForJava8(map, key, mappingFunction)
|
||||||
: map.computeIfAbsent(key, mappingFunction);
|
: map.computeIfAbsent(key, mappingFunction);
|
||||||
|
@ -34,6 +50,7 @@ public class ConcurrentHashMapUtil { // TODO 添加文档注释
|
||||||
|
|
||||||
public static <K, V> V computeIfAbsentForJava8(ConcurrentHashMap<K, V> map, final K key,
|
public static <K, V> V computeIfAbsentForJava8(ConcurrentHashMap<K, V> map, final K key,
|
||||||
final Function<? super K, ? extends V> mappingFunction) {
|
final Function<? super K, ? extends V> mappingFunction) {
|
||||||
|
Objects.requireNonNull(key);
|
||||||
Objects.requireNonNull(mappingFunction);
|
Objects.requireNonNull(mappingFunction);
|
||||||
V v = map.get(key);
|
V v = map.get(key);
|
||||||
if (null == v) {
|
if (null == v) {
|
||||||
|
|
Loading…
Reference in New Issue