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

org.openstreetmap.atlas.utilities.configuration.ConfigurationReader Maven / Gradle / Ivy

There is a newer version: 7.0.8
Show newest version
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);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy