/* * @(#)Variable.java 0.0.2 99/07/31 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats; import java.beans.*; import java.io.Serializable; /** * Base class for statistical variables. While Variable can be * instantiated directly, it is usually better to instantiate a subclass, * since Variable does not know anything about a variable other than * its name. (Subclasses can know other things about variables, such as the * possible values, the class type of the values, and so forth.) *

* Subclasses should override the following methods: *

* * @version 0.0.2 07/31/99 * @author Willie Wheeler * @author Joseph Ramsey */ public class Variable implements Serializable { /** Version ID for serialization. */ static final long serialVersionUID = -2774345904834347655L; /** * Name of the name property. */ public static final String NAME_PROPERTY = "name"; /** * Name of this variable. * * @serial */ protected String name; /** Property change support. */ protected transient PropertyChangeSupport support; /** * Builds a variable with a null name. */ public Variable() { this.name = null; } /** * Builds a variable having the specified name. * * @param name name of this variable */ public Variable(String name) { this.name = name; } /** * Sets the name of this variable. * * @param name name of this variable */ public void setName(String name) { String oldName = this.name; firePropertyChange(NAME_PROPERTY, oldName, this.name = name); } /** * Returns the name of this variable. * * @return name of this variable */ public String getName() { return name; } /** * Checks to see whether the passed value is an acceptable value for * this variable. For Variable, this method always * returns true. *

* Subclasses should override checkValue() in order to provide * for subclass-specific value checking. The value should pass the test * if it can be converted into an equivalent object of the correct class * type (see getValueClass() for this variable; otherwise, it * should fail. In general, checkValue() should not fail a * value for simply not being an instance of a particular class. *

* Since this method is not static, subclasses may (but need not) * provide for instance-specific value checking. * * @param value a value * @return true if the value is an acceptable * value for this variable, and * false otherwise * @see #getValueClass */ public boolean checkValue(Object value) { return true; } /** * Converts the value to an equivalent object of the correct class type * (see getValueClass() for values of this variable. * For Variable, the returned value is simply the passed value, * unmodified. *

* Subclasses should override this method in order to provide for subclass- * specific value filtering. Since this method is not static, * subclasses may (but need not) provide for instance-specific value * filtering. * * @param value a value * @return an equivalent object of the correct class * type for values of this variable * @see #getValueClass */ public Object filterValue(Object value) { return value; } /** * Returns the Class representing the class type for values of * this variable. The Variable implementation returns * Object.class. *

* Subclasses should override this method in order to provide subclass- * specific class type information. Since this method is not * static, subclasses may (but need not) provide an instance- * specific value class. * * @return the Class representing the class * type for values of this variable */ public Class getValueClass() { return Object.class; } /** * Returns the value to which variable data should be cleared when a * clear operation is performed on that data. The Variable * implementation returns null, meaning that individual data * should be cleared to null. *

* Subclasses should override this method in order to provide subclass- * specific clear values. Since this method is not static, * subclasses may (but need not) provide an instance-specific clear value. * * @return the clear value for data obtained on * this variable */ public Object getClearValue() { return null; } /** * Returns a String representation of this variable. Specifically, the * name of the variable is returned. * * @return name of this variable */ public String toString() { return name; } /** * Adds a property change listener to this variable. * * @param listener property change listener to add */ public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { support.addPropertyChangeListener(listener); } /** * Fires a property change event. * * @param propertyName property name * @param oldValue old property value * @param newValue new property value */ public void firePropertyChange(String propertyName, Object oldValue, Object newValue) { support.firePropertyChange(propertyName, oldValue, newValue); } /** * Removes a property change listener from this variable. * * @param listener property change listener to remove */ public synchronized void removePropertyChangeListener(PropertyChangeListener listener) { support.removePropertyChangeListener(listener); } }