ptolemy.data.expr
Class ConversionUtilities

java.lang.Object
  extended by ptolemy.data.expr.ConversionUtilities

public class ConversionUtilities
extends java.lang.Object

This class contains a series of static methods that facilitate the runtime conversion of tokens to and from Java representations that are not tokens. One might call this "marshaling and unmarshaling" of tokens. Primarily this facility is used by the expression language to properly type references to Java methods, and later invoke those methods during expression evaluation. Generally speaking this is somewhat nasty from an Object-oriented point of view. The nastiness is fairly well encapsulated in this class. The mapping is summarized in the following table:

 Token type               Java type
 ---------------------------------------------------
 IntToken                 int
 DoubleToken              double
 LongToken                long
 StringToken              java.lang.String
 BooleanToken             boolean
 ComplexToken             ptolemy.math.Complex
 FixToken                 ptolemy.math.FixPoint
 FixMatrixToken           ptolemy.math.FixPoint[][]
 IntMatrixToken           int[][]
 DoubleMatrixToken        double[][]
 ComplexMatrixToken       ptolemy.math.Complex[][]
 LongMatrixToken          long[][]
 BooleanMatrixToken       boolean[][]
 ArrayToken(FixToken)     ptolemy.math.FixPoint[]
 ArrayToken(IntToken)     int[]
 ArrayToken(LongToken)    long[]
 ArrayToken(DoubleToken)  double[]
 ArrayToken(ComplexToken) ptolemy.math.Complex[]
 ArrayToken(StringToken)  java.lang.String[]
 ArrayToken(BooleanToken) boolean[]
 ArrayToken  (*)          Token[]
 ---------------------------------------------------
 (*) Only when converting from java to Token types
 

Version:
$Id: ConversionUtilities.java 57046 2010-01-27 23:35:53Z cxh $
Author:
Neil Smyth, Edward A. Lee, Steve Neuendorffer, Zoltan Kemenczy, Research in Motion Limited
See Also:
ASTPtRootNode, PtParser, Token, UtilityFunctions, Math

Constructor Summary
ConversionUtilities()
           
 
Method Summary
static Token convertJavaTypeToToken(java.lang.Object object)
          Convert a java object to a corresponding Token.
static Type convertJavaTypeToTokenType(java.lang.Class tokenClass)
          Convert a java class, representing a Java type, to a corresponding instance of a ptolemy type object, as consistent with the convertJavaTypeToToken method.
static java.lang.Object convertTokenToJavaType(Token token)
          Convert a Token to a corresponding Java object.
static java.lang.Class convertTokenTypeToJavaType(Type type)
          Convert the given ptolemy type object to a java class representing a java type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConversionUtilities

public ConversionUtilities()
Method Detail

convertJavaTypeToToken

public static Token convertJavaTypeToToken(java.lang.Object object)
                                    throws IllegalActionException
Convert a java object to a corresponding Token. This method is called by the expression language to marshal numeric object into tokens. If the argument is a token, this function returns it. If the argument is an array, an array token will be returned. If the argument is an array of array, then a matrix token will be returned. If the argument is a Java numeric encapsulation object, e.g. java.lang.Double, then the number is extracted and re-encapsulated in a token. If no other conversion is possible, then this method will simply return an ObjectToken wrapping the object.

Returns:
A new token.
Throws:
IllegalActionException - If the selected conversion fails.

convertJavaTypeToTokenType

public static Type convertJavaTypeToTokenType(java.lang.Class tokenClass)
                                       throws IllegalActionException
Convert a java class, representing a Java type, to a corresponding instance of a ptolemy type object, as consistent with the convertJavaTypeToToken method.

Throws:
IllegalActionException - If the token class is not recognized, or creating the type fails.

convertTokenToJavaType

public static java.lang.Object convertTokenToJavaType(Token token)
                                               throws IllegalActionException
Convert a Token to a corresponding Java object. This method is called by the expression language to unmarshal numeric objects from tokens. If the argument is an array token, this function returns an Java array of the correct type. If the argument is a matrix token, this function returns a square Java array of arrays. If the argument is another type of token, this function returns the encapsulated data, rewrapped in a Java numeric encapsulating object, e.g. java.lang.Double, if necessary. If no conversion is possible, then this method throws an exception.

Returns:
An object that is not a ptolemy.data.Token or an array of ptolemy.data.Token.
Throws:
IllegalActionException - If the selected conversion fails.

convertTokenTypeToJavaType

public static java.lang.Class convertTokenTypeToJavaType(Type type)
                                                  throws IllegalActionException
Convert the given ptolemy type object to a java class representing a java type. The conversion is the complement of that provided by the convertJavaTypeToTokenType() method. Note that, generally speaking the reverse is not true, since ptolemy types represent more information about data types than do java types.

Throws:
IllegalActionException - If the token class is not recognized, or creating the type fails.