ptolemy.data.type
Class RecordType

java.lang.Object
  extended by ptolemy.data.type.StructuredType
      extended by ptolemy.data.type.RecordType
All Implemented Interfaces:
java.lang.Cloneable, Type
Direct Known Subclasses:
LocationType

public class RecordType
extends StructuredType
implements java.lang.Cloneable

A class representing the type of a RecordToken. To set the type of a typeable object (such as a port or parameter) to a record with particular fields, create an instance of this class and call setTypeEquals() with that instance as an argument.

Note that a record type with more fields is a subtype of a record type with a subset of the fields. For example, {x = double, y = int} is a subtype of {x = double}. When a record of type {x = double, y = int} is converted to one of type {x = double}, the extra field is discarded. The converted record, therefore, will have exactly the fields in the type.

A consequence of this is that all record types are subtypes of the empty record type. Hence, to require that a typeable object be a record type without specifying what the fields are, use

 typeable.setTypeAtMost(new RecordType(new String[0], new Type[0]));
 
Note, however, that by itself this type constraint will not be useful because it does not, by itself, prevent the type from resolving to unknown (the unknown type is at the bottom of the type lattice, and hence satisfies this type constraint).

Since:
Ptolemy II 3.0
Version:
$Id: RecordType.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Yuhong Xiong, Elaine Cheong and Steve Neuendorffer; contributor: J. S. Senecal
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (neuendor)

Nested Class Summary
private  class RecordType.FieldType
           
 
Field Summary
private  java.util.Map _fields
           
static RecordType EMPTY_RECORD
          An empty record.
 
Fields inherited from class ptolemy.data.type.StructuredType
MAXDEPTHBOUND
 
Fields inherited from interface ptolemy.data.type.Type
HASH_INVALID, HASH_MAX
 
Constructor Summary
RecordType(java.util.Map fieldMap)
          Construct a RecordType with the labels and values specified by a given Map object.
RecordType(java.lang.String[] labels, Type[] types)
          Construct a new RecordType with the specified labels and types.
 
Method Summary
protected  int _compare(StructuredType type)
          Compare this type with the specified type.
protected  StructuredType _getRepresentative()
          Return a static instance of RecordType.
protected  StructuredType _greatestLowerBound(StructuredType type)
          Return the greatest lower bound of this type with the specified type.
private  boolean _isLessThanOrEqualTo(RecordType t1, RecordType t2)
           
protected  StructuredType _leastUpperBound(StructuredType type)
          Return the least Upper bound of this type with the specified type.
 java.lang.Object clone()
          Return a deep copy of this RecordType if it is a variable, or itself if it is a constant.
 Token convert(Token token)
          Convert the argument token into a RecordToken having this type, if lossless conversion can be done.
 int depth()
          Return the depth of a record type.
 boolean equals(java.lang.Object object)
          Determine if the argument represents the same RecordType as this object.
 Type get(java.lang.String label)
          Return the type of the specified label.
 java.lang.Class getTokenClass()
          Return the class for tokens that this type represents.
 InequalityTerm getTypeTerm(java.lang.String label)
          Return the InequalityTerm representing the type of the specified label.
 int hashCode()
          Return a hash code value for this object.
 void initialize(Type type)
          Set the elements that have declared type BaseType.UNKNOWN (the leaf type variable) to the specified type.
 boolean isAbstract()
          Return true if this type does not correspond to a single token class.
 boolean isCompatible(Type type)
          Test if the argument type is compatible with this type.
 boolean isConstant()
          Test if this RecordType is a constant.
 boolean isInstantiable()
          Test if this type corresponds to an instantiable token class.
 boolean isSubstitutionInstance(Type type)
          Test if the specified type is a substitution instance of this type.
 java.util.Set labelSet()
          Return the labels of this record type as a Set.
 java.lang.String toString()
          Return the string representation of this type.
 void updateType(StructuredType newType)
          Update this Type to the specified RecordType.
 
Methods inherited from class ptolemy.data.type.StructuredType
add, divide, getTypeHash, modulo, multiply, one, subtract, zero
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_RECORD

public static RecordType EMPTY_RECORD
An empty record.


_fields

private java.util.Map _fields
Constructor Detail

RecordType

public RecordType(java.lang.String[] labels,
                  Type[] types)
Construct a new RecordType with the specified labels and types. To leave the types of some fields undeclared, use BaseType.UNKNOWN. The labels and the types are specified in two arrays. These two arrays must have the same length, and their elements have one to one correspondence. That is, the i'th entry in the types array is the type for the i'th label in the labels array. To construct the empty record type, set the length of the argument arrays to 0.

Parameters:
labels - An array of String.
types - An array of Type.
Throws:
java.lang.IllegalArgumentException - If the two arrays do not have the same size.
java.lang.NullPointerException - If one of the arguments is null.

RecordType

public RecordType(java.util.Map fieldMap)
           throws IllegalActionException
Construct a RecordType 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-Type values
Method Detail

clone

public java.lang.Object clone()
Return a deep copy of this RecordType if it is a variable, or itself if it is a constant.

Specified by:
clone in interface Type
Specified by:
clone in class StructuredType
Returns:
A RecordType.

convert

public Token convert(Token token)
              throws IllegalActionException
Convert the argument token into a RecordToken having this type, if lossless conversion can be done. The argument must be an RecordToken, and its type must be a subtype of this record type. The argument token must have at least the fields of this type. Extra fields in the argument token that are not in this type are removed.

Specified by:
convert in interface Type
Parameters:
token - A token.
Returns:
An RecordToken.
Throws:
IllegalActionException - If lossless conversion cannot be done.

depth

