com.bstek.dorado.core.ConfigureStore.java Source code

Java tutorial

Introduction

Here is the source code for com.bstek.dorado.core.ConfigureStore.java

Source

/*
 * This file is part of Dorado 7.x (http://dorado7.bsdn.org).
 * 
 * Copyright (c) 2002-2012 BSTEK Corp. All rights reserved.
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * and BSDN commercial (http://www.bsdn.org/licenses) licenses.
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at http://www.bstek.com/contact.
 */

package com.bstek.dorado.core;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.BooleanUtils;

/**
 * ??
 * 
 * @author Benny Bao (mailto:benny.bao@bstek.com)
 * @since Sep 27, 2008
 */
public abstract class ConfigureStore {

    private List<ConfigureListener> listeners;

    public void addListener(ConfigureListener listener) {
        listeners.add(listener);
    }

    public void removeListener(ConfigureListener listener) {
        listeners.remove(listener);
    }

    protected void fireBeforeConfigureChange(String property, Object newValue) {
        if (listeners == null) {
            return;
        }
        for (ConfigureListener listener : listeners) {
            listener.beforeConfigureChange(property, newValue);
        }
    }

    protected void fireOnConfigureChange(String property) {
        if (listeners == null) {
            return;
        }
        for (ConfigureListener listener : listeners) {
            listener.onConfigureChange(property);
        }
    }

    /**
     * ?????
     */
    public abstract boolean contains(String key);

    /**
     * ????
     * 
     * @param key ???
     */
    public abstract Object get(String key);

    /**
     * ?
     * 
     * @param key ???
     */
    public abstract void remove(String key);

    /**
     * ?
     * 
     * @param key ???
     * @param value 
     */
    public final void set(String key, Object value) {
        fireBeforeConfigureChange(key, value);
        doSet(key, value);
        fireOnConfigureChange(key);
    }

    protected abstract void doSet(String key, Object value);

    /**
     * ????
     * 
     * @return ????
     */
    public abstract Set<String> keySet();

    /**
     * String???
     * 
     * @param key
     *            ???
     */
    public String getString(String key) {
        Object value = get(key);
        return (value == null) ? null : value.toString();
    }

    /**
     * String?????
     * 
     * @param key ???
     * @param defaultValue 
     */
    public String getString(String key, String defaultValue) {
        if (contains(key)) {
            return getString(key);
        } else {
            return defaultValue;
        }
    }

    /**
     * boolean???
     * 
     * @param key
     *            ???
     */
    public boolean getBoolean(String key) {
        Object value = get(key);
        return (value instanceof Boolean) ? ((Boolean) value).booleanValue()
                : BooleanUtils.toBoolean((value == null) ? null : value.toString());
    }

    /**
     * boolean?????
     * 
     * @param key ???
     * @param defaultValue 
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        if (contains(key)) {
            return getBoolean(key);
        } else {
            return defaultValue;
        }
    }

    /**
     * long???
     * 
     * @param key ???
     */
    public long getLong(String key) {
        Object value = get(key);
        return (value instanceof Number) ? ((Number) value).longValue()
                : Long.parseLong((value == null) ? null : value.toString());
    }

    /**
     * long?????
     * 
     * @param key ???
     * @param defaultValue 
     */
    public long getLong(String key, long defaultValue) {
        if (contains(key)) {
            return getLong(key);
        } else {
            return defaultValue;
        }
    }
}