org.openstreetmap.atlas.utilities.configuration.ConfigurationReader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of atlas Show documentation
Show all versions of atlas Show documentation
"Library to load OSM data into an Atlas format"
package org.openstreetmap.atlas.utilities.configuration;
import java.util.List;
import java.util.function.Function;
import org.openstreetmap.atlas.exception.CoreException;
import com.google.common.collect.Lists;
/**
* Helper class to read a {@link Configuration}
*
* @author matthieun
*/
public class ConfigurationReader
{
private static final String CONFIGURATION_PATH_NAME_DEFAULT = "N/A";
private final String root;
public ConfigurationReader(final String root)
{
this.root = root;
}
public final String configurationKey(final String key)
{
return this.root.isEmpty() ? key : this.root + "." + key;
}
public String configurationValue(final Configuration configuration, final String key)
{
final String result = configuration
.get(configurationKey(key), CONFIGURATION_PATH_NAME_DEFAULT).value();
if (CONFIGURATION_PATH_NAME_DEFAULT.equals(result))
{
throw new CoreException("Malformed configuration for {}", configurationKey(key));
}
return result;
}
public U configurationValue(final Configuration configuration, final String key,
final U defaultValue)
{
return configuration.get(configurationKey(key), defaultValue).value();
}
public T configurationValue(final Configuration configuration,
final Function defaultValue)
{
return configuration.get(this.root, defaultValue).value();
}
public List configurationValues(final Configuration configuration, final String key,
final List defaultValue)
{
return configuration.get(configurationKey(key), defaultValue).value();
}
public List configurationValues(final Configuration configuration, final String key)
{
final List defaults = Lists.newArrayList(CONFIGURATION_PATH_NAME_DEFAULT);
final List result = configurationValues(configuration, key, defaults);
if (defaults.equals(result))
{
throw new CoreException("Malformed configuration for {}", configurationKey(key));
}
return result;
}
public boolean isPresent(final Configuration configuration, final String key)
{
final Object result = configuration
.get(configurationKey(key), CONFIGURATION_PATH_NAME_DEFAULT).value();
return !CONFIGURATION_PATH_NAME_DEFAULT.equals(result);
}
}