public int depth()
Return the depth of a record type. The depth of a record type is the number of times it contains other structured types. For example, a record of arrays has depth 2.

Overrides:
depth in class StructuredType
Returns:
the depth of a record type.

equals

public boolean equals(java.lang.Object object)
Determine if the argument represents the same RecordType as this object. Two record types are equal if they have the same field names and the type of each field is the same.

Specified by:
equals in interface Type
Overrides:
equals in class java.lang.Object
Parameters:
object - Another object.
Returns:
True if the argument represents the same RecordType as this object.

get

public Type get(java.lang.String label)
Return the type of the specified label. If this type does not contain the specified label, return null.

Parameters:
label - The specified label.
Returns:
a Type.

getTokenClass

public java.lang.Class getTokenClass()
Return the class for tokens that this type represents.

Specified by:
getTokenClass in interface Type
Returns:
The class for tokens that this type represents.

getTypeTerm

public InequalityTerm getTypeTerm(java.lang.String label)
Return the InequalityTerm representing the type of the specified label.

Parameters:
label - The specified label.
Returns:
An InequalityTerm.
See Also:
InequalityTerm

hashCode

public int hashCode()
Return a hash code value for this object.

Overrides:
hashCode in class java.lang.Object

isAbstract

public boolean isAbstract()
Return true if this type does not correspond to a single token class. This occurs if the type is not instantiable, or it represents either an abstract base class or an interface.

Specified by:
isAbstract in interface Type
Overrides:
isAbstract in class StructuredType
Returns:
true if the type of any field is abstract.

initialize

public void initialize(Type type)
Set the elements that have declared type BaseType.UNKNOWN (the leaf type variable) to the specified type.

Specified by:
initialize in class StructuredType
Parameters:
type - the type to set the leaf type variable to.

isCompatible

public boolean isCompatible(Type type)
Test if the argument type is compatible with this type. The given type will be compatible with this type if it is BaseType.UNKNOWN, or a RecordType that contains at least as many fields.

Specified by:
isCompatible in interface Type
Parameters:
type - An instance of Type.
Returns:
True if the argument is compatible with this type.

isConstant

public boolean isConstant()
Test if this RecordType is a constant. A RecordType is a constant if the declared type of all of its fields are constant.

Specified by:
isConstant in interface Type
Returns:
True if this type is a constant.

isInstantiable

public boolean isInstantiable()
Test if this type corresponds to an instantiable token class. A RecordType is instantiable if all of its fields are instantiable.

Specified by:
isInstantiable in interface Type
Returns:
True if this type is instantiable.

isSubstitutionInstance

public boolean isSubstitutionInstance(Type type)
Test if the specified type is a substitution instance of this type. One record is a substitution instance of another if they have fields with the same names and each field of the given type is a substitution instance of the corresponding field in this type.

Specified by:
isSubstitutionInstance in interface Type
Parameters:
type - A Type.
Returns:
True if the argument is a substitution instance of this type.
See Also:
isSubstitutionInstance(ptolemy.data.type.Type)

labelSet

public java.util.Set labelSet()
Return the labels of this record type as a Set.

Returns:
A Set containing strings.

toString

public java.lang.String toString()
Return the string representation of this type. The format is {label = type, label = type, ...}. The record fields are listed in the lexicographical order of the labels determined by the java.lang.String.compareTo() method.

Specified by:
toString in interface Type
Overrides:
toString in class java.lang.Object
Returns:
A String.

updateType

public void updateType(StructuredType newType)
                throws IllegalActionException
Update this Type to the specified RecordType. The specified type must be a RecordType and have the same structure as this one, and have depth less than the MAXDEPTHDOUND. This method will only update the component whose declared type is BaseType.UNKNOWN, and leave the constant part of this type intact.

Overrides:
updateType in class StructuredType
Parameters:
newType - A StructuredType.
Throws:
IllegalActionException - If the specified type is not a RecordType or it does not have the same structure as this one.

_compare

protected int _compare(StructuredType type)
Compare this type with the specified type. The specified type must be a RecordType, otherwise an exception will be thrown. This method returns one of ptolemy.graph.CPO.LOWER, ptolemy.graph.CPO.SAME, ptolemy.graph.CPO.HIGHER, ptolemy.graph.CPO.INCOMPARABLE, indicating this type is lower than, equal to, higher than, or incomparable with the specified type in the type hierarchy, respectively.

Specified by:
_compare in class StructuredType
Parameters:
type - a RecordType.
Returns:
An integer.
Throws:
java.lang.IllegalArgumentException - If the specified type is not a RecordType.

_getRepresentative

protected StructuredType _getRepresentative()
Return a static instance of RecordType.

Specified by:
_getRepresentative in class StructuredType
Returns:
a RecordType.

_greatestLowerBound

protected StructuredType _greatestLowerBound(StructuredType type)
Return the greatest lower bound of this type with the specified type. The specified type must be a RecordType, otherwise an exception will be thrown.

Specified by:
_greatestLowerBound in class StructuredType
Parameters:
type - a RecordType.
Returns:
a RecordType.
Throws:
java.lang.IllegalArgumentException - If the specified type is not a RecordType.

_leastUpperBound

protected StructuredType _leastUpperBound(StructuredType type)
Return the least Upper bound of this type with the specified type. The specified type must be a RecordType, otherwise an exception will be thrown.

Specified by:
_leastUpperBound in class StructuredType
Parameters:
type - a RecordType.
Returns:
a RecordType.
Throws:
java.lang.IllegalArgumentException - If the specified type is not a RecordType.

_isLessThanOrEqualTo

private boolean _isLessThanOrEqualTo(RecordType t1,
                                     RecordType t2)