edu.isi.nlp.ModuleIfPresent Maven / Gradle / Ivy
The newest version!
package edu.isi.nlp;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.inject.Module;
import edu.isi.nlp.parameters.Parameters;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
/**
* Installs specifies module(s) if a specified parameter is present.
*
* @author Ryan Gabbard
*/
public final class ModuleIfPresent {
private final List> classes;
private ModuleIfPresent(final String parameter, final List> classes) {
this.classes = Lists.newArrayList(classes);
}
public static ModuleIfPresent module(Class> clazz) {
return new ModuleIfPresent(null, ImmutableList.>of(clazz));
}
public static ModuleIfPresent modules(Class> clazz1, Class>... clazzes) {
final List> ret = new ArrayList<>();
ret.add(clazz1);
for (final Class> clazz : clazzes) {
ret.add(clazz);
}
return new ModuleIfPresent(null, ret);
}
public Stage2 ifPresent(String param) {
return new Stage2(param);
}
public class Stage2 {
private String parameter;
Stage2(final String parameter) {
this.parameter = checkNotNull(parameter);
}
public Module in(Parameters params) {
if (params.isPresent(parameter)) {
final ImmutableList.Builder ret = ImmutableList.builder();
for (final Class> clazz : classes) {
try {
ret.add(ModuleUtils.classNameToModule(params, clazz));
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {
throw new RuntimeException(
"Error instantiating " + clazz.getName() + " for parameter " + parameter);
}
}
return new ModuleUtils.MultiModule(ret.build());
} else {
return new NoOpModule();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy