Package ptolemy.data

Class UnionToken


  • public class UnionToken
    extends AbstractNotConvertibleToken
    A token that contains a label/token pairs. This pair is one of the choices in a union type, but the exact type is not stored in this class. Operations on union tokens may require that they have the same field in among the possible choices.
    Since:
    Ptolemy II 5.2
    Version:
    $Id$
    Author:
    Yuhong Xiong, Steve Neuendorffer, Elaine Cheong, Edward Lee
    Pt.AcceptedRating:
    Red (cxh)
    Pt.ProposedRating:
    Red (yuhong)
    • Constructor Detail

      • UnionToken

        public UnionToken​(java.lang.String label,
                          Token value)
                   throws IllegalActionException
        Construct a UnionToken with the specified label and value.
        Parameters:
        label - The label.
        value - The value of this token.
        Throws:
        IllegalActionException - If the label or the value is null.
      • UnionToken

        public UnionToken​(java.lang.String init)
                   throws IllegalActionException
        Construct a UnionToken from the specified string.
        Parameters:
        init - A string expression of a record.
        Throws:
        IllegalActionException - If the string does not contain a parsable record.
    • Method Detail

      • equals

        public boolean equals​(java.lang.Object object)
        Return true if the argument is an UnionToken with the same label and value.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        object - An instance of Object.
        Returns:
        True if the argument is equal to this token.
        See Also:
        hashCode()
      • getType

        public Type getType()
        Return the type of this token.
        Overrides:
        getType in class Token
        Returns:
        An instance of UnionType containing one field. The label of that field is the label of this token, and the type of that field is the type of the value of this token.
      • hashCode

        public int hashCode()
        Return a hash code value for this token. This method returns the hash codes of the value token.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        A hash code value for this token.
      • label

        public java.lang.String label()
        Return the label of this token.
        Returns:
        The label of this token.
      • one

        public Token one()
                  throws IllegalActionException
        Returns a new UnionToken representing the multiplicative identity. The returned token has the same label as this one, and contains the multiplicative identity of the value of this token.
        Overrides:
        one in class Token
        Returns:
        A UnionToken.
        Throws:
        IllegalActionException - If multiplicative identity is not supported by any value token.
      • toString

        public java.lang.String toString()
        Return the value of this token as a string. The syntax is {|label = value|}
        Overrides:
        toString in class Token
        Returns:
        A String beginning with "{|" that contains a label and value pair, ending with "|}".
      • value

        public Token value()
        Return the value of this token.
        Returns:
        The value of this token.
      • zero

        public Token zero()
                   throws IllegalActionException
        Returns a new UnionToken representing the additive identity. The returned token has the same label as this one, and contains the additive identity of the value token.
        Overrides:
        zero in class Token
        Returns:
        A UnionToken.
        Throws:
        IllegalActionException - If additive identity is not supported by any value token.
      • _add

        protected Token _add​(Token rightArgument)
                      throws IllegalActionException
        Return a new token whose value is the sum of this token and the argument. It is assumed that the class of the argument is UnionToken.
        Specified by:
        _add in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to add to this token.
        Returns:
        A new UnionToken.
        Throws:
        IllegalActionException - If the label of the argument and this token are different, or calling the add method on the value token throws it.
      • _divide

        protected Token _divide​(Token rightArgument)
                         throws IllegalActionException
        Return a new token whose value is the division of this token and the argument. It is assumed that the class of the argument is UnionToken.
        Specified by:
        _divide in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to divide this token by.
        Returns:
        A new UnionToken.
        Throws:
        IllegalActionException - If the label of the argument and this token are different, or calling the divide method on the value token throws it.
      • _isCloseTo

        protected BooleanToken _isCloseTo​(Token rightArgument,
                                          double epsilon)
                                   throws IllegalActionException
        Test whether 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. This method only makes sense for tokens where the distance between them is reasonably represented as a double. It is assumed that the argument is an UnionToken, and the isCloseTo() method of the fields is used. If the fields do not match, then the return value is false.
        Specified by:
        _isCloseTo in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to compare to this token.
        epsilon - The value that we use to determine whether two tokens are close.
        Returns:
        A token containing true if the value of the first argument is close to the value of this token.
        Throws:
        IllegalActionException - If thrown by calling isCloseTo() on the value of this Token.
      • _isEqualTo

        protected BooleanToken _isEqualTo​(Token rightArgument)
                                   throws IllegalActionException
        Return true if the specified token is equal to this one. Equal means that both tokens have the same label with the same value. This method is different from equals() in that _isEqualTo() looks for equalities of values irrespective of their types. It is assumed that the type of the argument is UnionToken.
        Overrides:
        _isEqualTo in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to compare to this token.
        Returns:
        True if the argument is equal to this.
        Throws:
        IllegalActionException - If this method is not supported by the derived class.
      • _modulo

        protected Token _modulo​(Token rightArgument)
                         throws IllegalActionException
        Return a new token whose value is the modulo of this token and the argument. It is assumed that the class of the argument is UnionToken.
        Specified by:
        _modulo in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to modulo this token by.
        Returns:
        A new UnionToken.
        Throws:
        IllegalActionException - If the label of the argument and this token are different, or calling the modulo method on the value token throws it.
      • _multiply

        protected Token _multiply​(Token rightArgument)
                           throws IllegalActionException
        Return a new token whose value is the multiplication of this token and the argument. It is assumed that the class of the argument is UnionToken.
        Specified by:
        _multiply in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to multiply this token by.
        Returns:
        A new UnionToken.
        Throws:
        IllegalActionException - If the label of the argument and this token are different, or calling the multiply method on the value token throws it.
      • _subtract

        protected Token _subtract​(Token rightArgument)
                           throws IllegalActionException
        Return a new token whose value is the subtraction of this token and the argument. It is assumed that the class of the argument is UnionToken.
        Specified by:
        _subtract in class AbstractNotConvertibleToken
        Parameters:
        rightArgument - The token to subtract from this token.
        Returns:
        A new UnionToken.
        Throws:
        IllegalActionException - If the label of the argument and this token are different, or calling the subtract method on the value token throws it.