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

com.ajjpj.asysmon.config.wiring.ConfigPropsFile Maven / Gradle / Ivy

There is a newer version: 1.0-pre28
Show newest version
package com.ajjpj.asysmon.config.wiring;

import com.ajjpj.asysmon.config.log.ASysMonLogger;
import com.ajjpj.asysmon.config.log.ASysMonLoggerFactory;
import com.ajjpj.abase.collection.immutable.AOption;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;


/**
 * @author arno
 */
public class ConfigPropsFile {
    private final Properties props;
    private final ASysMonLogger log;

    public ConfigPropsFile(Properties props, ASysMonLoggerFactory log) {
        this.props = props;
        this.log = log.getLogger(ConfigPropsFile.class);
    }

    public  T get(String key, Class type, Class... paramTypes) {
        return get(key, AOption.none(), type, paramTypes);
    }

    public  T get(String key, AOption defaultValue, Class type, Class... paramTypes) {
        final ConfigValueResolver r = new ConfigValueResolver(props, key, type, paramTypes);
        return (T) r.get(defaultValue);
    }

    public  List getList(String key, Class type) {
        final ConfigValueResolver r = new ConfigValueResolver(props, key, List.class, new Class[] {type});
        return (List) r.get(AOption.some(Collections.emptyList()));
    }

    public List getListRaw(String key) {
        final List result = new ArrayList();
        final String raw = props.getProperty(key);
        if(raw == null || raw.trim().isEmpty()) {
            return result;
        }

        final String[] split = raw.split(",");
        for(String s: split) {
            final String trimmed = s.trim();
            if(trimmed.isEmpty()) {
                log.warn("configuration error: empty list entry @ " + key + " - skipping");
            }
            else {
                result.add(trimmed);
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy