ptolemy.data
Class BooleanToken

java.lang.Object
  extended by ptolemy.data.Token
      extended by ptolemy.data.ScalarToken
          extended by ptolemy.data.BooleanToken
All Implemented Interfaces:
java.io.Serializable, BitwiseOperationToken

public class BooleanToken
extends ScalarToken

A token that contains a boolean variable. Arithmetic on booleans is that of a two-element Galois field (modulo two arithmetic). Thus, add() is logical xor, multiply() is logical and, zero() is false, one() is true.

In order to reduce the number of instances of this object that are created, it is highly recommended that the getInstance() method be used, instead of the constructor that takes a boolean argument.

Since:
Ptolemy II 0.2
Version:
$Id: BooleanToken.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Neil Smyth, Yuhong Xiong, Edward A. Lee, Steve Neuendorffer, Christopher Brooks
See Also:
Serialized Form
Accepted Rating:
Red (cxh)
Proposed Rating:
Yellow (cxh) nil token

Field Summary
private  boolean _value
           
static BooleanToken FALSE
          False-valued token.
static BooleanToken NIL
          A token that represents a missing value.
static BooleanToken TRUE
          True-valued token.
 
Fields inherited from class ptolemy.data.ScalarToken
_unitCategoryExponents
 
Constructor Summary
BooleanToken()
          Construct a token with value false.
BooleanToken(boolean b)
          Construct a token with the specified value.
BooleanToken(java.lang.String init)
          Construct a token with the specified string.
 
Method Summary
protected  ScalarToken _absolute()
          Return this token.
protected  ScalarToken _add(ScalarToken rightArgument)
          Return a new token whose value is the value of the argument Token added to the value of this Token.
protected  ScalarToken _bitwiseAnd(ScalarToken rightArgument)
          Returns a token representing the bitwise AND of this token and the given token.
protected  ScalarToken _bitwiseNot()
          Returns a token representing the bitwise NOT of this token.
protected  ScalarToken _bitwiseOr(ScalarToken rightArgument)
          Returns a token representing the bitwise OR of this token and the given token.
protected  ScalarToken _bitwiseXor(ScalarToken rightArgument)
          Returns a token representing the bitwise XOR of this token and the given token.
protected  ScalarToken _divide(ScalarToken rightArgument)
          Return a new token whose value is the value of this token divided by the value of this Token.
protected  BooleanToken _isCloseTo(ScalarToken token, double epsilon)
          Return a true-valued token if the first argument is close to this token, where in this class, "close" means "identical to."
protected  BooleanToken _isEqualTo(ScalarToken token)
          Test for equality of the values of this token and the argument.
protected  BooleanToken _isLessThan(ScalarToken rightArgument)
          Throw an exception.
protected  ScalarToken _modulo(ScalarToken rightArgument)
          Throw an exception.
protected  ScalarToken _multiply(ScalarToken rightArgument)
          Return a new token whose value is the value of this token multiplied by the value of the argument token.
protected  ScalarToken _subtract(ScalarToken rightArgument)
          Subtraction is not supported in Boolean algebras.
 BooleanToken and(BooleanToken rightArgument)
          Return a new token whose value is the logical AND of the value of this token and the the value of the argument token.
 boolean booleanValue()
          Return the value as a boolean.
static BooleanToken convert(Token token)
          Convert the specified token into an instance of BooleanToken.
 boolean equals(java.lang.Object object)
          Return true if the argument's class is BooleanToken and it has the same values as this token.
static BooleanToken getInstance(boolean value)
          Return the instance of this class corresponding to the given boolean value.
 Type getType()
          Return the type of this token.
 int hashCode()
          Return a hash code value for this token.
 boolean isNil()
          Return true if the token is nil, (aka null or missing).
 BooleanToken not()
          Return a new BooleanToken with the logical not of the value stored in this token.
 Token one()
          Returns a token representing the multiplicative identity.
 BooleanToken or(BooleanToken rightArgument)
          Return a new token whose value is the logical OR of the value of this token and the 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.
 BooleanToken xor(BooleanToken rightArgument)
          Return a new token whose value is the logical XOR of the value of this token and the the value of the argument token.
 Token zero()
          Returns a token representing the additive identity.
 
Methods inherited from class ptolemy.data.ScalarToken
_addCategoryExponents, _areUnitsEqual, _copyOfCategoryExponents, _isUnitless, _subtractCategoryExponents, absolute, add, addReverse, bitwiseAnd, bitwiseNot, bitwiseOr, bitwiseXor, byteValue, complexValue, divide, divideReverse, doubleValue, fixValue, floatValue, intValue, inUnitsOf, isCloseTo, isEqualTo, isGreaterThan, isLessThan, leftShift, logicalRightShift, longValue, modulo, moduloReverse, multiply, multiplyReverse, rightShift, setUnitCategory, shortValue, subtract, subtractReverse, unitsString
 
Methods inherited from class ptolemy.data.Token
isCloseTo, notSupportedConversionMessage, notSupportedIncomparableConversionMessage, notSupportedIncomparableMessage, notSupportedMessage, notSupportedNullNilStringMessage, pow, zeroReturnType
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

TRUE

public static final BooleanToken TRUE
True-valued token.


NIL

public static final BooleanToken NIL
A token that represents a missing value. Null or missing tokens are common in analytical systems like R and SAS where they are used to handle sparsely populated data sources. In database parlance, missing tokens are sometimes called null tokens. Since null is a Java keyword, we use the term "nil". The toString() method on a nil token returns the string "nil".


FALSE

public static final BooleanToken FALSE
False-valued token.


_value

private boolean _value
Constructor Detail

BooleanToken

public BooleanToken()
Construct a token with value false.


BooleanToken

public BooleanToken(boolean b)
Construct a token with the specified value.

Parameters:
b - The boolean value.

BooleanToken

public BooleanToken(java.lang.String init)
             throws IllegalActionException
Construct a token with the specified string. The initialization string is converted to lower case and compared to the string "true".

Parameters:
init - The initialization string.
Throws:
IllegalActionException - If the token could not be created with the given String.
Method Detail

and

public BooleanToken and(BooleanToken rightArgument)
Return a new token whose value is the logical AND of the value of this token and the the value of the argument token. If this token is nil, or the argument is nil, then NIL is returned.

Parameters:
rightArgument - The BooleanToken to OR with this one.
Returns:
A new BooleanToken containing the result.

booleanValue

public boolean booleanValue()
Return the value as a boolean.

Returns:
The value.

convert

public static BooleanToken convert(Token token)
                            throws IllegalActionException
Convert the specified token into an instance of BooleanToken. This method does lossless conversion, which in the case of booleans, means that the argument can only be already an instance of BooleanToken. It is returned unchanged. If the argument is a nil token, then NIL is returned.

Parameters:
token - The token to be converted to a BooleanToken.
Returns:
A BooleanToken.
Throws:
IllegalActionException - If the argument is not a BooleanToken.

equals

public boolean equals(java.lang.Object object)
Return true if the argument's class is BooleanToken and it has the same values as this token.

Overrides:
equals in class java.lang.Object
Parameters:
object - An instance of Object.
Returns:
True if the argument is a BooleanToken with the same value. If either this object or the argument is a nil Token, return false.

getInstance

public static BooleanToken getInstance(boolean value)
Return the instance of this class corresponding to the given boolean value.

Parameters:
value - The given value.
Returns:
BooleanToken.TRUE if the argument is true, or BooleanToken.FALSE otherwise.

getType

public Type getType()
Return the type of this token.

Specified by:
getType in class ScalarToken
Returns:
BaseType.BOOLEAN

hashCode

public int hashCode()
Return a hash code value for this token. This method returns 1 if this token has value true, and 0 if this token has value false.

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code value for this token.

isNil

public boolean isNil()
Return true if the token is nil, (aka null or missing). Nil or missing tokens occur when a data source is sparsely populated.

Overrides:
isNil in class Token
Returns:
True if the token is the NIL token.

not

public BooleanToken not()
Return a new BooleanToken with the logical not of the value stored in this token.

Returns:
The logical converse of this token.

one

public Token one()
Returns a token representing the multiplicative identity.

Overrides:
one in class Token
Returns:
TRUE.

or

public BooleanToken or(BooleanToken rightArgument)
Return a new token whose value is the logical OR of the value of this token and the the value of the argument token. If this token is nil, or the argument is nil, then NIL is returned.

Parameters:
rightArgument - The BooleanToken to OR with this one.
Returns:
A new BooleanToken containing the result.

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.

Overrides:
toString in class Token
Returns:
The string "true" if this token represents true, or the string "false" if it represents false.

xor

public BooleanToken xor(BooleanToken rightArgument)
Return a new token whose value is the logical XOR of the value of this token and the the value of the argument token. If this token is nil, or the argument is nil, then NIL is returned.

Parameters:
rightArgument - The BooleanToken to XOR with this one.
Returns:
A new BooleanToken containing the result.

zero

public Token zero()
Returns a token representing the additive identity.

Overrides:
zero in class Token
Returns:
FALSE.

_absolute

protected ScalarToken _absolute()
Return this token.

Specified by:
_absolute in class ScalarToken
Returns:
This token.

_add

protected ScalarToken _add(ScalarToken rightArgument)
Return a new token whose value is the value of the argument Token added to the value of this Token. It is assumed that the type of the argument is an BooleanToken.

