encapsulation of factory's implementation details

This commit is contained in:
Eugene Zhuravlev
2013-11-29 19:16:34 +01:00
parent 75beb19b08
commit dcfb1d80cf
13 changed files with 64 additions and 40 deletions
@@ -49,7 +49,7 @@ public final class IconLoader {
private static boolean USE_DARK_ICONS = UIUtil.isUnderDarcula();
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private static final ConcurrentMap<URL, CachedImageIcon> ourIconsCache = ConcurrentMapFactory.DEFAULT_FACTORY.createMap(100, 0.9f, 2);
private static final ConcurrentMap<URL, CachedImageIcon> ourIconsCache = ConcurrentMapFactory.createMap(100, 0.9f, 2);
/**
* This cache contains mapping between icons and disabled icons.
@@ -13,7 +13,7 @@ import java.util.Map;
* @author peter
*/
public class SharedProcessingContext {
private final Map<Object, Object> myMap = ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
private final Map<Object, Object> myMap = ConcurrentMapFactory.createMap();
public Object get(@NotNull @NonNls final String key) {
return myMap.get(key);
@@ -20,6 +20,6 @@ package com.intellij.util.containers;
*/
public class ConcurrentClassMap<T> extends ClassMap<T> {
public ConcurrentClassMap() {
super(ConcurrentMapFactory.DEFAULT_FACTORY.<Class, T>createMap());
super(ConcurrentMapFactory.<Class, T>createMap());
}
}
@@ -24,6 +24,6 @@ public abstract class ConcurrentFactoryMap<T,V> extends FactoryMap<T,V> {
@Override
protected Map<T, V> createMap() {
return ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
return ConcurrentMapFactory.createMap();
}
}
@@ -28,13 +28,13 @@ public class ConcurrentHashSet<K> implements Set<K> {
private final ConcurrentMap<K, Boolean> map;
public ConcurrentHashSet(int initialCapacity) {
map = ConcurrentMapFactory.DEFAULT_FACTORY.createMap(initialCapacity);
map = ConcurrentMapFactory.createMap(initialCapacity);
}
public ConcurrentHashSet() {
map = ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
map = ConcurrentMapFactory.createMap();
}
public ConcurrentHashSet(@NotNull TObjectHashingStrategy<K> hashingStrategy) {
map = ConcurrentMapFactory.DEFAULT_FACTORY.createMap(hashingStrategy);
map = ConcurrentMapFactory.createMap(hashingStrategy);
}
@Override
@@ -21,60 +21,84 @@ import org.jetbrains.annotations.NotNull;
import java.util.concurrent.ConcurrentMap;
public interface ConcurrentMapFactory {
int DEFAULT_CONCURRENCY_LEVEL = Runtime.getRuntime().availableProcessors();
public abstract class ConcurrentMapFactory {
private static final int DEFAULT_CONCURRENCY_LEVEL = Runtime.getRuntime().availableProcessors();
ConcurrentMapFactory V8_MAP_FACTORY = new ConcurrentMapFactory() {
public <T, V> ConcurrentMap<T, V> createMap() {
private ConcurrentMapFactory() {
}
protected abstract <T, V> ConcurrentMap<T, V> _createMap();
protected abstract <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity);
protected abstract <T, V> ConcurrentMap<T, V> _createMap(TObjectHashingStrategy<T> hashStrategy);
protected abstract <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity, float loadFactor, int concurrencyLevel);
protected abstract <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity, float loadFactor, int concurrencyLevel, TObjectHashingStrategy<T> hashStrategy);
private static final ConcurrentMapFactory V8_MAP_FACTORY = new ConcurrentMapFactory() {
protected <T, V> ConcurrentMap<T, V> _createMap() {
return new ConcurrentHashMap<T,V>();
}
public <T, V> ConcurrentMap<T, V> createMap(int initialCapacity) {
protected <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity) {
return new ConcurrentHashMap<T,V>(initialCapacity);
}
public <T, V> ConcurrentMap<T, V> createMap(TObjectHashingStrategy<T> hashStrategy) {
protected <T, V> ConcurrentMap<T, V> _createMap(TObjectHashingStrategy<T> hashStrategy) {
return new ConcurrentHashMap<T,V>(hashStrategy);
}
public <T, V> ConcurrentMap<T, V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
protected <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
return new ConcurrentHashMap<T,V>(initialCapacity, loadFactor, concurrencyLevel);
}
public <T, V> ConcurrentMap<T, V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel, @NotNull TObjectHashingStrategy<T> hashingStrategy) {
protected <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity, float loadFactor, int concurrencyLevel, @NotNull TObjectHashingStrategy<T> hashingStrategy) {
return new ConcurrentHashMap<T,V>(initialCapacity, loadFactor, concurrencyLevel, hashingStrategy);
}
};
ConcurrentMapFactory PLATFORM_MAP_FACTORY = new ConcurrentMapFactory() {
public <T, V> ConcurrentMap<T, V> createMap() {
return createMap(16, 0.75f, DEFAULT_CONCURRENCY_LEVEL);
private static final ConcurrentMapFactory PLATFORM_MAP_FACTORY = new ConcurrentMapFactory() {
protected <T, V> ConcurrentMap<T, V> _createMap() {
return _createMap(16, 0.75f, DEFAULT_CONCURRENCY_LEVEL);
}
public <T, V> ConcurrentMap<T, V> createMap(int initialCapacity) {
protected <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity) {
return new java.util.concurrent.ConcurrentHashMap<T,V>(initialCapacity);
}
public <T, V> ConcurrentMap<T, V> createMap(TObjectHashingStrategy<T> hashStrategy) {
protected <T, V> ConcurrentMap<T, V> _createMap(TObjectHashingStrategy<T> hashStrategy) {
// ignoring strategy parameter, because it is not supported by this implementation
return createMap();
return _createMap();
}
public <T, V> ConcurrentMap<T, V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
protected <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
return new java.util.concurrent.ConcurrentHashMap<T,V>(initialCapacity, loadFactor, concurrencyLevel);
}
public <T, V> ConcurrentMap<T, V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel, @NotNull TObjectHashingStrategy<T> hashingStrategy) {
protected <T, V> ConcurrentMap<T, V> _createMap(int initialCapacity, float loadFactor, int concurrencyLevel, @NotNull TObjectHashingStrategy<T> hashingStrategy) {
// ignoring strategy parameter, because it is not supported by this implementation
return createMap(initialCapacity, loadFactor, concurrencyLevel);
return _createMap(initialCapacity, loadFactor, concurrencyLevel);
}
};
ConcurrentMapFactory DEFAULT_FACTORY = SystemInfo.isOracleJvm || SystemInfo.isAppleJvm? V8_MAP_FACTORY : PLATFORM_MAP_FACTORY;
private static final ConcurrentMapFactory DEFAULT_FACTORY = SystemInfo.isOracleJvm || SystemInfo.isAppleJvm? V8_MAP_FACTORY : PLATFORM_MAP_FACTORY;
public static <T, V> ConcurrentMap<T,V> createMap() {
return DEFAULT_FACTORY._createMap();
}
public static <T, V> ConcurrentMap<T,V> createMap(TObjectHashingStrategy<T> hashStrategy) {
return DEFAULT_FACTORY._createMap(hashStrategy);
}
public static <T, V> ConcurrentMap<T,V> createMap(int initialCapacity) {
return DEFAULT_FACTORY._createMap(initialCapacity);
}
public static <T, V> ConcurrentMap<T,V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel, TObjectHashingStrategy<T> hashStrategy) {
return DEFAULT_FACTORY._createMap(initialCapacity, loadFactor, concurrencyLevel, hashStrategy);
}
public static <T, V> ConcurrentMap<T,V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
return DEFAULT_FACTORY._createMap(initialCapacity, loadFactor, concurrencyLevel);
}
<T, V> ConcurrentMap<T, V> createMap();
<T, V> ConcurrentMap<T, V> createMap(int initialCapacity);
<T, V> ConcurrentMap<T, V> createMap(TObjectHashingStrategy<T> hashStrategy);
<T, V> ConcurrentMap<T, V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel);
<T, V> ConcurrentMap<T, V> createMap(int initialCapacity, float loadFactor, int concurrencyLevel, TObjectHashingStrategy<T> hashStrategy);
}
@@ -28,7 +28,7 @@ public class ConcurrentMostlySingularMultiMap<K, V> extends MostlySingularMultiM
@NotNull
@Override
protected Map<K, Object> createMap() {
return ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
return ConcurrentMapFactory.createMap();
}
@Override
@@ -25,7 +25,7 @@ import java.util.Map;
public class ConcurrentMultiMap<K,V> extends MultiMap<K,V> {
@Override
protected Map<K, Collection<V>> createMap() {
return ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
return ConcurrentMapFactory.createMap();
}
@Override
@@ -149,7 +149,7 @@ abstract class ConcurrentRefHashMap<K, V> extends AbstractMap<K, V> implements C
int concurrencyLevel,
@NotNull TObjectHashingStrategy<K> hashingStrategy) {
myHashingStrategy = hashingStrategy == THIS ? this : hashingStrategy;
myMap = ConcurrentMapFactory.DEFAULT_FACTORY.<Key<K, V>, V>createMap(initialCapacity, loadFactor, concurrencyLevel, CANONICAL);
myMap = ConcurrentMapFactory.<Key<K, V>, V>createMap(initialCapacity, loadFactor, concurrencyLevel, CANONICAL);
}
@Override
@@ -35,18 +35,18 @@ abstract class ConcurrentRefValueHashMap<K, V> implements ConcurrentMap<K, V> {
}
public ConcurrentRefValueHashMap() {
myMap = ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
myMap = ConcurrentMapFactory.createMap();
}
public ConcurrentRefValueHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) {
myMap = ConcurrentMapFactory.DEFAULT_FACTORY.createMap(initialCapacity, loadFactor, concurrencyLevel);
myMap = ConcurrentMapFactory.createMap(initialCapacity, loadFactor, concurrencyLevel);
}
public ConcurrentRefValueHashMap(int initialCapacity,
float loadFactor,
int concurrencyLevel,
@NotNull TObjectHashingStrategy<K> hashingStrategy) {
myMap = ConcurrentMapFactory.DEFAULT_FACTORY.createMap(initialCapacity, loadFactor, concurrencyLevel, hashingStrategy);
myMap = ConcurrentMapFactory.createMap(initialCapacity, loadFactor, concurrencyLevel, hashingStrategy);
}
protected interface MyValueReference<K, V> {
@@ -299,7 +299,7 @@ public class ContainerUtil extends ContainerUtilRt {
@NotNull
public static <K, V> ConcurrentMap<K, V> newConcurrentMap() {
return ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
return ConcurrentMapFactory.createMap();
}
@NotNull
@@ -39,7 +39,7 @@ import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
public class RefCountingStorage extends AbstractStorage {
private final Map<Integer, Future<?>> myPendingWriteRequests = ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
private final Map<Integer, Future<?>> myPendingWriteRequests = ConcurrentMapFactory.createMap();
private int myPendingWriteRequestsSize;
private final ThreadPoolExecutor myPendingWriteRequestsExecutor = new ThreadPoolExecutor(1, 1, Long.MAX_VALUE, TimeUnit.DAYS, new LinkedBlockingQueue<Runnable>(), ConcurrencyUtil.newNamedThreadFactory("RefCountingStorage write content helper"));
@@ -34,9 +34,9 @@ public class DefaultPicoContainer implements MutablePicoContainer, Serializable
private final PicoContainer parent;
private final Set<PicoContainer> children = new HashSet<PicoContainer>();
private final Map<Object, ComponentAdapter> componentKeyToAdapterCache = ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
private final Map<Object, ComponentAdapter> componentKeyToAdapterCache = ConcurrentMapFactory.createMap();
private final LinkedHashSetWrapper<ComponentAdapter> componentAdapters = new LinkedHashSetWrapper<ComponentAdapter>();
private final Map<String, ComponentAdapter> classNameToAdapter = ConcurrentMapFactory.DEFAULT_FACTORY.createMap();
private final Map<String, ComponentAdapter> classNameToAdapter = ConcurrentMapFactory.createMap();
private final AtomicReference<FList<ComponentAdapter>> nonAssignableComponentAdapters = new AtomicReference<FList<ComponentAdapter>>(FList.<ComponentAdapter>emptyList());
public DefaultPicoContainer(@NotNull ComponentAdapterFactory componentAdapterFactory, PicoContainer parent) {