resource-registry/src/main/java/org/gcube/informationsystem/resourceregistry/types/TypesCache.java

42 lines
989 B
Java
Raw Normal View History

2021-02-08 16:26:58 +01:00
package org.gcube.informationsystem.resourceregistry.types;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
2021-02-08 16:26:58 +01:00
public class TypesCache {
private static Logger logger = LoggerFactory.getLogger(TypesCache.class);
2021-02-10 15:45:48 +01:00
private static TypesCache typesCache;
2021-02-08 16:26:58 +01:00
2021-02-10 15:45:48 +01:00
public synchronized static TypesCache getInstance() {
if(typesCache == null) {
typesCache = new TypesCache();
}
return typesCache;
}
2021-02-22 16:36:19 +01:00
protected final Map<String, CachedType<?>> cachedTypes;
2021-02-10 15:45:48 +01:00
private TypesCache() {
cachedTypes = new HashMap<>();
2021-02-08 16:26:58 +01:00
}
2021-02-22 16:36:19 +01:00
public CachedType<?> getCachedType(String typeName) {
CachedType<?> cachedType = cachedTypes.get(typeName);
if(cachedType == null ) {
logger.trace("{} not in cache. Going to create {} instance", typeName, CachedType.class.getSimpleName());
2021-02-22 16:36:19 +01:00
cachedType = new CachedType<>(typeName);
cachedTypes.put(typeName, cachedType);
2021-02-08 16:26:58 +01:00
}
return cachedType;
2021-02-08 16:26:58 +01:00
}
2021-02-08 16:26:58 +01:00
}