完善文档注释。

feature/security
ZhouXY108 2023-10-30 09:09:32 +08:00
parent 94d34faffd
commit ce62bdcdc6
2 changed files with 35 additions and 4 deletions

View File

@ -22,9 +22,21 @@ import java.util.function.Function;
import javax.annotation.concurrent.ThreadSafe;
import xyz.zhouxy.plusone.commons.base.JRE;
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
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
@ -102,6 +114,8 @@ public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
/** {@inheritDoc} */
@Override
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);
}
}

View File

@ -21,12 +21,28 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
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: Dubboissues#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,
final Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(map, "map");
return JRE.isJava8()
? computeIfAbsentForJava8(map, 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,
final Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(key);
Objects.requireNonNull(mappingFunction);
V v = map.get(key);
if (null == v) {