ptolemy.data.type
Class UnionType

java.lang.Object
  extended by ptolemy.data.type.StructuredType
      extended by ptolemy.data.type.UnionType
All Implemented Interfaces:
java.lang.Cloneable, Type

public class UnionType
extends StructuredType
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.

Note that an union type with more fields is a supertype of a union type with a subset of the fields. For example, {|x = double, y = int} is a supertype of {|x = double}. When an union of type {|x = double} is converted to one of type {|x = double, y = int}, an extra field is added, but a value with the lower type will not have the type of this extra field.

Since:
Ptolemy II 5.2
Version:
$Id: UnionType.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Yuhong Xiong, Elaine Cheong and Steve Neuendorffer
Accepted Rating:
Red (cxh)
Proposed Rating:
Red (yuhongx)

Nested Class Summary
private  class UnionType.FieldType
           
 
Field Summary
private  java.util.Map _fields
           
private static UnionType _representative
           
 
Fields inherited from class ptolemy.data.type.StructuredType
MAXDEPTHBOUND
 
Fields inherited from interface ptolemy.data.type.Type
HASH_INVALID, HASH_MAX
 
Constructor Summary
UnionType(java.lang.String[] labels, Type[] types)
          Construct a new UnionType 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(UnionType t1, UnionType 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 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
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

_fields

private java.util.Map _fields

_representative

private static UnionType _representative
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 interface Type
Specified by:
clone in class StructuredType
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 interface Type
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 class StructuredType
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.

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 UnionType 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

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.

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 interface Type
Overrides:
isAbstract in class StructuredType
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 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 UnionType is a constant. A UnionType 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 UnionType 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 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 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 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 class StructuredType
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 class StructuredType
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 class StructuredType
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 class StructuredType
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 class StructuredType
Parameters:
type - a UnionType.
Returns:
a UnionType.
Throws:
java.lang.IllegalArgumentException - If the specified type is not a UnionType.

_isLessThanOrEqualTo

private boolean _isLessThanOrEqualTo(UnionType t1,
                                     UnionType t2)