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

org.guppy4j.config.PropertiesSettings Maven / Gradle / Ivy

There is a newer version: 0.0.7
Show newest version
package org.guppy4j.config;

import org.guppy4j.log.Log;
import org.guppy4j.log.LogProvider;
import org.guppy4j.logic.Predicate;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static java.util.Map.Entry;
import static org.guppy4j.log.Log.Level.info;

/**
 * Settings based on a properties file
 */
public class PropertiesSettings implements Settings {

    private final Map settingsMap;

    public PropertiesSettings(LogProvider logProvider,
                              URL propertiesLocation) {
        this(logProvider,
                loadProperties(propertiesLocation),
                propertiesLocation.toString());
    }

    public PropertiesSettings(LogProvider logProvider,
                              Properties properties,
                              String propertiesOrigin) {

        settingsMap = toSettingsMap(properties);

        final Log log = logProvider.getLog(getClass());
        log.as(info, "Settings loaded from {}", propertiesOrigin);
        log.as(info, "Number of name/value pairs: {}", settingsMap.size());
    }

    @Override
    public Setting get(String name) {
        return settingsMap.get(name);
    }

    @Override
    public Iterable all() {
        return settingsMap.values();
    }

    @Override
    public Iterable matches(Predicate condition) {
        final List list = new LinkedList<>();
        for (Setting s : all()) {
            if (condition.isTrueFor(s)) {
                list.add(s);
            }
        }
        return list;
    }

    private static Properties loadProperties(URL url) {
        final Properties properties = new Properties();
        try (InputStream stream = url.openStream()) {
            properties.load(stream);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        return properties;
    }

    private static Map toSettingsMap(Properties properties) {
        final Map map = new HashMap<>(properties.size());
        for (Entry entry : properties.entrySet()) {
            final String name = entry.getKey().toString();
            final String value = entry.getValue().toString();
            map.put(name, new SettingImpl(name, value));
        }
        return map;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy