All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.polaris.core.map.Maps Maven / Gradle / Ivy
package io.polaris.core.map;
import java.lang.ref.Reference;
import java.util.AbstractMap;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Supplier;
import io.polaris.core.map.reference.ReferenceType;
import io.polaris.core.map.reference.ValueReference;
import io.polaris.core.reflect.Reflects;
/**
* @author Qt
* @since 1.8
*/
public class Maps {
/**
* 创建指定类型的Map
*
* @param mapType
* @param
* @param
* @return
*/
public static Map createMap(Class> mapType) {
if (null == mapType || mapType.isAssignableFrom(AbstractMap.class)) {
return new HashMap<>();
} else {
try {
return (Map) Reflects.newInstance(mapType);
} catch (ReflectiveOperationException e) {
return new HashMap<>();
}
}
}
/**
* 反转键值生成新的Map对象
*
* @param map
* @param
* @param
* @return
*/
public static Map inverse(Map map) {
Map result = createMap(map.getClass());
map.forEach((key, value) -> result.put(value, key));
return result;
}
public static FluentMap newFluentMap(Map map) {
return new FluentMap(map);
}
public static Map newLimitCapacityMap(int maxCapacity) {
return new LimitedLinkedHashMap(maxCapacity);
}
public static Map newLimitCapacityMap(int maxCapacity, boolean accessOrder) {
return new LimitedLinkedHashMap(maxCapacity, accessOrder);
}
public static , V> EnumMap newEnumMap(Class type) {
return new EnumMap(type);
}
public static LinkedHashMap newLinkedHashMap() {
return new LinkedHashMap();
}
public static HashMap newHashMap() {
return new HashMap();
}
public static , V> TreeMap newTreeMap() {
return new TreeMap();
}
public static Map newLowerCaseHashMap() {
return new CaseInsensitiveMap<>(new HashMap<>(), false);
}
public static Map newUpperCaseHashMap() {
return new CaseInsensitiveMap<>(new HashMap<>(), true);
}
public static Map newLowerCaseLinkedHashMap() {
return new CaseInsensitiveMap<>(new LinkedHashMap<>(), false);
}
public static Map newUpperCaseLinkedHashMap() {
return new CaseInsensitiveMap<>(new LinkedHashMap<>(), true);
}
public static Map newSoftHashMap() {
return new SoftHashMap<>();
}
public static Map newWeakHashMap() {
return new WeakHashMap<>();
}
public static Map newSoftKeyHashMap() {
return new SoftKeyHashMap<>();
}
public static Map newWeakKeyHashMap() {
return new WeakKeyHashMap<>();
}
public static Map newSoftValueHashMap() {
return new SoftValueHashMap<>();
}
public static Map newWeakValueHashMap() {
return new WeakValueHashMap<>();
}
public static Map newSoftMap(Supplier, ValueReference, V>>> raw) {
return new ReferenceMap<>(raw, ReferenceType.SOFT);
}
public static Map newWeakMap(Supplier, ValueReference, V>>> raw) {
return new ReferenceMap<>(raw, ReferenceType.WEAK);
}
public static Map newSoftKeyMap(Supplier, V>> raw) {
return new KeyReferenceMap<>(raw, ReferenceType.SOFT);
}
public static Map newWeakKeyMap(Supplier, V>> raw) {
return new KeyReferenceMap<>(raw, ReferenceType.WEAK);
}
public static Map newSoftValueMap(Supplier>> raw) {
return new ValueReferenceMap<>(raw, ReferenceType.SOFT);
}
public static Map newWeakValueMap(Supplier>> raw) {
return new ValueReferenceMap<>(raw, ReferenceType.WEAK);
}
public static Map newSoftMap(Map, ValueReference, V>> raw) {
return new ReferenceMap<>(raw, ReferenceType.SOFT);
}
public static Map newWeakMap(Map, ValueReference, V>> raw) {
return new ReferenceMap<>(raw, ReferenceType.WEAK);
}
public static Map newSoftKeyMap(Map, V> raw) {
return new KeyReferenceMap<>(raw, ReferenceType.SOFT);
}
public static Map newWeakKeyMap(Map, V> raw) {
return new KeyReferenceMap<>(raw, ReferenceType.WEAK);
}
public static Map newSoftValueMap(Map> raw) {
return new ValueReferenceMap<>(raw, ReferenceType.SOFT);
}
public static Map newWeakValueMap(Map> raw) {
return new ValueReferenceMap<>(raw, ReferenceType.WEAK);
}
}