/*
 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;

/**
 * Events of this type are fired by Symbol objects to
 * notify listeners about changes of various kinds. Instances of this class 
 * are constructed via the static valueOf factory methods. 
 *
 * @author Markus Kraetzig
 */
public class SymbolEvent {
    private final Symbol source;

    private final SymbolEventTypes type;

    private final Object newValue;

    private final Object oldValue;

    /**
     * SymbolEvent constructor.
     */
    private SymbolEvent(Symbol source, SymbolEventTypes type, boolean isEmpty) {
        this(source, type, new Boolean(!isEmpty), new Boolean(isEmpty));
    }

    /**
     * SymbolEvent constructor.
     */
    private SymbolEvent(Symbol source, SymbolEventTypes type, Object oldValue,
            Object newValue) {
        super();

        this.source = source;
        this.type = type;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    /**
     * Gets the value after the change.
     * 
     * @return an arbitrary object 
     */
    public final Object getNewValue() {
        return newValue;
    }

    /**
     * Gets the value after the change as a boolean.
     * 
     * @return true if new value is a Boolean that is true,
     * 			false otherwise
     */
    public final boolean isSourceEmpty() {

        if (newValue == null)
            return true;

        return source.isEmpty();
    }

    /**
     * Gets the value before the change.
     * 
     * @return an arbitrary object 
     */
    public final Object getOldValue() {
        return oldValue;
    }

    /**
     * Gets the data object where the event was fired from.
     * 
     * @return a Symbol object 
     */
    public final Symbol getSource() {
        return source;
    }

    /**
     * Gets the type of this event.
     * 
     * @return one of  SymbolEventTypes 
     */
    public final SymbolEventTypes getType() {
        return type;
    }

    /**
     * Gets a string representation of this.
     * 
     * @return informative string representation
     */
    public String toString() {
        return super.toString() + " [source symbol:name=" + source.NAME
                + ",type=" + source.type + ";evttype=" + type + "]";
    }

    /**
     * Gets an instance of this for events of type SymbolEventTypes.EMPTY_STATE.
     * 
     * @param source the source from which the event is fired
     * @param isEmptyNow true if source is empty
     * @return a cleared event
     */
    public static SymbolEvent valueOfEmptyState(Symbol source,
            boolean isEmptyNow) {

        return new SymbolEvent(source, SymbolEventTypes.EMPTY_STATE, isEmptyNow);
    }

    /**
     * Gets an instance of this for events of type JSCDataEvent.
     * This method translates a JSCDataEvent into a
     * SymbolEvent.
     *
     * @param source the source from which the event is fired
     * @param evt    the JSCDataEvent to be translated to a SymbolEvent
     * @return a symbol event
     */
    public static SymbolEvent valueOfJSCDataEvent(Symbol source,
            JSCDataEvent evt) {

        if (evt.getType() == JSCDataEventTypes.EMPTY_STATE)
            return valueOfEmptyState(source, ((Boolean) evt.getNewValue())
                    .booleanValue());

        if (evt.getType() == JSCDataEventTypes.VALUE_CHANGED)
            return valueOfValChanged(source, evt.getOldValue(), evt
                    .getNewValue());

        throw new IllegalArgumentException("JSCDataEvent " + evt
                + " could not be translated to a SymbolEvent.");

    }

    /**
     * Gets an instance of this for events of type SymbolEventTypes.VALUE_CHANGED.
     *
     * @param source the source from which the event is fired
     * @param oldValue the value before the change
     * @param newValue the value after the change
     *
     * @return a changed event
     */
    /*public static SymbolEvent valueOfRefChanged(
     Symbol source,
     JSCData oldValue,
     JSCData newValue) {
     
     return new SymbolEvent(
     source,
     SymbolEventTypes.REF_CHANGED,
     oldValue,
     newValue);
     }*/

    /**
     * Gets an instance of this for events of type SymbolEventTypes.VALUE_CHANGED.
     *
     * @param source the source from which the event is fired
     * @param oldValue the value before the change
     * @param newValue the value after the change
     *
     * @return a changed event
     */
    public static SymbolEvent valueOfValChanged(Symbol source, Object oldValue,
            Object newValue) {

        return new SymbolEvent(source, SymbolEventTypes.VALUE_CHANGED,
                oldValue, newValue);
    }

    /**
     * Gets an instance of this for events of type SymbolEventTypes.SOURCE_CHANGED.
     *
     * @param oldSource the value before the change
     * @param newSource the value after the change
     *
     * @return a changed event
     
     public static SymbolEvent valueOfSourceChanged(
     Symbol oldSource,
     Symbol newSource) {
     
     return new SymbolEvent(
     oldSource,
     SymbolEventTypes.SOURCE_CHANGED,
     oldSource,
     newSource);
     }*/
}
 

Ads help maintain this website.