Package ptolemy.data

Class OrderedRecordToken


  • public class OrderedRecordToken
    extends RecordToken
    A token that contains a set of label/token pairs. Record labels may be arbitrary strings. Operations on record tokens result in new record tokens containing only the common fields, where the operation specifies how to combine the data in the common fields. Thus, for example, if two record tokens are added or subtracted, then common records (those with the same labels) will be added or subtracted, and the disjoint records will not appear in the result.

    This implementation maintains the order of the entries as they were added.

    Since:
    Ptolemy II 8.0
    Version:
    $Id$, $Id$
    Author:
    Ben Leinfelder
    Pt.AcceptedRating:
    red (leinfelder)
    Pt.ProposedRating:
    yellow (leinfelder)
    • Constructor Detail

      • OrderedRecordToken

        public OrderedRecordToken()
        Construct an OrderedRecordToke with now fields.
        See Also:
        RecordToken
      • OrderedRecordToken

        public OrderedRecordToken​(java.util.Map<java.lang.String,​Token> fieldMap)
                           throws IllegalActionException
        Construct an OrderedRecordToken with the labels and values specified by a given Map object. The object cannot contain any null keys or values.
        Parameters:
        fieldMap - A Map that has keys of type String and values of type Token.
        Throws:
        IllegalActionException - If the map contains null keys or values, or if it contains non-String keys or non-Token values.
      • OrderedRecordToken

        public OrderedRecordToken​(java.lang.String init)
                           throws IllegalActionException
        Construct a RecordToken from the specified string.

        Record labels that contain any non-Java identifier characters must be presented as a string i.e., surrounded with single or double quotes. Quotes within label strings must be escaped using a backslash.

        Parameters:
        init - A string expression of a record.
        Throws:
        IllegalActionException - If the string does not contain a parsable record.
      • OrderedRecordToken

        public OrderedRecordToken​(java.lang.String[] labels,
                                  Token[] values)
                           throws IllegalActionException
        Construct an OrderedRecordToken with the specified labels and values. The labels and values arrays must have the same length, and have one to one correspondence with each other. That is, the i'th entry in the labels array is the label for the i'th value in the values array. If both arrays are empty, this creates an empty record token.
        Parameters:
        labels - An array of labels.
        values - An array of Tokens.
        Throws:
        IllegalActionException - If the labels or the values array do not have the same length, or contains null element, or the labels array contains duplicate elements.
    • Method Detail

      • equals

        public boolean equals​(java.lang.Object object)
        Return true if the class of the argument is RecordToken, and the argument has the same set of labels as this token and the corresponding fields are equal, as determined by the equals method of the contained tokens. Order matters
        Overrides:
        equals in class RecordToken
        Parameters:
        object - An instance of Object.
        Returns:
        True if the argument is equal to this token.
        See Also:
        hashCode()
      • hashCode

        public int hashCode()
        Return a hash code value for this token. This method returns the xor of the hash codes of the labels and the element tokens.
        Overrides:
        hashCode in class RecordToken
        Returns:
        A hash code value for this token.
      • toString

        public java.lang.String toString()
        Return the value of this token as a string. The syntax is similar to that of a record, but using square braces instead of curly braces, [label = value, label = value, ...] The record fields are listed in the their original order

        Record labels that contain any non-Java identifier characters are surrounded with double quotes. Quotes within label strings are escaped using a backslash.

        Overrides:
        toString in class RecordToken
        Returns:
        A String beginning with "[" that contains label and value pairs separated by commas, ending with "]".
      • _createRecordToken

        protected RecordToken _createRecordToken​(java.lang.String[] labels,
                                                 Token[] values)
                                          throws IllegalActionException
        Description copied from class: RecordToken
        Create a new RecordToken. Subclasses of RecordToken may return a different subclass instance.
        Overrides:
        _createRecordToken in class RecordToken
        Parameters:
        labels - An array of String labels for the RecordToken to be created.
        values - An array of Token values for the RecordToken to be created.
        Returns:
        a new RecordToken.
        Throws:
        IllegalActionException - If thrown while constructing the RecordToken
        See Also:
        RecordToken
      • _initializeStorage

        protected void _initializeStorage()
        Initialize the storage used by this token. OrderedRecordToken uses a LinkedHashMap so that the original order of the record is maintained.
        Overrides:
        _initializeStorage in class RecordToken
      • _createSet

        protected java.util.Set<java.lang.String> _createSet()
        Create a Set implementation appropriate for operations on this RecordToken. Here we are using an ordered set.
        Overrides:
        _createSet in class RecordToken
        Returns:
        a new Set.
      • _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 RecordToken, and the isCloseTo() method of the fields is used. If the fields do not match, then the return value is false.
        Overrides:
        _isCloseTo in class RecordToken
        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 throw while checking the closeness of an element of the record.
      • _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 labels with the same values. 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 RecordToken.
        Overrides:
        _isEqualTo in class RecordToken
        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.