This commit is contained in:
Looly
2023-03-31 22:09:59 +08:00
parent 8dab3881f7
commit 3c33ab7fc2
3 changed files with 48 additions and 39 deletions

View File

@@ -12,6 +12,7 @@
package cn.hutool.core.map; package cn.hutool.core.map;
import cn.hutool.core.array.ArrayUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.collection.iter.ArrayIter; import cn.hutool.core.collection.iter.ArrayIter;
@@ -19,23 +20,11 @@ import cn.hutool.core.collection.iter.IterUtil;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.reflect.ConstructorUtil; import cn.hutool.core.reflect.ConstructorUtil;
import cn.hutool.core.text.StrUtil; import cn.hutool.core.text.StrUtil;
import cn.hutool.core.array.ArrayUtil; import cn.hutool.core.util.JdkUtil;
import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjUtil;
import java.util.AbstractMap; import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
@@ -1282,6 +1271,7 @@ public class MapUtil extends MapGetUtil {
* 方法来自Dubbo解决使用ConcurrentHashMap.computeIfAbsent导致的死循环问题。issues#2349<br> * 方法来自Dubbo解决使用ConcurrentHashMap.computeIfAbsent导致的死循环问题。issues#2349<br>
* A temporary workaround for Java 8 specific performance issue JDK-8161372 .<br> * A temporary workaround for Java 8 specific performance issue JDK-8161372 .<br>
* This class should be removed once we drop Java 8 support. * This class should be removed once we drop Java 8 support.
* 参考https://github.com/apache/dubbo/blob/3.2/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConcurrentHashMapUtils.java
* *
* @param <K> 键类型 * @param <K> 键类型
* @param <V> 值类型 * @param <V> 值类型
@@ -1292,11 +1282,15 @@ public class MapUtil extends MapGetUtil {
* @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a> * @see <a href="https://bugs.openjdk.java.net/browse/JDK-8161372">https://bugs.openjdk.java.net/browse/JDK-8161372</a>
*/ */
public static <K, V> V computeIfAbsent(final Map<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) { public static <K, V> V computeIfAbsent(final Map<K, V> map, final K key, final Function<? super K, ? extends V> mappingFunction) {
V value = map.get(key); if (JdkUtil.IS_JDK8) {
if (null == value) { V value = map.get(key);
map.putIfAbsent(key, mappingFunction.apply(key)); if (null == value) {
value = map.get(key); //map.putIfAbsent(key, mappingFunction.apply(key));
value = map.computeIfAbsent(key, mappingFunction);
}
return value;
} else {
return map.computeIfAbsent(key, mappingFunction);
} }
return value;
} }
} }

View File

@@ -57,18 +57,19 @@ public class JdkUtil {
* @return JVM名称 * @return JVM名称
*/ */
private static String _getJvmName() { private static String _getJvmName() {
return System.getProperty("java.vm.name"); return SystemUtil.getQuietly("java.vm.name");
} }
/** /**
* 根据{@code java.specification.version}属性值,获取版本号 * 根据{@code java.specification.version}属性值,获取版本号<br>
* 默认8
* *
* @return 版本号 * @return 版本号
*/ */
private static int _getJvmVersion() { private static int _getJvmVersion() {
int jvmVersion = -1; int jvmVersion = 8;
String javaSpecVer = System.getProperty("java.specification.version"); String javaSpecVer = SystemUtil.getQuietly("java.specification.version");
if (StrUtil.isNotBlank(javaSpecVer)) { if (StrUtil.isNotBlank(javaSpecVer)) {
if (javaSpecVer.startsWith("1.")) { if (javaSpecVer.startsWith("1.")) {
javaSpecVer = javaSpecVer.substring(2); javaSpecVer = javaSpecVer.substring(2);

View File

@@ -30,7 +30,9 @@ import java.util.Properties;
*/ */
public class SystemUtil { public class SystemUtil {
/** Hutool自定义系统属性是否解析日期字符串采用严格模式 */ /**
* Hutool自定义系统属性是否解析日期字符串采用严格模式
*/
public static String HUTOOL_DATE_LENIENT = "hutool.date.lenient"; public static String HUTOOL_DATE_LENIENT = "hutool.date.lenient";
/** /**
@@ -43,7 +45,31 @@ public class SystemUtil {
* @see System#getenv(String) * @see System#getenv(String)
*/ */
public static String get(final String name, final String defaultValue) { public static String get(final String name, final String defaultValue) {
return ObjUtil.defaultIfNull(get(name, false), defaultValue); return ObjUtil.defaultIfNull(get(name), defaultValue);
}
/**
* 获得System属性
*
* @param key 键
* @return 属性值
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(final String key) {
return get(key, false);
}
/**
* 获得System属性忽略无权限问题
*
* @param key 键
* @return 属性值
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String getQuietly(final String key) {
return get(key, true);
} }
/** /**
@@ -62,7 +88,7 @@ public class SystemUtil {
} catch (final SecurityException e) { } catch (final SecurityException e) {
if (false == quiet) { if (false == quiet) {
Console.error("Caught a SecurityException reading the system property '{}'; " + Console.error("Caught a SecurityException reading the system property '{}'; " +
"the SystemUtil property value will default to null.", name); "the SystemUtil property value will default to null.", name);
} }
} }
@@ -72,7 +98,7 @@ public class SystemUtil {
} catch (final SecurityException e) { } catch (final SecurityException e) {
if (false == quiet) { if (false == quiet) {
Console.error("Caught a SecurityException reading the system env '{}'; " + Console.error("Caught a SecurityException reading the system env '{}'; " +
"the SystemUtil env value will default to null.", name); "the SystemUtil env value will default to null.", name);
} }
} }
} }
@@ -80,18 +106,6 @@ public class SystemUtil {
return value; return value;
} }
/**
* 获得System属性
*
* @param key 键
* @return 属性值
* @see System#getProperty(String)
* @see System#getenv(String)
*/
public static String get(final String key) {
return get(key, null);
}
/** /**
* 获得boolean类型值 * 获得boolean类型值
* *