io.github.linpeilie.AbstractCachedConverterFactory Maven / Gradle / Ivy
The newest version!
package io.github.linpeilie;
import java.util.concurrent.ConcurrentHashMap;
public abstract class AbstractCachedConverterFactory implements ConverterFactory {
private final ConcurrentHashMap mapperMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap cycleAvoidingMapper = new ConcurrentHashMap<>();
private final ConcurrentHashMap mapMapperMap = new ConcurrentHashMap<>();
@Override
@SuppressWarnings("unchecked")
public BaseMapper getMapper(final Class sourceType, final Class targetType) {
Class> source = wrapperClass(sourceType);
Class> target = wrapperClass(targetType);
String key = key(source, target);
if (mapperMap.containsKey(key)) {
return mapperMap.get(key);
}
BaseMapper mapper = findMapper(source, target);
if (mapper != null) {
mapperMap.put(key, mapper);
return mapper;
}
return null;
}
@Override
public BaseCycleAvoidingMapper getCycleAvoidingMapper(Class sourceType, Class targetType) {
BaseMapper mapper = getMapper(sourceType, targetType);
if (mapper == null) {
return null;
}
if (mapper instanceof BaseCycleAvoidingMapper) {
return (BaseCycleAvoidingMapper) mapper;
}
return null;
}
@Override
public BaseMapMapper getMapMapper(final Class sourceType) {
final Class> source = wrapperClass(sourceType);
final String key = source.getName();
if (mapMapperMap.containsKey(key)) {
return mapMapperMap.get(key);
}
final BaseMapMapper mapper = findMapMapper(source);
if (mapper != null) {
mapMapperMap.put(key, mapper);
return mapper;
}
return null;
}
protected Class> wrapperClass(final Class> clazz) {
return clazz;
}
protected abstract BaseMapper findMapper(Class source, Class target);
protected abstract BaseMapMapper findMapMapper(final Class> source);
private String key(Class> source, Class> target) {
return source.getName() + "__" + target.getName();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy