Package ptolemy.data.type
Class UnionType
- java.lang.Object
-
- ptolemy.data.type.StructuredType
-
- ptolemy.data.type.AssociativeType
-
- ptolemy.data.type.UnionType
-
- All Implemented Interfaces:
java.lang.Cloneable
,Type
public class UnionType extends AssociativeType implements java.lang.Cloneable
A class representing the type of a UnionToken. To set the type of a typeable object (such as a port or parameter) to a union with particular fields, create an instance of this class and call setTypeEquals() with that instance as an argument.The depth subtyping is similar to that of
RecordTypes
. However, the width subtyping forUnionType
is opposite compared toRecordType
i.e., aUnionType
with more fields is a supertype of aUnionType
with a subset of the fields. For example, {|x = double, y = int} is a supertype of {|x = double}.- Since:
- Ptolemy II 5.2
- Version:
- $Id$
- Author:
- Yuhong Xiong, Elaine Cheong and Steve Neuendorffer
- Pt.AcceptedRating:
- Red (cxh)
- Pt.ProposedRating:
- Red (yuhongx)
-
-
Field Summary
-
Fields inherited from class ptolemy.data.type.StructuredType
MAXDEPTHBOUND
-
Fields inherited from interface ptolemy.data.type.Type
HASH_INVALID
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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 UnionType if it is a variable, or itself if it is a constant.Token
convert(Token token)
Convert the argument token into a UnionToken having this type, if lossless conversion can be done.int
depth()
Return the depth of a union type.boolean
equals(java.lang.Object object)
Determine if the argument represents the same UnionType 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()
Test if this type corresponds to an abstract token class.boolean
isCompatible(Type type)
Test if the argument type is compatible with this type.boolean
isConstant()
Test if this UnionType 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 UnionType.-
Methods inherited from class ptolemy.data.type.StructuredType
add, divide, getTypeHash, modulo, multiply, one, subtract, zero
-
-
-
-
Constructor Detail
-
UnionType
public UnionType(java.lang.String[] labels, Type[] types)
Construct a new UnionType 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.
-
-
Method Detail
-
clone
public java.lang.Object clone()
Return a deep copy of this UnionType if it is a variable, or itself if it is a constant.- Specified by:
clone
in interfaceType
- Specified by:
clone
in classStructuredType
- Returns:
- A UnionType.
-
convert
public Token convert(Token token) throws IllegalActionException
Convert the argument token into a UnionToken having this type, if lossless conversion can be done. The argument must be a UnionToken, 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 interfaceType
- Parameters:
token
- A token.- Returns:
- An UnionToken.
- Throws:
IllegalActionException
- If lossless conversion cannot be done.
-
depth
public int depth()
Return the depth of a union type. The depth of a union type is the number of times it contains other structured types.- Overrides:
depth
in classStructuredType
- Returns:
- the depth of a union type.
-
equals
public boolean equals(java.lang.Object object)
Determine if the argument represents the same UnionType as this object. Two record types are equal if they have the same field names and the type of each field is the same.
-
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.- Specified by:
get
in classAssociativeType
- 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 interfaceType
- 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 classjava.lang.Object
-
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 classStructuredType
- Parameters:
type
- the type to set the leaf type variable to.
-
isAbstract
public boolean isAbstract()
Test if this type corresponds to an abstract token class. A UnionType is abstract if any field is abstract- Specified by:
isAbstract
in interfaceType
- Overrides:
isAbstract
in classStructuredType
- Returns:
- True if this type is instantiable.
-
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 UnionType that contains at most as many fields.- Specified by:
isCompatible
in interfaceType
- Parameters:
type
- An instance of Type.- Returns:
- True if the argument is compatible with this type.
-
isConstant
public boolean isConstant()
Test if this UnionType is a constant. A UnionType is a constant if the declared type of all of its fields are constant.- Specified by:
isConstant
in interfaceType
- Returns:
- True if this type is a constant.
-
isInstantiable
public boolean isInstantiable()
Test if this type corresponds to an instantiable token class. A UnionType is instantiable if all of its fields are instantiable.- Specified by:
isInstantiable
in interfaceType
- 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 union 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 interfaceType
- Parameters:
type
- A Type.- Returns:
- True if the argument is a substitution instance of this type.
- See Also:
Type.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.
-
updateType
public void updateType(StructuredType newType) throws IllegalActionException
Update this Type to the specified UnionType. The specified type must be a UnionType and have the same structure as this one. 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 classStructuredType
- Parameters:
newType
- A StructuredType.- Throws:
IllegalActionException
- If the specified type is not a UnionType 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 UnionType, 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 classStructuredType
- Parameters:
type
- a UnionType.- Returns:
- An integer.
- Throws:
java.lang.IllegalArgumentException
- If the specified type is not a UnionType.
-
_getRepresentative
protected StructuredType _getRepresentative()
Return a static instance of RecordType.- Specified by:
_getRepresentative
in classStructuredType
- Returns:
- a UnionType.
-
_greatestLowerBound
protected StructuredType _greatestLowerBound(StructuredType type)
Return the greatest lower bound of this type with the specified type. The specified type must be a UnionType, otherwise an exception will be thrown.- Specified by:
_greatestLowerBound
in classStructuredType
- Parameters:
type
- a UnionType.- Returns:
- a UnionType.
- Throws:
java.lang.IllegalArgumentException
- If the specified type is not a UnionType.
-
_leastUpperBound
protected StructuredType _leastUpperBound(StructuredType type)
Return the least Upper bound of this type with the specified type. The specified type must be a UnionType, otherwise an exception will be thrown.- Specified by:
_leastUpperBound
in classStructuredType
- Parameters:
type
- a UnionType.- Returns:
- a UnionType.
- Throws:
java.lang.IllegalArgumentException
- If the specified type is not a UnionType.
-
-