/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library 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
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.model;

import java.util.HashMap;
import java.util.Map;

/**
 * Abstract implementation of JSCData which provides default
 * functionality for handling the different listeners.
 * 
 * @author Markus Kraetzig 
 */
public abstract class AbstractJSCData implements JSCData {
    /**
     * Should be used by subclasses to call
     * eventSupport.dispatchEvent.
     */
    protected volatile JSCDataEventSupport eventSupport = null;

    private Map propertyMap = null;

    /**
     * Gets the value for a property type.
     * 
     * @param type
     *            the property type
     * @return the value or null if the property has not been set
     * @throws IllegalArgumentException
     *             if (type == null)
     */
    public Object getJSCProperty(JSCPropertyTypes type) {

        if (type == null)
            throw new IllegalArgumentException("Argument was null.");

        if (getPropertyMap().containsKey(type))
            return getPropertyMap().get(type);
        return null;
    }

    /**
     * Set the value for a property type. Values are not serialized. The
     * properties can be used to store runtime information associated with a
     * data object.
     * 
     * @param type
     *            the property type
     * @param val
     *            the associated property value
     * @throws IllegalArgumentException
     *             if (type == null)
     */
    public void setJSCProperty(JSCPropertyTypes type, Object val) {

        if (type == null)
            throw new IllegalArgumentException("Argument was null.");

        getPropertyMap().put(type, val);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.jstatcom.model.JSCData#removeJSCProperty(com.jstatcom.model.JSCPropertyTypes)
     */
    public void removeJSCProperty(JSCPropertyTypes type) {
        getPropertyMap().remove(type);

    }

    /**
     * Gets a string representation of this JSCData instance.
     * 
     * @return string with name, type and value information
     */
    public synchronized String toString() {
        return super.toString() + " [name=" + name() + ",type=" + type()
                + ",value=" + display() + "]";
    }

    /**
     * @see com.jstatcom.model.JSCData
     */
    public final synchronized void addJSCDataListener(
            JSCDataListener evtListener, JSCDataEventTypes evtType) {

        getEventSupport().addListener(evtListener, evtType);

    }

    /**
     * Default implementation that gets a string representing the value stored
     * in this JSCData object.
     * 
     * @return "{}" if isEmpty == true, otherwise
     *         value().toString()
     */
    public synchronized String display() {
        if (isEmpty())
            return "{}";
        return value().toString();
    }

    /**
     * Lazily initializes eventSupport.
     * 
     * @return instance of JSCDataEventSupport
     */
    protected synchronized JSCDataEventSupport getEventSupport() {
        if (eventSupport == null)
            eventSupport = new JSCDataEventSupport();
        return eventSupport;
    }

    /**
     * Lazily initializes propertyMap.
     * 
     * @return instance of Map
     */
    private synchronized Map getPropertyMap() {
        if (propertyMap == null)
            propertyMap = new HashMap();
        return propertyMap;
    }

    /**
     * @see com.jstatcom.model.JSCData
     */
    public final synchronized void removeJSCDataListener(
            JSCDataListener evtListener) {

        if (eventSupport == null)
            return;

        eventSupport.removeListener(evtListener);
    }
}
 

Ads help maintain this website.