nz.co.gregs.dbvolution.internal.properties.RowDefinitionWrapperFactory Maven / Gradle / Ivy
package nz.co.gregs.dbvolution.internal.properties;
import java.util.HashMap;
import java.util.Map;
import nz.co.gregs.dbvolution.query.RowDefinition;
/**
* Constructs class adaptors for DB table classes and maintains an in-memory
* cache for re-use. Creating class adaptors is expensive and this class is
* provided as a convenience for anything that needs to access class adaptors
* for multiple types and would benefit from the performance improvement of
* caching their values.
*
*
* Note that class adaptors are immutable, so this is safe to do.
*
*
* This class is thread-safe.
*
*
Support DBvolution at
* Patreon
*
* @author Malcolm Lett
*/
public class RowDefinitionWrapperFactory {
/**
* Thread-safety: access to this object must be synchronized on it
*/
private final Map, RowDefinitionClassWrapper> classWrappersByClass = new HashMap, RowDefinitionClassWrapper>();
/**
* Gets the class adaptor for the given class. If an adaptor for the given
* class has not yet been created, one will be created and added to the
* internal cache.
*
* @param clazz clazz
* Support DBvolution at
* Patreon
* @return the class adaptor
*/
public RowDefinitionClassWrapper classWrapperFor(Class extends RowDefinition> clazz) {
synchronized (classWrappersByClass) {
RowDefinitionClassWrapper wrapper = classWrappersByClass.get(clazz);
if (wrapper == null) {
wrapper = new RowDefinitionClassWrapper(clazz);
classWrappersByClass.put(clazz, wrapper);
}
return wrapper;
}
}
/**
* Gets the object adaptor for the given object. If an adaptor for the
* object's class has not yet been created, one will be created and added to
* the internal cache.
*
* @param object the DBRow instance to wrap
* Support DBvolution at
* Patreon
* @return the object adaptor for the given object
*/
public RowDefinitionInstanceWrapper instanceWrapperFor(RowDefinition object) {
return classWrapperFor(object.getClass()).instanceWrapperFor(object);
}
}