org.xblackcat.sjpu.skel.InstanceCachedFactory 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;
/**
* 04.04.2014 15:47
*
* @author xBlackCat
*/
public class InstanceCachedFactory extends ACachedFactory {
public InstanceCachedFactory(IBuilder builder, Class>... argClasses) {
super(builder, argClasses);
}
public T get(Class clazz, Object... args) throws GeneratorException {
Key key = new Key(clazz, args);
lock.readLock().lock();
try {
@SuppressWarnings({"unchecked"})
T accessHelper = (T) helpers.get(key);
if (accessHelper != null) {
return accessHelper;
}
} finally {
lock.readLock().unlock();
}
lock.writeLock().lock();
try {
final Class extends T> builtClass = builder.build(clazz);
T accessHelper = instantiate(builtClass, args);
@SuppressWarnings({"unchecked"})
T oldAccessHelper = (T) helpers.get(key);
if (oldAccessHelper != null) {
return oldAccessHelper;
}
helpers.put(key, accessHelper);
return accessHelper;
} finally {
lock.writeLock().unlock();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy