Files
plusone-commons/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java

122 lines
4.7 KiB
Java
Raw Normal View History

/*
2024-10-12 00:27:28 +08:00
* Copyright 2022-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;
2023-10-30 09:09:32 +08:00
import xyz.zhouxy.plusone.commons.base.JRE;
2024-04-03 16:20:41 +08:00
import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools;
2023-10-30 09:09:32 +08:00
/**
* 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
2024-04-03 16:20:41 +08:00
* @see ConcurrentHashMapTools#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function)
2023-10-30 09:09:32 +08:00
*/
@ThreadSafe
public class SafeConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> {
2023-06-27 00:47:39 +08:00
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<? extends K, ? extends V> 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);
}
2023-06-27 00:47:39 +08:00
/** {@inheritDoc} */
@Override
public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
2023-10-30 09:09:32 +08:00
return JRE.isJava8()
2024-04-03 16:20:41 +08:00
? ConcurrentHashMapTools.computeIfAbsentForJava8(this, key, mappingFunction)
2023-10-30 09:09:32 +08:00
: super.computeIfAbsent(key, mappingFunction);
}
}