public class Token
extends java.lang.Object
Generally, it is painful to implement both the operation and operationReverse methods of this class. It is also painful to implement tokens that are automatically converted to other tokens in a consistent fashion. As such, there are several subclasses of this class that implement these methods and provide a somewhat nicer abstraction. The ScalarToken derived class is useful for many types that are losslessly convertible to other types and may be associated with units, such as IntToken. The MatrixToken derived class is useful for implementing matrices of ScalarTokens, such as IntMatrixToken. The AbstractNotConvertible derived class is useful for implementing tokens that are not losslessly convertible to a token in implemented in another class, such as ArrayToken. Lastly, AbstractConvertibleToken is useful for implementing tokens that are losslessly convertible to a token in another class, but don't have units, such as BooleanToken.
Instances of this base class *should not* be used to represent pure events, i.e., to indicate that an event is present. To represent pure events, it is better to use the EventToken class. The reasoning is that the type BaseType.GENERAL is reserved to represent types which the type system cannot represent exactly. Using the EventToken class, and the type BaseType.EVENT allows typesafe use of pure events.
Nil, null or missing tokens are common in analytical systems like R and SAS
where they are used to handle sparsely populated data sources.
This class has support for such tokens, see NIL
for details.
ScalarToken
,
AbstractConvertibleToken
,
AbstractNotConvertibleToken
,
MatrixToken
Red (cxh) nil token code |
Yellow (cxh) |
Modifier and Type | Field and Description |
---|---|
static Token |
NIL
A token that represents a missing value.
|
Constructor and Description |
---|
Token()
Create a Token.
|
Modifier and Type | Method and Description |
---|---|
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.
|
Token |
divide(Token rightArgument)
Return a new token whose value is the value of this token
divided by the value of the argument token.
|
Token |
divideReverse(Token leftArgument)
Return a new token whose value is the value of the argument
token divided by the value of this token.
|
Type |
getType()
Return the type of this token.
|
BooleanToken |
isCloseTo(Token token)
Test that the value of this Token is close to the argument
Token.
|
BooleanToken |
isCloseTo(Token token,
double epsilon)
Test that the value of this Token is close to the first argument,
where "close" means that the distance between them is less than
or equal to the second argument.
|
BooleanToken |
isEqualTo(Token rightArgument)
Test for equality of the values of this Token and the argument
Token.
|
boolean |
isNil()
Return true if the token is nil, (aka null or missing).
|
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 rightArgument)
Return a new token whose value is the value of this token
multiplied by the value of the argument token.
|
Token |
multiplyReverse(Token leftArgument)
Return a new token whose value is the value of the argument
token multiplied by the value of this token.
|
static java.lang.String |
notSupportedConversionMessage(Token token,
java.lang.String typeString)
Return a string with an error message that states that
the given token cannot be converted to the given token type.
|
static java.lang.String |
notSupportedIncomparableConversionMessage(Token token,
java.lang.String typeString)
Return a string with an error message that states that
the given token cannot be converted to the given token type.
|
static java.lang.String |
notSupportedIncomparableMessage(java.lang.String operation,
Token firstToken,
Token secondToken)
Return a string with an error message that states that the
given operation is not supported between two tokens, because
they have incomparable types and cannot be converted to the
same type.
|
static java.lang.String |
notSupportedMessage(java.lang.String operation,
Token firstToken,
Token secondToken)
Return a string with an error message that states that the
given operation is not supported between two tokens.
|
static java.lang.String |
notSupportedNullNilStringMessage(java.lang.String type,
java.lang.String init)
Return a string with an error message that states that
the token cannot be created with a string that is null
or the value of the init parameter.
|
Token |
one()
Returns a new Token representing the multiplicative identity.
|
Token |
pow(int times)
Return a new token computed as follows:
For positive times arguments, the result represents the product of this token multiplied by itself the number of times given by the argument. |
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.
|
Token |
zero()
Returns a new token representing the additive identity.
|
static Type |
zeroReturnType(Type type)
Return the (exact) return type of the zero function above.
|
public static final Token NIL
public Token add(Token rightArgument) throws IllegalActionException
rightArgument
- The token to add to this token.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token addReverse(Token leftArgument) throws IllegalActionException
leftArgument
- The token to add this token to.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token divide(Token rightArgument) throws IllegalActionException
rightArgument
- The token to divide into this token.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token divideReverse(Token leftArgument) throws IllegalActionException
leftArgument
- The token to be divided by the value of
this token.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Type getType()
public final BooleanToken isCloseTo(Token token) throws IllegalActionException
token
- The token to test closeness of this token with.IllegalActionException
- If the argument token is not
of a type that can be compared with this token.isEqualTo(ptolemy.data.Token)
public BooleanToken isCloseTo(Token token, double epsilon) throws IllegalActionException
token
- The token to test closeness of this token with.epsilon
- The value that we use to determine whether two
tokens are close.IllegalActionException
- If the argument token is not
of a type that can be compared with this token.public BooleanToken isEqualTo(Token rightArgument) throws IllegalActionException
rightArgument
- The token with which to test equality.IllegalActionException
- If the argument token is not
of a type that can be compared with this token.public boolean isNil()
NIL
.public Token modulo(Token rightArgument) throws IllegalActionException
rightArgument
- The token to divide into this token.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token moduloReverse(Token leftArgument) throws IllegalActionException
leftArgument
- The token to apply modulo to by the value
of this token.
If either this token or the argument token is a nil token, then
NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token multiply(Token rightArgument) throws IllegalActionException
rightArgument
- The token to multiply this token by.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token multiplyReverse(Token leftArgument) throws IllegalActionException
leftArgument
- The token to be multiplied by the value of
this token.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public static java.lang.String notSupportedConversionMessage(Token token, java.lang.String typeString)
token
- The token being converted.typeString
- A string representing the type that is being
converted to.public static java.lang.String notSupportedIncomparableConversionMessage(Token token, java.lang.String typeString)
token
- The token being converted.typeString
- A string representing the type that is being
converted to.public static java.lang.String notSupportedIncomparableMessage(java.lang.String operation, Token firstToken, Token secondToken)
operation
- A string naming the unsupported token
operation.firstToken
- The first token in the message.secondToken
- The second token in the message.public static java.lang.String notSupportedMessage(java.lang.String operation, Token firstToken, Token secondToken)
operation
- A string naming the unsupported token
operation.firstToken
- The first token in the message.secondToken
- The second token in the message.public Token one() throws IllegalActionException
NIL
is returned.IllegalActionException
- If this method is not
supported by the derived class.public Token pow(int times) throws IllegalActionException
The token type returned by this method is the same as the type of this token. Note that the method is different from java.lang.Math.pow(), since it returns an integer given an integer token type, and is also well defined for matrix types.
times
- The number of times to multiply.NIL
is returned.IllegalActionException
- If the token is not
compatible for this operation. Specifically, if the Token
type does not support division (for example matrices) then
using a negative times argument may throw an exception.public Token subtract(Token rightArgument) throws IllegalActionException
rightArgument
- The token to subtract from this token.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public Token subtractReverse(Token leftArgument) throws IllegalActionException
leftArgument
- The token to subtract this token from.NIL
is returned.IllegalActionException
- If the argument token and
this token are of incomparable types, or the operation does
not make sense for the given types.public java.lang.String toString()
NIL
then
return "nil"toString
in class java.lang.Object
NIL
,
in which case return the String "nil".public Token zero() throws IllegalActionException
NIL
is returned.IllegalActionException
- If this method is not
supported by the derived class.public static Type zeroReturnType(Type type)
type
- The type of the argument to the corresponding function.public static java.lang.String notSupportedNullNilStringMessage(java.lang.String type, java.lang.String init)
type
- The type we are trying to create.init
- The initialization string.