mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
config model improvements
This commit is contained in:
@@ -3,6 +3,7 @@ package io.nosqlbench.nb.api.config;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ConfigAware {
|
||||
void applyConfig(Map<String,?> element);
|
||||
void applyConfig(Map<String, ?> providedConfig);
|
||||
|
||||
ConfigModel getConfigModel();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package io.nosqlbench.nb.api.config;
|
||||
|
||||
public class ConfigElement<T> {
|
||||
|
||||
public final String name;
|
||||
public final Class<? extends T> type;
|
||||
public final String description;
|
||||
private final T defaultValue;
|
||||
public boolean required;
|
||||
|
||||
public ConfigElement(
|
||||
String name,
|
||||
Class<? extends T> type,
|
||||
String description,
|
||||
boolean required,
|
||||
T defaultValue
|
||||
) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.description = description;
|
||||
this.required = required;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Element{" +
|
||||
"name='" + name + '\'' +
|
||||
", type=" + type +
|
||||
", description='" + description + '\'' +
|
||||
", required=" + required +
|
||||
", defaultValue = " + defaultValue +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Class<?> getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public boolean isRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public void setRequired(boolean required) {
|
||||
this.required = required;
|
||||
}
|
||||
|
||||
public T getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package io.nosqlbench.nb.api.config;
|
||||
|
||||
import io.nosqlbench.nb.api.Environment;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ConfigReader extends LinkedHashMap<String, Object> {
|
||||
private final ConfigModel configModel;
|
||||
|
||||
public ConfigReader(ConfigModel model, LinkedHashMap<String, Object> validConfig) {
|
||||
super(validConfig);
|
||||
this.configModel = model;
|
||||
}
|
||||
|
||||
public <T> T paramEnv(String name, Class<? extends T> vclass) {
|
||||
T param = param(name, vclass);
|
||||
if (param instanceof String) {
|
||||
Optional<String> interpolated = Environment.INSTANCE.interpolate(param.toString());
|
||||
if (interpolated.isEmpty()) {
|
||||
throw new RuntimeException("Unable to interpolate env and sys props in '" + param + "'");
|
||||
}
|
||||
return (T) interpolated.get();
|
||||
} else {
|
||||
return param;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public <T> T param(String name, Class<? extends T> vclass) {
|
||||
Object o = get(name);
|
||||
ConfigElement<?> elem = configModel.getElements().get(name);
|
||||
if (elem == null) {
|
||||
throw new RuntimeException("Invalid config element named '" + name + "'");
|
||||
}
|
||||
Class<T> type = (Class<T>) elem.getType();
|
||||
T typeCastedValue = type.cast(o);
|
||||
return typeCastedValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user