au.csiro.sparkle.cmd.DefaultObjectFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of variant-spark_2.11 Show documentation
Show all versions of variant-spark_2.11 Show documentation
Genomic variants interpretation toolkit
The newest version!
package au.csiro.sparkle.cmd;
import java.util.HashMap;
import java.util.Map;
import au.csiro.sparkle.common.ObjectFactory;
import au.csiro.sparkle.common.ObjectProvider;
public class DefaultObjectFactory implements ObjectFactory{
static class ClassProvider extends ObjectProvider{
private final Class extends T> clazz;
public ClassProvider(Class extends T> clazz) {
this.clazz = clazz;
}
@SuppressWarnings("unchecked")
public ClassProvider(String className) {
try {
this.clazz = (Class) Class.forName(className);
} catch (ClassNotFoundException ex) {
throw new RuntimeException(ex);
}
}
@Override
public T provide() {
try {
return clazz.newInstance();
} catch (InstantiationException ex) {
throw new RuntimeException(ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}
protected Map> modules = new HashMap<>();
public void registerClass(String name, Class extends T> clazz) {
register(name, new ClassProvider(clazz));
}
public void register(String name, ObjectProvider provider) {
modules.put(name, provider);
}
@Override
public T create(String clazzName) {
return modules.computeIfAbsent(clazzName, name-> new ClassProvider(name)).provide();
}
}