package com.ozs.da.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

/**
 * @author 许明
 * on 2019/3/5.
 * @version 2.0.0
 */
public final class CommonConfUtil {
    private static final Logger logger = LogManager.getLogger(CommonConfUtil.class);
    private static HashMap<String, String> map = new HashMap<>();

    static {
        try {
            Properties properties = new Properties();
            properties.load(CommonConfUtil.class.getResourceAsStream("/CommonConfig.properties"));
            properties.keySet().iterator().forEachRemaining(k -> map.put(String.valueOf(k), String.valueOf(properties.get(k))));
        } catch (IOException e) {
            logger.error("读取配置文件错误，错误信息：" + e.getMessage());
        }
    }

    public static String getConf(String key) {
        return map.get(key);
    }

    public static int getInteger(String key, int defaultValue) {
        try {
            return Integer.parseInt(map.get(key));
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static long getLong(String key, long defaultValue) {
        try {
            return Long.parseLong(map.get(key));
        } catch (Exception e) {
            return defaultValue;
        }
    }
    public static boolean getBool(String key, boolean defaultValue) {
        try {
            return Boolean.valueOf(map.get(key));
        } catch (Exception e) {
            return false;
        }
    }
}
