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

lodsve.core.properties.env.PropertiesConfiguration Maven / Gradle / Ivy

There is a newer version: 2.7.5-RELEASE
Show newest version
/*
 * Copyright (C) 2018  Sun.Hao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package lodsve.core.properties.env;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 配置文件.
 *
 * @author sunhao([email protected])
 * @version V1.0, 14-8-17 下午9:44
 */
public class PropertiesConfiguration extends AbstractConfiguration {

    private Map store = new HashMap();

    public PropertiesConfiguration(Properties properties) {
        load(properties);
    }

    private void load(Properties properties) {
        for (Map.Entry entry : properties.entrySet()) {
            addPropertyValues((String) entry.getKey(), entry.getValue(),
                    isDelimiterParsingDisabled() ? DISABLED_DELIMITER : getListDelimiter());
        }
    }

    @Override
    public Set getKeys() {
        return store.keySet();
    }

    @Override
    public boolean containsKey(String key) {
        return store.containsKey(key);
    }

    @Override
    public Object getProperty(String key) {
        return store.get(key);
    }

    protected void addPropertyValues(String key, Object value, char delimiter) {
        Iterator it = PropertyConverter.toIterator(value, delimiter);
        while (it.hasNext()) {
            addPropertyDirect(key, it.next());
        }
    }

    protected void addPropertyDirect(String key, Object value) {
        Object previousValue = getProperty(key);

        if (previousValue == null) {
            store.put(key, value);
        } else if (previousValue instanceof List) {
            // safe to case because we have created the lists ourselves
            @SuppressWarnings("unchecked")
            List valueList = (List) previousValue;
            // the value is added to the existing list
            valueList.add(value);
        } else {
            // the previous value is replaced by a list containing the previous
            // value and the new value
            List list = new ArrayList();
            list.add(previousValue);
            list.add(value);

            store.put(key, list);
        }
    }

    @Override
    public Map getStore() {
        return store;
    }
}