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

io.datakernel.config.ConfigWithFullPath Maven / Gradle / Ivy

package io.datakernel.config;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NoSuchElementException;

import static io.datakernel.common.Preconditions.checkArgument;
import static io.datakernel.config.Config.concatPath;

public final class ConfigWithFullPath implements Config {
	private final String path;
	private final Config config;
	private final Map children;

	private ConfigWithFullPath(String path, Config config) {
		this.path = path;
		this.config = config;
		this.children = new LinkedHashMap<>();
		config.getChildren().forEach((key, value) ->
				this.children.put(key, new ConfigWithFullPath(concatPath(this.path, key), value)));
	}

	public static ConfigWithFullPath wrap(Config config) {
		return new ConfigWithFullPath("", config);
	}

	@Override
	public String getValue(String defaultValue) {
		return config.getValue(defaultValue);
	}

	@Override
	public String getValue() throws NoSuchElementException {
		try {
			return config.getValue();
		} catch (NoSuchElementException ignored) {
			throw new NoSuchElementException(path);
		}
	}

	@Override
	public Map getChildren() {
		return children;
	}

	@Override
	public Config provideNoKeyChild(String key) {
		checkArgument(!children.containsKey(key), "Children already contain key '%s'", key);
		return new ConfigWithFullPath(concatPath(path, key), config.provideNoKeyChild(key));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy