org.xblackcat.sjpu.skel.InstanceClassCachedFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sjpu-dbah Show documentation
Show all versions of sjpu-dbah Show documentation
Service for generating DB access logic in simple way via interfaces and annotations
package org.xblackcat.sjpu.skel;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* 04.04.2014 15:47
*
* @author xBlackCat
*/
public class InstanceClassCachedFactory extends ACachedFactory > {
protected final ReadWriteLock lock = new ReentrantReadWriteLock();
public InstanceClassCachedFactory(IBuilder builder, Class>... argClasses) {
super(builder, argClasses);
}
public T get(Class clazz, Object... args) throws GeneratorException {
Key key = new Key(clazz, args);
Class extends Base> accessHelperClass;
lock.readLock().lock();
try {
accessHelperClass = helpers.get(key);
} finally {
lock.readLock().unlock();
}
if (accessHelperClass == null) {
lock.writeLock().lock();
try {
final Class extends T> builtClass = builder.build(clazz);
Class extends Base> oldAccessHelper = helpers.get(key);
if (oldAccessHelper != null) {
accessHelperClass = oldAccessHelper;
} else {
accessHelperClass = builtClass;
}
} finally {
lock.writeLock().unlock();
}
}
@SuppressWarnings("unchecked")
final Class extends T> helperClass = (Class extends T>) accessHelperClass;
return instantiate(helperClass, args);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy