org.xblackcat.sjpu.skel.ACachedFactory 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 org.apache.commons.lang3.ArrayUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/**
* 14.10.2015 14:48
*
* @author xBlackCat
*/
public abstract class ACachedFactory implements IFactory {
protected final ReadWriteLock lock = new ReentrantReadWriteLock();
protected final Class>[] argClasses;
protected final Map helpers = new HashMap<>();
protected final IBuilder builder;
public ACachedFactory(IBuilder builder, Class>... argClasses) {
this.argClasses = argClasses;
this.builder = builder;
}
protected T instantiate(Class extends T> builtClass, Object[] args) throws GeneratorException {
try {
return builtClass.getConstructor(argClasses).newInstance(args);
} catch (InstantiationException e) {
throw new GeneratorException("Class is not implemented", e);
} catch (IllegalAccessException e) {
throw new GeneratorException("Access helper constructor should be public", e);
} catch (InvocationTargetException e) {
throw new GeneratorException("Exception occurs in access helper constructor", e);
} catch (NoSuchMethodException e) {
throw new GeneratorException(
"Access helper class constructor should have the following signature: " +
builtClass.getName() + "(" + ArrayUtils.toString(argClasses) + " arg);",
e
);
}
}
@Override
public ReadWriteLock getLock() {
return lock;
}
@Override
public void purge() {
lock.writeLock().lock();
try {
helpers.clear();
} finally {
lock.writeLock().unlock();
}
}
protected final static class Key {
private final Class> clazz;
private final Object[] args;
public Key(Class> clazz, Object[] args) {
this.clazz = clazz;
this.args = args;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
return Objects.equals(clazz, key.clazz) &&
Arrays.equals(args, key.args);
}
@Override
public int hashCode() {
return Objects.hash(clazz, Arrays.hashCode(args));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy