use of com.google.inject.binder.ConstantBindingBuilder in project open-kilda by telstra.
the class YamlConfigModule method configure.
@Override
protected void configure() {
Map<String, Object> config = parser.loadAsMap();
for (String name : config.keySet()) {
Object value = config.get(name);
ConstantBindingBuilder builder = bindConstant().annotatedWith(Names.named(name));
if (value instanceof String) {
builder.to((String) value);
} else if (value instanceof Integer) {
builder.to((Integer) value);
} else if (value instanceof Long) {
builder.to((Long) value);
} else if (value instanceof Boolean) {
builder.to((Boolean) value);
} else {
// TODO - throw more appropriate exception?
throw new RuntimeException("don't know how to bind constant to value of type" + value.getClass());
}
}
}
use of com.google.inject.binder.ConstantBindingBuilder in project open-kilda by telstra.
the class YamlConfigModule method configure.
@Override
protected void configure() {
Map<String, Object> config = parser.loadAsMap();
for (String name : config.keySet()) {
Object value = config.get(name);
ConstantBindingBuilder builder = bindConstant().annotatedWith(Names.named(name));
if (value instanceof String) {
builder.to((String) value);
} else if (value instanceof Integer) {
builder.to((Integer) value);
} else if (value instanceof Long) {
builder.to((Long) value);
} else if (value instanceof Boolean) {
builder.to((Boolean) value);
} else {
// TODO - throw more appropriate exception?
throw new RuntimeException("don't know how to bind constant to value of type" + value.getClass());
}
}
}
use of com.google.inject.binder.ConstantBindingBuilder in project opt4j by felixreimann.
the class Opt4JModule method configure.
/*
* (non-Javadoc)
*
* @see com.google.inject.AbstractModule#configure()
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
/**
* Configure injected constants.
*/
PropertyModule module = new PropertyModule(this);
for (Property property : module.getProperties()) {
for (Annotation annotation : property.getAnnotations()) {
if (annotation.annotationType().getAnnotation(BindingAnnotation.class) != null) {
Class<?> type = property.getType();
Object value = property.getValue();
ConstantBindingBuilder builder = bindConstant(annotation);
if (type.equals(Integer.TYPE)) {
builder.to((Integer) value);
} else if (type.equals(Long.TYPE)) {
builder.to((Long) value);
} else if (type.equals(Double.TYPE)) {
builder.to((Double) value);
} else if (type.equals(Float.TYPE)) {
builder.to((Float) value);
} else if (type.equals(Byte.TYPE)) {
builder.to((Byte) value);
} else if (type.equals(Short.TYPE)) {
builder.to((Short) value);
} else if (type.equals(Boolean.TYPE)) {
builder.to((Boolean) value);
} else if (type.equals(Character.TYPE)) {
builder.to((Character) value);
} else if (type.equals(String.class)) {
builder.to((String) value);
} else if (type.equals(Class.class)) {
builder.to((Class<?>) value);
} else if (value instanceof Enum<?>) {
builder.to((Enum) value);
} else {
String message = "Constant type not bindable: " + type + " of field " + property.getName() + " in module " + this.getClass().getName();
throw new ConfigurationException(Arrays.asList(new Message(message)));
}
}
}
}
multi(OptimizerStateListener.class);
multi(OptimizerIterationListener.class);
multi(IndividualStateListener.class);
config();
}
Aggregations