jsky.science
Class Quantity

java.lang.Object
  extended by jsky.science.AbstractScienceObject
      extended by jsky.science.Quantity
All Implemented Interfaces:
java.beans.PropertyChangeListener, java.io.Serializable, java.lang.Cloneable, java.lang.Comparable, java.util.EventListener, ScienceObjectModel, ReplaceablePropertyChangeListener
Direct Known Subclasses:
Flux, Redshift, Time, Wavelength

public abstract class Quantity
extends AbstractScienceObject

Quantity, abstract super class to match values and units and easily allow developers to manage the units a quantity-style value. This class also provides infrastructure for creating new types of quantity values and/or new sets of units.

This class mixes a static master list of quantity types and their affiliated units. Subclasses (such as Time) can easily be created that provide a set of units for a different type of quantity.

The Quantity class also works with the QuantityPanel GUI component to provide a text entry for a unit that automatically tracks, displays and converts values into a user-settable application wide default unit.

This code was developed by NASA, Goddard Space Flight Center, Code 588 for the Scientist's Expert Assistant (SEA) project for Next Generation Space Telescope (NGST). See Time or Wavelength for an examples of quantity subclasses.

Version:
2000.11.29
Author:
Sandy Grosvenor
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class jsky.science.AbstractScienceObject
AbstractScienceObject.EventMonitor
 
Field Summary
protected  double fValue
          internal holder for quantity amount
 
Fields inherited from interface jsky.science.ScienceObjectModel
NAME_PROPERTY, PENDING_PROPERTY, VALID_PROPERTY
 
Constructor Summary
Quantity()
           
 
Method Summary
 Quantity add(Quantity q)
          adds the parameter to the value of the quantity and returns a NEW quantity Throws a ClassCastException if the two classes are not compatible
static void addDefaultUnitsChangeListener(java.lang.Class cl, java.beans.PropertyChangeListener listener)
          Add a listener to the list of objects listening to changes in the default unit type
 boolean equals(java.lang.Object that)
          compares to Quantities for equality.
protected static void fireDefaultUnitsChange(java.lang.Class cl, java.beans.PropertyChangeEvent evt)
          Fire a property change event to all listeners of the default unit type for the specified Quantity subclass
protected  void fireDefaultUnitsChange(java.beans.PropertyChangeEvent evt)
          Fire a property change event to all listeners of the default unit type of this instance's class
 java.util.List getAllUnits()
          Returns the list of string labels for this instance's class
static java.util.List getAllUnits(java.lang.Class cl)
          Returns the list of all units for specfied Quantity subclass
 java.util.List getAllUnitsAbbrev()
          Returns the list of abbreviations for this instance's class
static java.util.List getAllUnitsAbbrev(java.lang.Class cl)
          Returns the list of abbreviations for specified Quantity subclass
 java.lang.String getDefaultUnits()
          Returns the name of the current default units
static java.lang.String getDefaultUnits(java.lang.Class cl)
          Returns the current default units for the specified Quantity subclass
static java.lang.String getDefaultUnitsAbbrev(java.lang.Class cl)
          Returns the current default units for the specified Class
static java.lang.String getDefaultUnitsProperty(java.lang.Class cl)
          Returns the current default propertyName for the specified Quantity subclass
static java.lang.String getUnitsAbbrev(java.lang.Class cl, java.lang.String def)
          Returns the abbreviation for the specified Units type.
static java.lang.String getUnitsIgnoreCase(java.lang.Class cl, java.lang.String inUnits)
          Does a case-insensitve search through the list of valid units for a class and returns the proper casing for the specified units
 double getValue()
          Returns the value of the an instance as a double in the current default units
abstract  double getValue(java.lang.String units)
          Returns the quantity value in specified units.
 int hashCode()
          sets the hashcode value to the int of the value
static void initializeSubClass(java.lang.Class cl, java.util.List unitNames, java.util.List abbrevStrings, java.lang.String defaultUnits, java.lang.String defaultUnitsChangeProperty)
          Called by static initializer of subclasses to initialize a new quantity.
static boolean isInitialized(java.lang.Class thisClass)
          returns true if the a specified Class has been initialized as a Quantity subclass
abstract  Quantity newInstance(double inValue)
          Returns a new instance an object with same value as creating instance.
static void removeDefaultUnitsChangeListener(java.lang.Class cl, java.beans.PropertyChangeListener listener)
          Remove a listener from the list of objects listening to changes in the default unit type
static void setDefaultUnits(java.lang.Class cl, java.lang.String inUnits)
          Sets the default units, for the specified Quantity subclass
protected abstract  void setValue(double inValue, java.lang.String inUnits)
          Sets the value of the Quantity in specified units
 java.lang.String toString()
          returns a string displaying the current value in the default units to 2 decimal places of accuracy.
 java.lang.String toString(int decs)
          formats the current quantity value in the default units to the specfied decimal accuracy.
 
Methods inherited from class jsky.science.AbstractScienceObject
addEventMonitor, addPropertyChangeListener, areNamesEqual, clearAllListeners, clone, compareTo, createDefaultName, firePropertyChange, firePropertyChange, fireReplaceObject, fireReplaceObject, fireReplaceObject, fireVetoableChange, fireVetoableChange, getException, getLabel, getName, getObjectIdString, getObjectIdString, getParent, isHolding, isPending, isTracing, isValid, notifyEventMonitors, propertyChange, removeEventMonitor, removePropertyChangeListener, replaceObject, replaceObjectNYI, setException, setHolding, setName, setParent, setTraceAll, setTracing, writeDebug, writeError
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

fValue

protected double fValue
internal holder for quantity amount

Constructor Detail

Quantity

public Quantity()
Method Detail

getValue

public abstract double getValue(java.lang.String units)
Returns the quantity value in specified units. Must be defined by subclasses and should support all units supported by the subclass


newInstance

public abstract Quantity newInstance(double inValue)
Returns a new instance an object with same value as creating instance.

Note this method is only expected to be called by QuantityPanel.actionPerformed()


setValue

protected abstract void setValue(double inValue,
                                 java.lang.String inUnits)
Sets the value of the Quantity in specified units


initializeSubClass

public static void initializeSubClass(java.lang.Class cl,
                                      java.util.List unitNames,
                                      java.util.List abbrevStrings,
                                      java.lang.String defaultUnits,
                                      java.lang.String defaultUnitsChangeProperty)
Called by static initializer of subclasses to initialize a new quantity.

Parameters:
cl - The class of the Quantity subclass
unitNames - a List of long names of the supported units
abbrevStrings - a List of abbreviations of the supported units (must be same length and order as the unitNames
defaultUnits - the unit that is the starting default unit
defaultUnitsChangeProperty - the bound propertyName to be specified in PropertyChangeEvents when the default units are changed

isInitialized

public static boolean isInitialized(java.lang.Class thisClass)
returns true if the a specified Class has been initialized as a Quantity subclass


getDefaultUnitsAbbrev

public static java.lang.String getDefaultUnitsAbbrev(java.lang.Class cl)
Returns the current default units for the specified Class


getUnitsAbbrev

public static java.lang.String getUnitsAbbrev(java.lang.Class cl,
                                              java.lang.String def)
Returns the abbreviation for the specified Units type.

Parameters:
cl - Class reference to the desired Quantity subclass
def - the long name of the units for which the abbreviation is desired.

getDefaultUnits

public static java.lang.String getDefaultUnits(java.lang.Class cl)
Returns the current default units for the specified Quantity subclass


getDefaultUnitsProperty

public static java.lang.String getDefaultUnitsProperty(java.lang.Class cl)
Returns the current default propertyName for the specified Quantity subclass


setDefaultUnits

public static void setDefaultUnits(java.lang.Class cl,
                                   java.lang.String inUnits)
Sets the default units, for the specified Quantity subclass


getValue

public double getValue()
Returns the value of the an instance as a double in the current default units


getDefaultUnits

public java.lang.String getDefaultUnits()
Returns the name of the current default units


toString

public java.lang.String toString()
returns a string displaying the current value in the default units to 2 decimal places of accuracy. @see toString( decs)

Overrides:
toString in class AbstractScienceObject

toString

public java.lang.String toString(int decs)
formats the current quantity value in the default units to the specfied decimal accuracy. Scientific notation may be used if necessary.


equals

public boolean equals(java.lang.Object that)
compares to Quantities for equality.

Overrides:
equals in class AbstractScienceObject

hashCode

public int hashCode()
sets the hashcode value to the int of the value

Overrides:
hashCode in class java.lang.Object

getAllUnits

public static java.util.List getAllUnits(java.lang.Class cl)
Returns the list of all units for specfied Quantity subclass

Parameters:
cl - Quantity subclass

getAllUnits

public java.util.List getAllUnits()
Returns the list of string labels for this instance's class


getAllUnitsAbbrev

public static java.util.List getAllUnitsAbbrev(java.lang.Class cl)
Returns the list of abbreviations for specified Quantity subclass


getAllUnitsAbbrev

public java.util.List getAllUnitsAbbrev()
Returns the list of abbreviations for this instance's class


getUnitsIgnoreCase

public static java.lang.String getUnitsIgnoreCase(java.lang.Class cl,
                                                  java.lang.String inUnits)
Does a case-insensitve search through the list of valid units for a class and returns the proper casing for the specified units

Parameters:
cl - the subclass of Quantity to be searched
inUnits - the units string to be searched for

addDefaultUnitsChangeListener

public static void addDefaultUnitsChangeListener(java.lang.Class cl,
                                                 java.beans.PropertyChangeListener listener)
Add a listener to the list of objects listening to changes in the default unit type

Parameters:
listener - to add

add

public Quantity add(Quantity q)
             throws java.lang.ClassCastException
adds the parameter to the value of the quantity and returns a NEW quantity Throws a ClassCastException if the two classes are not compatible

Throws:
java.lang.ClassCastException

removeDefaultUnitsChangeListener

public static void removeDefaultUnitsChangeListener(java.lang.Class cl,
                                                    java.beans.PropertyChangeListener listener)
Remove a listener from the list of objects listening to changes in the default unit type

Parameters:
listener - to remove

fireDefaultUnitsChange

protected static void fireDefaultUnitsChange(java.lang.Class cl,
                                             java.beans.PropertyChangeEvent evt)
Fire a property change event to all listeners of the default unit type for the specified Quantity subclass


fireDefaultUnitsChange

protected void fireDefaultUnitsChange(java.beans.PropertyChangeEvent evt)
Fire a property change event to all listeners of the default unit type of this instance's class