Specified by:
_add in class ScalarToken
Parameters:
rightArgument - The token to add to this token.
Returns:
A new BooleanToken containing the result.

_bitwiseAnd

protected ScalarToken _bitwiseAnd(ScalarToken rightArgument)
                           throws IllegalActionException
Returns a token representing the bitwise AND of this token and the given token. This is the same as and().

Specified by:
_bitwiseAnd in class ScalarToken
Parameters:
rightArgument - The DoubleToken to bitwise AND with this one.
Returns:
The bitwise AND.
Throws:
IllegalActionException - Always thrown by this base class.

_bitwiseNot

protected ScalarToken _bitwiseNot()
                           throws IllegalActionException
Returns a token representing the bitwise NOT of this token. This is the same as not().

Specified by:
_bitwiseNot in class ScalarToken
Returns:
The bitwise NOT of this token.
Throws:
IllegalActionException - Always thrown by this base class.

_bitwiseOr

protected ScalarToken _bitwiseOr(ScalarToken rightArgument)
                          throws IllegalActionException
Returns a token representing the bitwise OR of this token and the given token. This is the same as or().

Specified by:
_bitwiseOr in class ScalarToken
Parameters:
rightArgument - The DoubleToken to bitwise OR with this one.
Returns:
The bitwise OR.
Throws:
IllegalActionException - Always thrown by this base class.

_bitwiseXor

protected ScalarToken _bitwiseXor(ScalarToken rightArgument)
                           throws IllegalActionException
Returns a token representing the bitwise XOR of this token and the given token. This is the same as xor().

Specified by:
_bitwiseXor in class ScalarToken
Parameters:
rightArgument - The DoubleToken to bitwise XOR with this one.
Returns:
The bitwise XOR.
Throws:
IllegalActionException - Always thrown by this base class.

_divide

protected ScalarToken _divide(ScalarToken rightArgument)
                       throws IllegalActionException
Return a new token whose value is the value of this token divided by the value of this Token. It is assumed that the type of the argument is an BooleanToken. For booleans, division is defined by multiplication (which is logical and). Thus, if c = a/b then c is defined so that cb = a. If b is false then this result is not well defined, so this method will throw an exception. Specifically, if the argument is true, then this method returns this token. Otherwise it throws an exception.

Specified by:
_divide in class ScalarToken
Parameters:
rightArgument - The token to divide this token by
Returns:
A new BooleanToken containing the result.
Throws:
IllegalActionException - If the argument token is FALSE.

_isCloseTo

protected BooleanToken _isCloseTo(ScalarToken token,
                                  double epsilon)
Return a true-valued token if the first argument is close to this token, where in this class, "close" means "identical to." It is assumed that the type of the argument is BooleanToken.

Specified by:
_isCloseTo in class ScalarToken
Parameters:
token - The token to compare to this token.
epsilon - Ignored in this method.
Returns:
A token containing the result.

_isEqualTo

protected BooleanToken _isEqualTo(ScalarToken token)
Test for equality of the values of this token and the argument. This is the same as equals(), except that it returns a BooleanToken instead of a boolean.

Overrides:
_isEqualTo in class ScalarToken
Parameters:
token - The token to compare to this token.
Returns:
A token containing the result. If either this object or the argument is a nil Token, return false.

_isLessThan

protected BooleanToken _isLessThan(ScalarToken rightArgument)
                            throws IllegalActionException
Throw an exception.

Specified by:
_isLessThan in class ScalarToken
Parameters:
rightArgument - The token to compare to this token.
Returns:
Does not return.
Throws:
IllegalActionException - Always.

_modulo

protected ScalarToken _modulo(ScalarToken rightArgument)
                       throws IllegalActionException
Throw an exception. This method is not supported.

Specified by:
_modulo in class ScalarToken
Parameters:
rightArgument - The token to modulo this token by.
Returns:
Does not return.
Throws:
IllegalActionException - Always.

_multiply

protected ScalarToken _multiply(ScalarToken rightArgument)
                         throws IllegalActionException
Return a new token whose value is the value of this token multiplied by the value of the argument token. It is assumed that the type of the argument is an BooleanToken. For booleans, this corresponds to the logical AND.

Specified by:
_multiply in class ScalarToken
Parameters:
rightArgument - The token to multiply this token by.
Returns:
A new BooleanToken containing the result.
Throws:
IllegalActionException - If ANDing the argument and the token throws it.

_subtract

protected ScalarToken _subtract(ScalarToken rightArgument)
                         throws IllegalActionException
Subtraction is not supported in Boolean algebras.

Specified by:
_subtract in class ScalarToken
Parameters:
rightArgument - The token to subtract from this token.
Returns:
A new Token containing the result.
Throws:
IllegalActionException - If this method is not supported by the derived class.