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

io.nosqlbench.nb.api.config.params.ElementImpl Maven / Gradle / Ivy

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

import java.util.*;

public class ElementImpl implements Element {

    private final ElementData data;

    public ElementImpl(ElementData data) {
        this.data = data;
    }

    public String getElementName() {
        return get(ElementData.NAME, String.class).orElse(null);
    }

    public  Optional get(String name, Class classOfT) {
        List path = Arrays.asList(name.split("\\."));

        ElementData top = data;
        int idx = 0;
        String lookup = path.get(idx);

        while (idx + 1 < path.size()) {
            if (!top.containsKey(lookup)) {
                throw new RuntimeException("unable to find '" + lookup + "' in '" + String.join(".", path));
            }
            Object o = top.get(lookup);
            top = DataSources.element(o);
//            top = top.getChildElementData(lookup);
            idx++;
            lookup = path.get(idx);
        }

        if (top.containsKey(lookup)) {
            Object elem = top.get(lookup);
            T convertedValue = top.convert(elem, classOfT);
//            T typeCastedValue = classOfT.cast(elem);
            return Optional.of(convertedValue);
        } else {
            return Optional.empty();
        }

    }

    public  T getOr(String name, T defaultValue) {
        Class cls = (Class) defaultValue.getClass();
        return get(name, cls).orElse(defaultValue);
    }

    @Override
    public Map getMap() {
        Set keys = this.data.getKeys();
        Map map = new LinkedHashMap<>();

        for (String key : keys) {
            Object value = this.data.get(key);
            map.put(key, value);
        }

        return map;
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy