ptolemy.data
Class UnsignedByteToken

java.lang.Object
  |
  +--ptolemy.data.Token
        |
        +--ptolemy.data.ScalarToken
              |
              +--ptolemy.data.UnsignedByteToken
All Implemented Interfaces:
java.io.Serializable

public class UnsignedByteToken
extends ScalarToken

A token that contains an unsigned byte number in the range 0 through 255. This is in contrast to Java's default that a byte is in the range -128 through 127. To get our desired behavior we need only apply a custom conversion unsignedConvert() from byte to integer. Conversion to byte already gives the desired behavior of truncating the value, keeping the lowest 8 bits. Thus, for example, the integers -1 and 1023 both truncate to the byte 255. Throughout the code, casts (byte) to byte occur. These are necessary because Java converts to integer or higher by default when doing arithmetic. Java does this even when the types of both operands are byte.

Since:
Ptolemy II 2.0
Version:
$Id: UnsignedByteToken.java,v 1.1.2.1 2002/05/08 01:13:37 cxh Exp $
Author:
Winthrop Williams
See Also:
Serialized Form

Fields inherited from class ptolemy.data.ScalarToken
_unitCategoryExponents
 
Constructor Summary
UnsignedByteToken()
          Construct a token with byte 0.
UnsignedByteToken(byte value)
          Construct a UnsignedByteToken with the specified byte value.
UnsignedByteToken(int value)
          Construct a UnsignedByteToken from the specified integer.
UnsignedByteToken(java.lang.String init)
          Construct a UnsignedByteToken from the specified string.
 
Method Summary
 ScalarToken absolute()
          Return a UnsignedByteToken containing the absolute value (which, for bytes, equals the value) of this token.
 Token add(Token rightArgument)
          Return a new token whose value is the sum of this token and the argument.
 Token addReverse(Token leftArgument)
          Return a new token whose value is the sum of this token and the argument.
 byte byteValue()
          Return the value in the token as a byte.
 Complex complexValue()
          Return the value of this token as a Complex.
static Token convert(Token token)
          Convert the specified token into an instance of UnsignedByteToken.
 Token divide(Token divisor)
          Return a new token whose value is the value of this token divided by the value of the argument token.
 Token divideReverse(Token dividend)
          Return a new token whose value is the value of the argument token divided by the value of this token.
 double doubleValue()
          Return the value in the token as a double.
 boolean equals(java.lang.Object object)
          Return true if the argument is an instance of UnsignedByteToken with the same value.
 Type getType()
          Return the type of this token.
 int hashCode()
          Return a hash code value for this token.
 int intValue()
          Return the value in the token as an int.
 BooleanToken isEqualTo(Token token)
          Test the value and units of this token and the argument token for equality.
 BooleanToken isLessThan(ScalarToken token)
          Check if the value of this token is strictly less than that of the argument token.
 long longValue()
          Return the value in the token as a long.
 Token modulo(Token rightArgument)
          Return a new token whose value is the value of this token modulo the value of the argument token.
 Token moduloReverse(Token leftArgument)
          Return a new token whose value is the value of the argument token modulo the value of this token.
 Token multiply(Token rightFactor)
          Return a new token whose value is the value of this token multiplied by the value of the argument token.
 Token multiplyReverse(Token leftFactor)
          Return a new token whose value is the value of the argument token multiplied by the value of this token.
 Token one()
          Returns a new UnsignedByteToken with value 1.
 Token subtract(Token rightArgument)
          Return a new token whose value is the value of the argument token subtracted from the value of this token.
 Token subtractReverse(Token leftArgument)
          Return a new token whose value is the value of this token subtracted from the value of the argument token.
 java.lang.String toString()
          Return the value of this token as a string that can be parsed by the expression language to recover a token with the same value.
 int unsignedConvert(byte value)
          Convert the given unsigned byte to an integer.
 Token zero()
          Returns a new UnsignedByteToken with value 0.
 
Methods inherited from class ptolemy.data.ScalarToken
_addCategoryExponents, _areUnitsEqual, _copyOfCategoryExponents, _isUnitless, _subtractCategoryExponents, fixValue, inUnitsOf, setUnitCategory, unitsString
 
Methods inherited from class ptolemy.data.Token
_notSupportedMessage, isCloseTo, isCloseTo
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UnsignedByteToken

public UnsignedByteToken()
Construct a token with byte 0.

UnsignedByteToken

public UnsignedByteToken(byte value)
Construct a UnsignedByteToken with the specified byte value.

UnsignedByteToken

public UnsignedByteToken(int value)
Construct a UnsignedByteToken from the specified integer. This takes the low 8 bits and discards the rest. Having this form (which takes an integer) in addition to the one above (which takes a byte) avoids us having to cast to byte when calling constructors such as those called from within the one() and zero() methods.

UnsignedByteToken

public UnsignedByteToken(java.lang.String init)
                  throws IllegalActionException
Construct a UnsignedByteToken from the specified string.
Throws:
IllegalActionException - If the token could not be created from the given string.
Method Detail

absolute

public ScalarToken absolute()
Return a UnsignedByteToken containing the absolute value (which, for bytes, equals the value) of this token. Since we have defined the byte as ranging from 0 through 255, its absolute value is equal to itself. Although this method does not actually do anything, it is included to make the UnsignedByteToken interface compatible with the interfaces of the other types.
Overrides:
absolute in class ScalarToken
Returns:
An UnsignedByteToken.

add

public Token add(Token rightArgument)
          throws IllegalActionException
Return a new token whose value is the sum of this token and the argument. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
add in class Token
Parameters:
rightArgument - The token to add to this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be added to this token, or the units of this token and the argument token are not the same.

addReverse

public Token addReverse(Token leftArgument)
                 throws IllegalActionException
Return a new token whose value is the sum of this token and the argument. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
addReverse in class Token
Parameters:
leftArgument - The token to add this token to.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be added to this token, or the units of this token and the argument token are not the same.

complexValue

public Complex complexValue()
Return the value of this token as a Complex. The real part of the Complex is the value of this token, the imaginary part is set to 0.
Overrides:
complexValue in class ScalarToken
Returns:
A Complex.

convert

public static Token convert(Token token)
                     throws IllegalActionException
Convert the specified token into an instance of UnsignedByteToken. This method does lossless conversion, or throws an exception if it cannot. If the argument is already an instance of UnsignedByteToken, it is returned without any change. Otherwise, if the argument is below UnsignedByteToken in the type hierarchy, it is converted to an instance of UnsignedByteToken or one of the subclasses of UnsignedByteToken and returned. If none of the above condition is met, an exception is thrown.
Parameters:
token - The token to be converted to a UnsignedByteToken.
Returns:
A UnsignedByteToken.
Throws:
IllegalActionException - If the conversion cannot be carried out.

divide

public Token divide(Token divisor)
             throws IllegalActionException
Return a new token whose value is the value of this token divided by the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion. If two bytes are divided, then the result will be a byte containing the quotient. For example, 255 divided by 10 returns 25.
Overrides:
divide in class Token
Parameters:
divisor - The token to divide this token by
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is of a type that cannot divide this token.

divideReverse

public Token divideReverse(Token dividend)
                    throws IllegalActionException
Return a new token whose value is the value of the argument token divided by the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
divideReverse in class Token
Parameters:
dividend - The token to be divided by the value of this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be divided by this token's value.

doubleValue

public double doubleValue()
Return the value in the token as a double.
Overrides:
doubleValue in class ScalarToken
Returns:
The value contained in this token as a double.

equals

public boolean equals(java.lang.Object object)
Return true if the argument is an instance of UnsignedByteToken with the same value.
Overrides:
equals in class Token
Parameters:
object - An instance of Object.
Returns:
True if the argument is an instance of UnsignedByteToken with the same value.

getType

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

hashCode

public int hashCode()
Return a hash code value for this token. This method just returns the value of this token.
Overrides:
hashCode in class Token
Returns:
A hash code value for this token.

isEqualTo

public BooleanToken isEqualTo(Token token)
                       throws IllegalActionException
Test the value and units of this token and the argument token for equality. Units need not match, this merely causes the test to return false. Type resolution also occurs here, and lossless conversion is performed prior to comparison. The returned type is boolean, regardless of the types compared.
Overrides:
isEqualTo in class Token
Parameters:
token - The token with which to test equality.
Returns:
a boolean token that contains the value true if both the value and the units of this token are equal to those of the argument token.
Throws:
IllegalActionException - If the argument token is not of a type that can be compared with this token.

isLessThan

public BooleanToken isLessThan(ScalarToken token)
                        throws IllegalActionException
Check if the value of this token is strictly less than that of the argument token. Units must match or an exception is thrown.
Overrides:
isLessThan in class ScalarToken
Parameters:
token - A ScalarToken.
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, or the units of this token and the argument are not the same.

byteValue

public byte byteValue()
Return the value in the token as a byte.
Returns:
The byte value contained in this token.

intValue

public int intValue()
Return the value in the token as an int.
Overrides:
intValue in class ScalarToken
Returns:
The byte value contained in this token as a int.

longValue

public long longValue()
Return the value in the token as a long.
Overrides:
longValue in class ScalarToken
Returns:
The byte value contained in this token as a long.

modulo

public Token modulo(Token rightArgument)
             throws IllegalActionException
Return a new token whose value is the value of this token modulo the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
modulo in class Token
Parameters:
rightArgument - The token to modulo this token by.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be used with modulo, or the units of this token and the argument token are not the same.

moduloReverse

public Token moduloReverse(Token leftArgument)
                    throws IllegalActionException
Return a new token whose value is the value of the argument token modulo the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
moduloReverse in class Token
Parameters:
leftArgument - The token to apply modulo to by the value of this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can apply modulo by this token, or if the units of this token are not the same as those of the argument.

multiply

public Token multiply(Token rightFactor)
               throws IllegalActionException
Return a new token whose value is the value of this token multiplied by the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
multiply in class Token
Parameters:
rightFactor - The token to multiply this token by.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be multiplied to this token.

multiplyReverse

public Token multiplyReverse(Token leftFactor)
                      throws IllegalActionException
Return a new token whose value is the value of the argument token multiplied by the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
multiplyReverse in class Token
Parameters:
leftFactor - The token to be multiplied by the value of this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be multiplied by this token.

one

public Token one()
Returns a new UnsignedByteToken with value 1.
Overrides:
one in class Token
Returns:
A new UnsignedByteToken with value 1.

subtract

public Token subtract(Token rightArgument)
               throws IllegalActionException
Return a new token whose value is the value of the argument token subtracted from the value of this token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
subtract in class Token
Parameters:
rightArgument - The token to subtract from this token.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be subtracted from this token, or the units of this token and the argument token are not the same.

subtractReverse

public Token subtractReverse(Token leftArgument)
                      throws IllegalActionException
Return a new token whose value is the value of this token subtracted from the value of the argument token. Type resolution also occurs here, with the returned token type chosen to achieve a lossless conversion.
Overrides:
subtractReverse in class Token
Parameters:
leftArgument - The token to subtract this token from.
Returns:
A new token containing the result.
Throws:
IllegalActionException - If the argument token is not of a type that can be added to this token, or the units of this token and the argument token are not the same. FIXME: byte (and int too) have less code in the subtractReverse() method. Something is not being handled here. CPO is not even being checked!!

toString

public java.lang.String toString()
Return the value of this token as a string that can be parsed by the expression language to recover a token with the same value. If this token has a unit, the return string also includes a unit string produced by the unitsString() method in the super class.
Overrides:
toString in class Token
Returns:
A String representing the byte value and the units (if any) of this token.
See Also:
ScalarToken.unitsString()

unsignedConvert

public int unsignedConvert(byte value)
Convert the given unsigned byte to an integer. This is different from the default (int) conversion. The default (int) conversion yields negative values from bytes whose high bit is true. This is necessary because Java, by default, interprets bytes as signed numbers. We are interested in unsigned bytes. Conversion from integers to bytes requires no hand coding. This is because Java converts to bytes by keeping just the least significant 8 bits. It pays no attention to sign in that conversion.
Parameters:
byte - The byte to convert.
Returns:
An integer between 0 and 255.

zero

public Token zero()
Returns a new UnsignedByteToken with value 0.
Overrides:
zero in class Token
Returns:
A new UnsignedByteToken with value 0.