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

com.hframework.common.util.message.PropertyReader Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.hframework.common.util.message;

import java.util.*;

/**
 * Created by zhangquanhong on 2017/4/27.
 */
public class PropertyReader {

    private String path;

    private List propertyNameDefines = new ArrayList();

    private Map> propertyDefineAndExtName = new LinkedHashMap>();

    private Map propertyNameAndValue = new HashMap();

    private List unknownPropertyName = new ArrayList();

    private boolean beenGroup = false;

    public PropertyReader addDefine(String propertyName) {
        propertyNameDefines.add(propertyName);
        return this;
    }

    public PropertyReader addDefine(String... propertyName) {
        propertyNameDefines.addAll(Arrays.asList(propertyName));
        return this;
    }

    public Map getAsList(String propertyName) {
        if(!beenGroup) {
            synchronized (this) {
                if(!beenGroup) {
                    group();
                    beenGroup = true;
                }
            }
        }

        Map result = new HashMap();
        if(propertyDefineAndExtName.containsKey(propertyName)) {
            List extNames = propertyDefineAndExtName.get(propertyName);
            for (String extName : extNames) {
                result.put(extName, propertyNameAndValue.get(propertyName + extName));
            }
        }else {
            if(propertyNameAndValue.containsKey(propertyName)) {
                result.put("", propertyNameAndValue.get(propertyName));
            }
        }
        return result;
    }

    private void group() {
        Collections.sort(propertyNameDefines);
        Collections.reverse(propertyNameDefines);
        for (Map.Entry nameAndValue : propertyNameAndValue.entrySet()) {
            String key = nameAndValue.getKey();
            if(!propertyNameDefines.contains(key)) {
                for (String propertyNameDefine : propertyNameDefines) {
                    if(key.startsWith(propertyNameDefine)) {
                        if(!propertyDefineAndExtName.containsKey(propertyNameDefine)) {
                            propertyDefineAndExtName.put(propertyNameDefine, new ArrayList());
                        }
                        propertyDefineAndExtName.get(propertyNameDefine).add(key.substring(propertyNameDefine.length()));
                        break;
                    }
                }
                unknownPropertyName.add(key);
            }
        }
    }

    public Integer getAsInt(String propertyName, Integer defaultValue) {
        if(propertyNameAndValue.get(propertyName) != null) {
            return Integer.valueOf(propertyNameAndValue.get(propertyName).trim());
        }else {
            return defaultValue;
        }
    }

    public Boolean getAsBoolean(String propertyName, boolean defaultValue) {
        if(propertyNameAndValue.get(propertyName) != null) {
            return Boolean.valueOf(propertyNameAndValue.get(propertyName).trim());
        }else {
            return defaultValue;
        }
    }

    public Long getAsLong(String propertyName, Long defaultValue) {
        if(propertyNameAndValue.get(propertyName) != null) {
            return Long.valueOf(propertyNameAndValue.get(propertyName).trim());
        }else {
            return defaultValue;
        }
    }
    public String get(String propertyName, String defaultValue) {
        if(propertyNameAndValue.get(propertyName) != null) {
            return propertyNameAndValue.get(propertyName).trim();
        }else {
            return defaultValue;
        }
    }
    public String get(String propertyName) {
        if(propertyNameAndValue.get(propertyName) != null) {
            return propertyNameAndValue.get(propertyName).trim();
        }else {
            return null;
        }
    }

    public PropertyReader merge(String resourceName) {
        try{
            ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceName.replace(".properties",""));
            if(resourceBundle == null) return this;
            return mergeProperty(this, resourceBundle);
        }catch (Exception e ) {
            e.printStackTrace();
            return  this;
        }

    }

    public static PropertyReader read(String resourceName){
        PropertyReader propertyContext = new PropertyReader();
        try{
            ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceName.replace(".properties",""));
            if(resourceBundle == null) return propertyContext;
            propertyContext.setPath(resourceName);
            return mergeProperty(propertyContext, resourceBundle);
        }catch (Exception e) {
            e.printStackTrace();
            return propertyContext;
        }
    }

    private static PropertyReader mergeProperty(PropertyReader propertyContext, ResourceBundle resourceBundle) {
        Enumeration keys = resourceBundle.getKeys();
        while(keys.hasMoreElements()) {
            String elementName = keys.nextElement();
            String elementValue = resourceBundle.getString(elementName);
            propertyContext.put(elementName, elementValue);
        }
        return propertyContext;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void put(String key, String value) {
        propertyNameAndValue.put(key,value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy