diff --git a/src/main/java/xyz/zhouxy/plusone/commons/base/JRE.java b/src/main/java/xyz/zhouxy/plusone/commons/base/JRE.java deleted file mode 100644 index 957d309..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/base/JRE.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2023-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.base; - -import xyz.zhouxy.plusone.commons.util.StringTools; - -/** - * JRE version - */ -public class JRE { - - private static final int JAVA_8 = 8; - - public static final int CURRENT_VERSION = getJre(); - - public static boolean isJava8() { - return CURRENT_VERSION == JAVA_8; - } - - private static int getJre() { - String version = System.getProperty("java.version"); - boolean isNotBlank = StringTools.isNotBlank(version); - if (isNotBlank && version.startsWith("1.8")) { - return JAVA_8; - } - // if JRE version is 9 or above, we can get version from - // java.lang.Runtime.version() - try { - return getMajorVersion(version); - } catch (Exception e) { - // assuming that JRE version is 8. - } - // default java 8 - return JAVA_8; - } - - private static int getMajorVersion(String version) { - if (version.startsWith("1.")) { - return Integer.parseInt(version.substring(2, 3)); - } else { - int dotIndex = version.indexOf("."); - return (dotIndex != -1) ? Integer.parseInt(version.substring(0, dotIndex)) : Integer.parseInt(version); - } - } - - private JRE() { - throw new IllegalStateException("Utility class"); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java index 93b4242..04a974b 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java @@ -23,7 +23,6 @@ import java.util.Map.Entry; import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; import java.util.function.Function; @@ -33,7 +32,6 @@ import javax.annotation.Nullable; import com.google.common.annotations.Beta; import xyz.zhouxy.plusone.commons.util.AssertTools; -import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools; /** * AbstractMapWrapper @@ -159,12 +157,7 @@ public abstract class AbstractMapWrapper) this.map, key, func); - } else { - return this.map.computeIfAbsent(key, func); - } + return this.map.computeIfAbsent(key, func); } public final Map exportMap() { diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java deleted file mode 100644 index 0b60882..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright 2023-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.collection; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; - -import javax.annotation.concurrent.ThreadSafe; - -import xyz.zhouxy.plusone.commons.base.JRE; -import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools; - -/** - * SafeConcurrentHashMap - * - *

- * Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug, - * 使用 Java 8 时,可使用这个类进行替换。 - * - * @author ZhouXY - * @since 1.0 - * @see ConcurrentHashMap - * @see ConcurrentHashMapTools#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function) - */ -@ThreadSafe -public class SafeConcurrentHashMap extends ConcurrentHashMap { - - private static final long serialVersionUID = 4352954948768449595L; - - /** - * Creates a new, empty map with the default initial table size (16). - */ - public SafeConcurrentHashMap() { - } - - /** - * Creates a new, empty map with an initial table size - * accommodating the specified number of elements without the need - * to dynamically resize. - * - * @param initialCapacity The implementation performs internal - * sizing to accommodate this many elements. - * @throws IllegalArgumentException if the initial capacity of - * elements is negative - */ - public SafeConcurrentHashMap(int initialCapacity) { - super(initialCapacity); - } - - /** - * Creates a new map with the same mappings as the given map. - * - * @param m the map - */ - public SafeConcurrentHashMap(Map m) { - super(m); - } - - /** - * Creates a new, empty map with an initial table size based on - * the given number of elements ({@code initialCapacity}) and - * initial table density ({@code loadFactor}). - * - * @param initialCapacity the initial capacity. The implementation - * performs internal sizing to accommodate this many elements, - * given the specified load factor. - * @param loadFactor the load factor (table density) for - * establishing the initial table size - * @throws IllegalArgumentException if the initial capacity of - * elements is negative or the load factor is nonpositive - * @since 1.6 - */ - public SafeConcurrentHashMap(int initialCapacity, float loadFactor) { - super(initialCapacity, loadFactor); - } - - /** - * Creates a new, empty map with an initial table size based on - * the given number of elements ({@code initialCapacity}), table - * density ({@code loadFactor}), and number of concurrently - * updating threads ({@code concurrencyLevel}). - * - * @param initialCapacity the initial capacity. The implementation - * performs internal sizing to accommodate this many elements, - * given the specified load factor. - * @param loadFactor the load factor (table density) for - * establishing the initial table size - * @param concurrencyLevel the estimated number of concurrently - * updating threads. The implementation may use this value as - * a sizing hint. - * @throws IllegalArgumentException if the initial capacity is - * negative or the load factor or concurrencyLevel are - * nonpositive - */ - public SafeConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) { - super(initialCapacity, loadFactor, concurrencyLevel); - } - - /** {@inheritDoc} */ - @Override - public V computeIfAbsent(K key, Function mappingFunction) { - return JRE.isJava8() - ? ConcurrentHashMapTools.computeIfAbsentForJava8(this, key, mappingFunction) - : super.computeIfAbsent(key, mappingFunction); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapTools.java deleted file mode 100644 index c36ecf0..0000000 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapTools.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright 2023-2024 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.commons.util; - -import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; - -import xyz.zhouxy.plusone.commons.base.JRE; -import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; - -/** - * ConcurrentHashMapTools - * - *

- * Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug, - * 可使用这个工具类的 {@link computeIfAbsentForJava8} 进行替换。 - * - *

- * NOTE: 方法来自Dubbo,见:issues#2349 - * - * @author ZhouXY - * @since 1.0 - * @see ConcurrentHashMap - * @see SafeConcurrentHashMap - */ -public class ConcurrentHashMapTools { - - public static V computeIfAbsent( - ConcurrentHashMap map, final K key, // NOSONAR - final Function mappingFunction) { - Objects.requireNonNull(map, "map"); - return JRE.isJava8() - ? computeIfAbsentForJava8(map, key, mappingFunction) - : map.computeIfAbsent(key, mappingFunction); - } - - public static V computeIfAbsentForJava8( - ConcurrentHashMap map, final K key, // NOSONAR - final Function mappingFunction) { - Objects.requireNonNull(key); - Objects.requireNonNull(mappingFunction); - V v = map.get(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 v; - } - - private ConcurrentHashMapTools() { - throw new IllegalStateException("Utility class"); - } -}