org.infinispan.commons.util.CollectionFactory Maven / Gradle / Ivy
package org.infinispan.commons.util;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.infinispan.commons.equivalence.Equivalence;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
/**
* A factory for ConcurrentMaps.
*
* @author Manik Surtani
* @author Galder Zamarreño
* @since 5.1
*/
public class CollectionFactory {
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
public static int computeCapacity(int expectedSize) {
return computeCapacity(expectedSize, DEFAULT_LOAD_FACTOR);
}
public static int computeCapacity(int expectedSize, float loadFactor) {
return (int) (expectedSize / loadFactor + 1.0f);
}
public static ConcurrentMap makeConcurrentMap() {
return new ConcurrentHashMap<>();
}
public static ConcurrentMap makeConcurrentMap(int initCapacity) {
return new ConcurrentHashMap<>(initCapacity);
}
public static ConcurrentMap makeConcurrentMap(int initCapacity, int concurrencyLevel) {
return new ConcurrentHashMap<>(initCapacity, 0.75f, concurrencyLevel);
}
public static ConcurrentMap makeConcurrentParallelMap(int initCapacity, int concurrencyLevel) {
return new ConcurrentHashMap<>(initCapacity);
}
public static ConcurrentMap makeConcurrentMap(int initCapacity, float loadFactor, int concurrencyLevel) {
return new ConcurrentHashMap<>(initCapacity, loadFactor, concurrencyLevel);
}
public static Map makeMap() {
return new HashMap<>();
}
public static Map makeMap(int initialCapacity) {
return new HashMap<>(initialCapacity);
}
public static Map makeMap(Map extends K, ? extends V> entries) {
return new HashMap<>(entries);
}
public static Map makeLinkedMap(int initialCapacity, float loadFactor, boolean accessOrder) {
return new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
}
public static Set makeSet() {
return new HashSet<>();
}
public static Set makeSet(int initialCapacity) {
return new HashSet<>(initialCapacity);
}
/**
* @deprecated Since 9.0, please use {@link #makeConcurrentMap()} instead.
*/
@Deprecated
public static ConcurrentMap makeConcurrentMap(
Equivalence super K> keyEq, Equivalence super V> valueEq) {
return makeConcurrentMap();
}
/**
* @deprecated Since 9.0, please use {@link #makeConcurrentMap(int)} instead.
*/
@Deprecated
public static ConcurrentMap makeConcurrentMap(
int initCapacity, Equivalence super K> keyEq, Equivalence super V> valueEq) {
return makeConcurrentMap(initCapacity);
}
/**
* @deprecated Since 9.0, please use {@link #makeConcurrentMap(int, int)} instead.
*/
@Deprecated
public static ConcurrentMap makeConcurrentMap(
int initCapacity, int concurrencyLevel, Equivalence super K> keyEq, Equivalence super V> valueEq) {
return makeConcurrentMap(initCapacity, concurrencyLevel);
}
/**
* @deprecated Since 9.0, please use {@link #makeConcurrentParallelMap(int, int)} instead.
*/
@Deprecated
public static ConcurrentMap makeConcurrentParallelMap(
int initCapacity, int concurrencyLevel, Equivalence super K> keyEq, Equivalence super V> valueEq) {
return makeConcurrentParallelMap(initCapacity, concurrencyLevel);
}
/**
* @deprecated Since 9.0, please use {@link #makeConcurrentMap(int, float, int)} instead.
*/
@Deprecated
public static ConcurrentMap makeConcurrentMap(
int initCapacity, float loadFactor, int concurrencyLevel,
Equivalence super K> keyEq, Equivalence super V> valueEq) {
return makeConcurrentMap(initCapacity, loadFactor, concurrencyLevel);
}
public static ConcurrentMap makeBoundedConcurrentMap(int maxSize) {
Cache cache = Caffeine.newBuilder().maximumSize(maxSize).build();
return cache.asMap();
}
/**
* @deprecated Since 9.0, please use {@link #makeMap()} instead.
*/
@Deprecated
public static Map makeMap(
Equivalence super K> keyEq, Equivalence super V> valueEq) {
return new HashMap<>();
}
/**
* @deprecated Since 9.0, please use {@link #makeMap(int)} instead.
*/
@Deprecated
public static Map makeMap(
int initialCapacity, Equivalence super K> keyEq, Equivalence super V> valueEq) {
return new HashMap<>(initialCapacity);
}
/**
* @deprecated Since 9.0, please use {@link #makeMap(Map)} instead.
*/
@Deprecated
public static Map makeMap(
Map extends K, ? extends V> entries, Equivalence super K> keyEq, Equivalence super V> valueEq) {
return new HashMap<>(entries);
}
/**
* @deprecated Since 9.0, please use {@link #makeSet(int)} instead.
*/
@Deprecated
public static Map makeLinkedMap(int initialCapacity,
float loadFactor, boolean accessOrder,
Equivalence super K> keyEq, Equivalence super V> valueEq) {
return new LinkedHashMap<>(initialCapacity, loadFactor, accessOrder);
}
/**
* @deprecated Since 9.0, please use {@link #makeSet()} instead.
*/
@Deprecated
public static Set makeSet(Equivalence super T> entryEq) {
return new HashSet<>();
}
/**
* @deprecated Since 9.0, please use {@link #makeSet(int)} instead.
*/
@Deprecated
public static Set makeSet(int initialCapacity, Equivalence super T> entryEq) {
return new HashSet<>(initialCapacity);
}
/**
* Create a Set backed by the specified array.
*
* @param entries the array by which the list will be backed
* @param type of elements
* @return a set view of the specified array
*/
@SafeVarargs
public static Set makeSet(T... entries) {
return new HashSet<>(Arrays.asList(entries));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy