ptolemy.data
Class ScalarToken

java.lang.Object
  |
  +--ptolemy.data.Token
        |
        +--ptolemy.data.ScalarToken
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
ComplexToken, ConcreteScalarToken, DoubleToken, FixToken, IntToken, LongToken, UnsignedByteToken

public abstract class ScalarToken
extends Token

Abstract base class for tokens that contain a scalar. This class defines methods for type conversion among different scalar tokens. The implementation in this base class just throws an exception. Derived class should override the methods that the corresponding conversion can be achieved without loss of information.

Instances of ScalarToken may have units. In the arithmetic methods add() and subtract(), the two operands must have the same units. Otherwise, an exception will be thrown. In the methods multiply() and divide(), the units of the resulting token will be computed automatically.

Since:
Ptolemy II 0.2
Version:
$Id: ScalarToken.java,v 1.46 2002/02/21 19:33:33 cxh Exp $
Author:
Yuhong Xiong, Mudit Goel
See Also:
Serialized Form

Field Summary
protected  int[] _unitCategoryExponents
          The unit category exponents.
 
Constructor Summary
ScalarToken()
           
 
Method Summary
protected  int[] _addCategoryExponents(ScalarToken token)
          Add the corresponding unit category exponents.
protected  boolean _areUnitsEqual(ScalarToken scalarToken)
          Return true if the units of this token are the same as that of the argument token.
protected  int[] _copyOfCategoryExponents()
          Return a copy of the unit category exponents array.
protected  boolean _isUnitless()
          Return true if this token does not have a unit.
protected  int[] _subtractCategoryExponents(ScalarToken token)
          Subtract the corresponding unit category exponents of the argument token from that of this token.
abstract  ScalarToken absolute()
          Return a ScalarToken containing the absolute value of the value of this token.
 Complex complexValue()
          Return the value of this token as a Complex.
 double doubleValue()
          Return the value of this token as a double.
 FixPoint fixValue()
          Return the value of this token as a FixPoint.
 Type getType()
          Return the type of this token.
 int intValue()
          Return the value of this token as an int.
 ScalarToken inUnitsOf(ScalarToken units)
          Return a scalar token that contains the value of this token in the units of the argument token.
abstract  BooleanToken isLessThan(ScalarToken token)
          Check whether the value of this token is strictly less than that of the argument token.
 long longValue()
          Return the value of this token as a long integer.
 void setUnitCategory(int index)
          Set the unit category this token belongs to.
 java.lang.String unitsString()
          Return the string representation of the units of this token.
 
Methods inherited from class ptolemy.data.Token
_notSupportedMessage, add, addReverse, convert, divide, divideReverse, equals, hashCode, isCloseTo, isCloseTo, isEqualTo, modulo, moduloReverse, multiply, multiplyReverse, one, subtract, subtractReverse, toString, zero
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_unitCategoryExponents

protected int[] _unitCategoryExponents
The unit category exponents. The unit system contains a set of base unit categories and derived categories. The base categories are customizable by the user. For example, the user may choose to use the SI unit system which has 7 base categories: length, mass, time, electric current, thermodynamic temperature, amount of substance, and luminous intensity. The customization is done by defining a MoML file to specify the categories and the units in each category. Each category has an index, assigned by the order the category appears in the MoML file. Derived units are recorded by the exponents of the category. For example, the category speed, which is length/time, is stored by an exponent of 1 for the length category, and an exponent of -1 for the time category. This array records the exponents of the base categories.
Constructor Detail

ScalarToken

public ScalarToken()
Method Detail

absolute

public abstract ScalarToken absolute()
Return a ScalarToken containing the absolute value of the value of this token. If this token contains a non-negative number, it is returned directly; otherwise, a new token is is return.
Returns:
A ScalarToken of the same type as the argument.

complexValue

public Complex complexValue()
                     throws IllegalActionException
Return the value of this token as a Complex. In this base class, we just throw an exception.
Returns:
A Complex
Throws:
IllegalActionException - Always thrown

doubleValue

public double doubleValue()
                   throws IllegalActionException
Return the value of this token as a double. In this base class, we just throw an exception.
Returns:
A double
Throws:
IllegalActionException - Always thrown

getType

public Type getType()
Return the type of this token.
Overrides:
getType in class Token
Returns:
BaseType.SCALAR

fixValue

public FixPoint fixValue()
                  throws IllegalActionException
Return the value of this token as a FixPoint. In this base class, we just throw an exception.
Returns:
A FixPoint
Throws:
IllegalActionException - Always thrown.

intValue

public int intValue()
             throws IllegalActionException
Return the value of this token as an int. In this base class, we just throw an exception.
Returns:
The value of this token.
Throws:
IllegalActionException - Always thrown.

isLessThan

public abstract BooleanToken isLessThan(ScalarToken token)
                                 throws IllegalActionException
Check whether the value of this token is strictly less than that of the argument token.
Parameters:
token - A token to compare against.
Returns:
A BooleanToken with value true if this token is strictly less than the argument.
Throws:
IllegalActionException - If the type of the argument token is incomparable with the type of this token.

inUnitsOf

public ScalarToken inUnitsOf(ScalarToken units)
                      throws IllegalActionException
Return a scalar token that contains the value of this token in the units of the argument token. The unit category of the argument token must be the same as that of this token, otherwise, an exception will be thrown. The returned token is unitless.
Parameters:
units - A scalar token that represents a unit.
Returns:
A scalar token that do not have a unit.
Throws:
IllegalActionException - If the unit category of the argument token is not the same as that of this one.

longValue

public long longValue()
               throws IllegalActionException
Return the value of this token as a long integer. In this base class, we just throw an exception.
Returns:
A long
Throws:
IllegalActionException - Always thrown.

setUnitCategory

public void setUnitCategory(int index)
Set the unit category this token belongs to.
Parameters:
The - unit category index.

unitsString

public java.lang.String unitsString()
Return the string representation of the units of this token. The general format of the returned string is "(l_1 * l_2 * ... * l_m) / (s_1 * s_2 * ... * s_n)". For example: "(meter * kilogram) / (second * second)". If m or n is 1, then the parenthesis above or below "/" is omited. For example: "meter / second". If there is no term above "/", the format becomes "1 / (s_1 * s_2 * ... * s_n)". For example: "1 / meter". If this token does not have a unit, return an empty string.
Returns:
A string representation of the units of this token.

_addCategoryExponents

protected int[] _addCategoryExponents(ScalarToken token)
Add the corresponding unit category exponents.
Parameters:
token - A token whose exponent will be added with the exponents of this token.
Returns:
An int array containing the addition result, or null if the result is unitless.

_copyOfCategoryExponents

protected int[] _copyOfCategoryExponents()
Return a copy of the unit category exponents array. If this token does not have a unit, return null;
Returns:
An int array that is a copy of the unit category exponents of this token.

_areUnitsEqual

protected boolean _areUnitsEqual(ScalarToken scalarToken)
Return true if the units of this token are the same as that of the argument token. If both tokens do not have units, return true.
Parameters:
scalarToken - A scalar token.
Returns:
True if the units of this token is the same as that of the argument token; false otherwise.

_isUnitless

protected boolean _isUnitless()
Return true if this token does not have a unit.
Returns:
True if this token does not have a unit.

_subtractCategoryExponents

protected int[] _subtractCategoryExponents(ScalarToken token)
Subtract the corresponding unit category exponents of the argument token from that of this token.
Parameters:
token - A token whose exponent will be subtracted from the exponents of this token.
Returns:
An int array containing the addition result, or null if the result is unitless.