ptolemy.data.type
Class ObjectType

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

public class ObjectType
extends java.lang.Object
implements java.lang.Cloneable, Type

A type of tokens that contain arbitrary Java objects. An instance of this class specifies the Java class that the contents of the ObjectTokens of that type must be instances of.

A special type lattice is defined for variants of ObjectType. The top element of the elements in the type lattice is an ObjectType that does not specify any Java class as the class for the contents of its tokens. In the expression language, that element can be referred to with "object" or "object()". Any ObjectToken conforms to this type. A subtype of this type specifies a Java class. In the expression language, such a subtype can be defined with "object(string)", where string is a string (starting and ending with a quote). For example, the following expression refers to a variant of ObjectType to which only ObjectTokens containing atomic actors as their contents conform:

object("ptolemy.actor.AtomicActor")
This ObjectType is a subtype of the most general ObjectType, "object". Furthermore, it is also a subtype of "object(\"ptolemy.kernel.Entity\")", and at the same time a supertype of "object(\"ptolemy.actor.lib.Const\")".

The bottom element of the type lattice is an artificial ObjectType with ObjectType.BottomClass as its specified Java class. In Java, the class hierarchy does not form a lattice, so this artificial type is needed to be the greatest lower bound for any two classes if one is not a subclass of the other.

Since:
Ptolemy II 8.0
Version:
$Id: ObjectType.java 57044 2010-01-27 22:41:05Z cxh $
Author:
Thomas Huining Feng
See Also:
ObjectToken
Accepted Rating:
Red (tfeng)
Proposed Rating:
Red (tfeng)

Nested Class Summary
static class ObjectType.BottomClass
          An artificial Java class that serves as the bottom element.
 
Field Summary
private  java.lang.Class<?> _class
          The Java class specified in this type.
private  java.lang.Object _value
          The actual Object, or null if the actual object is unknown.
static ObjectType BOTTOM
          The bottom element among all ObjectTypes.
static ObjectType TOP
          The top element among all ObjectTypes.
 
Fields inherited from interface ptolemy.data.type.Type
HASH_INVALID, HASH_MAX
 
Constructor Summary
ObjectType()
          Construct an ObjectType with null as the Java class specified in it.
ObjectType(java.lang.Class<?> valueClass)
          Construct an ObjectType with the given Java class as the class specified in it.
ObjectType(java.lang.Object value, java.lang.Class<?> valueClass)
          Construct an ObjectType with the given Java class as the class specified in it.
 
Method Summary
protected  Type _greatestLowerBound(Type type)
          Compute the type representing the greatest lower bound of this type and the given type.
private  boolean _isLessThanOrEqualTo(ObjectType t1, ObjectType t2)
          Test whether the first type is less than or equal to the second in the type lattice.
protected  Type _leastUpperBound(Type type)
          Compute the type representing the least upper bound of this type and the given type.
 Type add(Type rightArgumentType)
          Return a new type which represents the type that results from adding a token of this type and a token of the given argument type.
 java.lang.Object clone()
          Return a deep clone of this type.
 Token convert(Token token)
          Convert the specified token into a token having the type represented by this object.
 Type divide(Type rightArgumentType)
          Return a new type which represents the type that results from dividing a token of this type and a token of the given argument type.
 boolean equals(java.lang.Object object)
          Determine if the argument represents the same type as this object.
 java.lang.Class<?> getTokenClass()
          Return the class for tokens that this type represents.
 int getTypeHash()
          Return a perfect hash for this type.
 java.lang.Object getValue()
          Get the actual value.
 java.lang.Class<?> getValueClass()
          Get the Java class specified in this type, of which the contents of ObjectTokens conforming to this type must be instances.
 int hashCode()
          Return the hash code for this object.
 boolean isAbstract()
          Return true if this type does not correspond to a single token class.
 boolean isCompatible(Type type)
          Return true if this type does not correspond to a single token class.
 boolean isConstant()
          Test if the argument type is compatible with this type.
 boolean isInstantiable()
          Determine if this Type corresponds to an instantiable token class.
 boolean isSubstitutionInstance(Type type)
          Return true if the specified type is a substitution instance of this type.
 Type modulo(Type rightArgumentType)
          Return a new type which represents the type that results from moduloing a token of this type and a token of the given argument type.
 Type multiply(Type rightArgumentType)
          Return a new type which represents the type that results from multiplying a token of this type and a token of the given argument type.
 Type one()
          Return the type of the multiplicative identity for elements of this type.
 Type subtract(Type rightArgumentType)
          Return a new type which represents the type that results from subtracting a token of this type and a token of the given argument type.
 java.lang.String toString()
          Return a string describing this object.
 Type zero()
          Return the type of the additive identity for elements of this type.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

BOTTOM

public static final ObjectType BOTTOM
The bottom element among all ObjectTypes.


TOP

public static final ObjectType TOP
The top element among all ObjectTypes.


_class

private java.lang.Class<?> _class
The Java class specified in this type.


_value

private java.lang.Object _value
The actual Object, or null if the actual object is unknown.

Constructor Detail

ObjectType

public ObjectType()
Construct an ObjectType with null as the Java class specified in it. This type is the most general type (top element) among all the ObjectTypes.


ObjectType

public ObjectType(java.lang.Class<?> valueClass)
Construct an ObjectType with the given Java class as the class specified in it.

Parameters:
valueClass - The Java class.

ObjectType

public ObjectType(java.lang.Object value,
                  java.lang.Class<?> valueClass)
           throws IllegalActionException
Construct an ObjectType with the given Java class as the class specified in it.

Parameters:
value - The actual object value, or null if the value is unknown.
valueClass - The Java class.
Throws:
IllegalActionException - If the value is not an instance of valueClass.
Method Detail

add

public Type add(Type rightArgumentType)
Return a new type which represents the type that results from adding a token of this type and a token of the given argument type.

Specified by:
add in interface Type
Parameters:
rightArgumentType - The type to add to this type.
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

clone

public java.lang.Object clone()
Return a deep clone of this type.

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

convert

public Token convert(Token token)
              throws IllegalActionException
Convert the specified token into a token having the type represented by this object.

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

divide

public Type divide(Type rightArgumentType)
Return a new type which represents the type that results from dividing a token of this type and a token of the given argument type.

Specified by:
divide in interface Type
Parameters:
rightArgumentType - The type to add to this type.
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

equals

public boolean equals(java.lang.Object object)
Determine if the argument represents the same type as this object.

Specified by:
equals in interface Type
Overrides:
equals in class java.lang.Object
Parameters:
object - A Type.
Returns:
True if the argument represents the same type as this object; false otherwise.

getTokenClass

public java.lang.Class<?> getTokenClass()
Return the class for tokens that this type represents. The returned class is always ObjectToken.

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

getTypeHash

public int getTypeHash()
Return a perfect hash for this type. This number corresponds uniquely to a particular type, and is used to improve performance of certain operations in the TypeLattice class. All instances of a particular type (e.g. integer array) must return the same number. Types that return HASH_INVALID will not have results in TypeLattice cached. Note that it is safer to return HASH_INVALID, than to return a number that is not unique, or different number for the same type from different instances.

Specified by:
getTypeHash in interface Type
Returns:
A number between 0 and HASH_MAX, or HASH_INVALID.

getValue

public java.lang.Object getValue()
Get the actual value.

Returns:
The actual value, or null if it is unknown.

getValueClass

public java.lang.Class<?> getValueClass()
Get the Java class specified in this type, of which the contents of ObjectTokens conforming to this type must be instances.

Returns:
The Java class specified in this type.

hashCode

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

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code.

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
Returns:
True if this type does not correspond to a single token class.

isCompatible

public boolean isCompatible(Type type)
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:
isCompatible in interface Type
Parameters:
type - The type to be checked
Returns:
True if this type does not correspond to a single token class.

isConstant

public boolean isConstant()
Test if the argument type is compatible with this type. Compatible is defined as follows: If this type is a constant, the argument is compatible if it is the same or less than this type in the type lattice; If this type is a variable, the argument is compatible if it is a substitution instance of this type.

Specified by:
isConstant in interface Type
Returns:
True if the argument is compatible with this type.

isInstantiable

public boolean isInstantiable()
Determine if this Type corresponds to an instantiable token class.

Specified by:
isInstantiable in interface Type
Returns:
True if this type corresponds to an instantiable token class.

isSubstitutionInstance

public boolean isSubstitutionInstance(Type type)
Return true if the specified type is a substitution instance of this type. For the argument to be a substitution instance, it must be either the same as this type, or it must be a type that can be obtained by replacing the BaseType.UNKNOWN component of this type by another type.

Specified by:
isSubstitutionInstance in interface Type
Parameters:
type - A Type.
Returns:
True if the argument is a substitution instance of this type.

modulo

public Type modulo(Type rightArgumentType)
Return a new type which represents the type that results from moduloing a token of this type and a token of the given argument type.

Specified by:
modulo in interface Type
Parameters:
rightArgumentType - The type to add to this type.
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

multiply

public Type multiply(Type rightArgumentType)
Return a new type which represents the type that results from multiplying a token of this type and a token of the given argument type.

Specified by:
multiply in interface Type
Parameters:
rightArgumentType - The type to add to this type.
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

one

public Type one()
Return the type of the multiplicative identity for elements of this type.

Specified by:
one in interface Type
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

subtract

public Type subtract(Type rightArgumentType)
Return a new type which represents the type that results from subtracting a token of this type and a token of the given argument type.

Specified by:
subtract in interface Type
Parameters:
rightArgumentType - The type to add to this type.
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

toString

public java.lang.String toString()
Return a string describing this object.

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

zero

public Type zero()
Return the type of the additive identity for elements of this type.

Specified by:
zero in interface Type
Returns:
A new type, or BaseType.GENERAL, if the operation does not make sense for the given types.

_greatestLowerBound

protected Type _greatestLowerBound(Type type)
Compute the type representing the greatest lower bound of this type and the given type.

Parameters:
type - The given type.
Returns:
The greatest lower bound.

_leastUpperBound

protected Type _leastUpperBound(Type type)
Compute the type representing the least upper bound of this type and the given type.

Parameters:
type - The given type.
Returns:
The least upper bound.

_isLessThanOrEqualTo

private boolean _isLessThanOrEqualTo(ObjectType t1,
                                     ObjectType t2)
Test whether the first type is less than or equal to the second in the type lattice.

Parameters:
t1 - The first class.
t2 - The second class.
Returns:
true if the first class is less than or equal to the second; false otherwise.