All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.nb.api.config.ConfigReader Maven / Gradle / Ivy

There is a newer version: 4.15.102
Show newest version
package io.nosqlbench.nb.api.config;

import io.nosqlbench.nb.api.NBEnvironment;

import java.util.LinkedHashMap;
import java.util.Optional;

public class ConfigReader extends LinkedHashMap {
    private final ConfigModel configModel;

    public ConfigReader(ConfigModel model, LinkedHashMap validConfig) {
        super(validConfig);
        this.configModel = model;
    }

    public  T paramEnv(String name, Class vclass) {
        T param = param(name, vclass);
        if (param instanceof String) {
            Optional interpolated = NBEnvironment.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 param(String name, Class vclass) {
        Object o = get(name);
        ConfigElement elem = configModel.getElements().get(name);
        if (elem == null) {
            throw new RuntimeException("Invalid config element named '" + name + "'" );
        }
        Class type = (Class) elem.getType();
        T typeCastedValue = type.cast(o);
        return typeCastedValue;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.configModel.getOf().getSimpleName()).append(":" );
        sb.append(this.configModel.toString());
        return sb.toString();

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy