FileDocCategorySizeDatePackage
TypeSupport.javaAPI DocGlassfish v2 API22729Fri May 04 22:34:50 BST 2007com.sun.jdo.spi.persistence.support.ejb.ejbqlc

TypeSupport

public class TypeSupport extends Object
Helper class to support type info access. A type info is statically an object, internally the helper uses the type name as type info. The helper uses a model instance to access meta model info and uses a NameMapper to map EJB names to JDO names and vice versa.
author
Michael Bouschen
author
Shing Wai Chan

Fields Summary
public static final Object
errorType
Represents the internal error type.
public static final Object
booleanType
Represents the primitive type boolean.
public static final Object
byteType
Represents the primitive type byte.
public static final Object
shortType
Represents the primitive type short.
public static final Object
charType
Represents the primitive type char.
public static final Object
intType
Represents the primitive type int.
public static final Object
longType
Represents the primitive type long.
public static final Object
floatType
Represents the primitive type float.
public static final Object
doubleType
Represents the primitive type double.
public static final Object
booleanClassType
Represents the wrapper class type boolean.
public static final Object
byteClassType
Represents the wrapper class type byte.
public static final Object
shortClassType
Represents the wrapper class type short.
public static final Object
characterClassType
Represents the wrapper class type char.
public static final Object
integerClassType
Represents the wrapper class type int.
public static final Object
longClassType
Represents the wrapper class type long.
public static final Object
floatClassType
Represents the wrapper class type float.
public static final Object
doubleClassType
Represents the wrapper class type double.
public static final Object
stringType
Represents the type java.lang.String.
public static final Object
bigDecimalType
Represents the type java.math.BigDecimal.
public static final Object
bigIntegerType
Represents the type java.math.BigInteger.
protected static final Set
numericTypes
Set of names of numeric types.
protected static final Set
numericWrapperTypes
Set of names of numeric wrapper classes.
protected static final Set
dateTimeTypes
Set of names of date and time types.
protected com.sun.jdo.api.persistence.model.Model
model
Meta data access.
protected com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper
nameMapper
Name mapping EJB <-> JDO.
protected static final ResourceBundle
msgs
I18N support.
Constructors Summary
public TypeSupport(com.sun.jdo.api.persistence.model.Model model, com.sun.jdo.spi.persistence.support.ejb.model.util.NameMapper nameMapper)
Creates a new TypeSupport using the specified model instance to access meta data and the specified nameMapper for EJB <-> JDO name mapping.

    
          
     
    
        numericTypes.add(byteType);
        numericTypes.add(shortType);
        numericTypes.add(charType);
        numericTypes.add(intType);
        numericTypes.add(longType);
        numericTypes.add(floatType);
        numericTypes.add(doubleType);
        
        numericWrapperTypes.add(byteClassType);
        numericWrapperTypes.add(shortClassType);
        numericWrapperTypes.add(characterClassType);
        numericWrapperTypes.add(integerClassType);
        numericWrapperTypes.add(longClassType);
        numericWrapperTypes.add(floatClassType);
        numericWrapperTypes.add(doubleClassType);

        dateTimeTypes.add("java.util.Date"); //NOI18N
        dateTimeTypes.add("java.sql.Date"); //NOI18N
        dateTimeTypes.add("java.sql.Time"); //NOI18N
        dateTimeTypes.add("java.sql.Timestamp"); //NOI18N
    
        this.model = model;
        this.nameMapper = nameMapper;
    
Methods Summary
public static java.lang.ObjectbinaryNumericPromotion(java.lang.Object left, java.lang.Object right)
Implements binary numeric promotion as defined in the Java Language Specification section 5.6.2

        if (isNumericType(left) && isNumericType(right)) {
            if (left.equals(doubleType) || right.equals(doubleType))
                return doubleType;
            else if (left.equals(floatType) || right.equals(floatType))
                return floatType;
            else if (left.equals(longType) || right.equals(longType))
                return longType;
            else
                return intType;
        }
        return errorType;
    
public java.lang.StringgetAbstractSchemaForTypeInfo(java.lang.Object typeInfo)
Returns the typeInfo (the ejb name) for the specified abstract schema.

        String typeName = getTypeName(typeInfo);
        return nameMapper.isEjbName(typeName) ? 
            nameMapper.getAbstractSchemaForEjbName(typeName) :
            typeName;
    
public java.lang.ObjectgetAvgReturnType(java.lang.Object type)
Return JDO QL return type for Avg function for a given type.

param
type is a number data type

        if (isNumericType(type) || isNumericWrapperType(type)) {
            return doubleClassType;
        } else {
            return type;
        }
    
public java.lang.ObjectgetElementType(java.lang.Object fieldInfo)
Returns the type info of the element type if the specified field info denotes a collection relationship. Otherwise it returns null.

        if ((fieldInfo != null) && (fieldInfo instanceof RelationshipElement)) {
            String elementClass = ((RelationshipElement)fieldInfo).getElementClass();
            return nameMapper.getEjbNameForPersistenceClass(elementClass);
        }
        else
            return null;
    
public java.lang.ObjectgetFieldInfo(java.lang.Object typeInfo, java.lang.String fieldName)
Returns the field info for the specified field of the specified type. The field info is opaque for the caller. Methods {@link #isRelationship} and {@link #getElementType} allow to get details for a given field info.

        Object fieldInfo = null;
        String typeName = getTypeName(typeInfo);
        if (!nameMapper.isEjbName(typeName)) {
            ErrorMsg.fatal(I18NHelper.getMessage(
                msgs, "ERR__EjbNameExpected", //NOI18N
                "TypeSupport.getFieldInfo", typeName)); //NOI18N
        }
        String pcClassName = nameMapper.getPersistenceClassForEjbName(typeName);
        String pcFieldName = nameMapper.getPersistenceFieldForEjbField(
            typeName, fieldName);
        PersistenceClassElement pce = model.getPersistenceClass(pcClassName);
        if (pce != null) {
            fieldInfo = pce.getField(pcFieldName);
        }
        return fieldInfo;
    
public java.lang.ObjectgetFieldType(java.lang.Object typeInfo, java.lang.String fieldName)
Returns the type info for the type of the given field.

        String typeName = getTypeName(typeInfo);
        if (!nameMapper.isEjbName(typeName)) {
            ErrorMsg.fatal(I18NHelper.getMessage(
                msgs, "ERR_EjbNameExpected", //NOI18N
                "TypeSupport.getFieldType", typeName)); //NOI18N
        }
        
        String fieldType = model.getFieldType(typeName, fieldName);
        // check for local or remote interface, map to ejb name
        if (nameMapper.isLocalInterface(fieldType)) {
            fieldType = nameMapper.getEjbNameForLocalInterface(
                typeName, fieldName, fieldType);
        }
        else if (nameMapper.isRemoteInterface(fieldType)) {

            fieldType = nameMapper.getEjbNameForRemoteInterface(
                typeName, fieldName, fieldType);
        }
        return getTypeInfo(fieldType);
    
public java.lang.ObjectgetMinMaxReturnType(java.lang.Object type)
Return JDO QL return type for Min/Max function for a given type.

param
type is an orderable data type

        if (isFloatingPointType(type)) {
            return doubleClassType;
        } else if (isCharType(type)) {
            return characterClassType;
        } else if (isNumericType(type) || isNumericWrapperType(type)) {
            return longClassType;
        } else {
            return type;
        }
    
public java.lang.StringgetPCForTypeInfo(java.lang.Object typeInfo)
Gets the name of the persistence-capable class which corresponds to the specified typeInfo (assuming an ejb name). The method returs the type name of the specified typeInfo, it the typeInfo does not denote an ejb-name (e.g. a local or remote interface).

        String typeName = getTypeName(typeInfo);
        String pcClassName = 
            nameMapper.getPersistenceClassForEjbName(typeName);
        return (pcClassName != null) ? pcClassName : typeName;
    
public static java.lang.ObjectgetPrimitiveType(java.lang.Object type)
Returns the type info for a primitive type. The method returns {@link #errorType} if the specified type is not a primitive type.

        Object result = errorType;
        if (type.equals(booleanClassType))
            result = booleanType;
        else if (type.equals(integerClassType))
            result = intType;
        else if (type.equals(longClassType))
            result = longType;
        else if (type.equals(floatClassType))
            result = floatType;
        else if (type.equals(doubleClassType))
            result = doubleType;
        else if (type.equals(byteClassType))
            result = byteType;
        else if (type.equals(shortClassType))
            result = shortType;
        else if (type.equals(characterClassType))
            result = charType;
        return result;
    
public java.lang.ObjectgetSumReturnType(java.lang.Object type)
Return JDO QL return type for Sum function for a given type.

param
type is a number data type

        if (isFloatingPointType(type)) {
            return doubleClassType;
        } else if (isNumericType(type) || isNumericWrapperType(type)) {
            return longClassType;
        } else {
            return type;
        }
    
public java.lang.ObjectgetTypeInfo(java.lang.String name)
The method returns a type info by type name. If the type name denotes a class the name should be fully qualified. The method uses the type name as type info.

        return name;
    
public java.lang.ObjectgetTypeInfo(java.lang.Class clazz)
The method returns a type info by type name by class object.

        return getTypeInfo(clazz.getName());
    
public java.lang.ObjectgetTypeInfoForAbstractSchema(java.lang.String abstractSchema)
Returns the typeInfo (the ejb name) for the specified abstract schema.

        return nameMapper.getEjbNameForAbstractSchema(abstractSchema);
    
public static java.lang.StringgetTypeName(java.lang.Object type)
Returns the type name for a specified type info.

        return (String)type;
    
public static java.lang.ObjectgetWrapperType(java.lang.Object type)
Returns the type info for a wrapper class type. The method returns {@link #errorType} if the specified type is not a wrapper class type.

        Object result = errorType;
        if (type.equals(booleanType))
            result = booleanClassType;
        else if (type.equals(intType))
            result = integerClassType;
        else if (type.equals(longType))
            result = longClassType;
        else if (type.equals(floatType))
            result = floatClassType;
        else if (type.equals(doubleType))
            result = doubleClassType;
        else if (type.equals(byteType))
            result = byteClassType;
        else if (type.equals(shortType))
            result = shortClassType;
        else if (type.equals(charType))
            result = characterClassType;
        return result;
    
public booleanhasLocalInterface(java.lang.Object typeInfo)
Returns true if the bean with the specified ejb name has a local interface.

        return nameMapper.getLocalInterfaceForEjbName(
            getTypeName(typeInfo)) != null;
    
public booleanhasRemoteInterface(java.lang.Object typeInfo)
Returns true if the bean with the specified ejb name has a remote interface.

        return nameMapper.getRemoteInterfaceForEjbName(
            getTypeName(typeInfo)) != null;
    
public static booleanisBooleanType(java.lang.Object type)
Returns true if type is boolean or java.lang.Boolean

        return type.equals(booleanType) || 
               type.equals(booleanClassType);
    
public static booleanisCharType(java.lang.Object type)
Returns true if type is char or java.lang.Character

        return type.equals(charType) || 
               type.equals(characterClassType);
    
public booleanisCollectionType(java.lang.Object type)
Returns true if type is a collection type.

        return model.isCollection((String)type);
    
public booleanisCompatibleWith(java.lang.Object left, java.lang.Object right)
Implements type compatibility. The method returns true if left is compatible with right. This is equivalent to rightClass.isAssignableFrom(leftClass). Note, the method does not support inheritance.

        String leftTypeName = getTypeName(left);
        String rightTypeName = getTypeName(right);

        if (nameMapper.isLocalInterface(leftTypeName) && 
            nameMapper.isEjbName(rightTypeName))
            rightTypeName = nameMapper.getLocalInterfaceForEjbName(rightTypeName);
        else if (nameMapper.isRemoteInterface(leftTypeName) && 
            nameMapper.isEjbName(rightTypeName))
            rightTypeName = nameMapper.getRemoteInterfaceForEjbName(rightTypeName);
        else if (nameMapper.isLocalInterface(rightTypeName) && 
            nameMapper.isEjbName(leftTypeName))
            leftTypeName = nameMapper.getLocalInterfaceForEjbName(leftTypeName);
        else if (nameMapper.isRemoteInterface(rightTypeName) && 
            nameMapper.isEjbName(leftTypeName))
            leftTypeName = nameMapper.getRemoteInterfaceForEjbName(leftTypeName);

        // does not handle inheritance!
        return leftTypeName.equals(rightTypeName);
    
public booleanisDateTimeType(java.lang.Object type)
Returns true if type is a date or time type

        return dateTimeTypes.contains(getTypeName(type));
    
public static booleanisDoubleType(java.lang.Object type)
Returns true if type is double or java.lang.Double.

        return type.equals(doubleType) || 
               type.equals(doubleClassType);
    
public booleanisEjbName(java.lang.Object typeInfo)
Returns true if the specified type info denotes an ejb name.

        return nameMapper.isEjbName(getTypeName(typeInfo));
    
public booleanisEjbOrInterfaceName(java.lang.Object typeInfo)
Returns true if the specified type info denotes an ejb name or the name of a local interface or the name of a remote interface.

        String typeName = getTypeName(typeInfo);
        return nameMapper.isEjbName(typeName) || 
               nameMapper.isLocalInterface(typeName) || 
               nameMapper.isRemoteInterface(typeName);
    
public static booleanisErrorType(java.lang.Object type)
Returns true if type denotes the error type.

        return type.equals(errorType);
    
public static booleanisFloatingPointType(java.lang.Object type)
Returns true if type is a floating point type or wrapper class of a floating point type.

        return doubleType.equals(type) ||
               doubleClassType.equals(type) ||
               floatType.equals(type) ||
               floatClassType.equals(type);
    
public static booleanisIntType(java.lang.Object type)
Returns true if type is int or java.lang.Integer

        return type.equals(intType) || 
               type.equals(integerClassType);
    
public booleanisLocalInterface(java.lang.Object typeInfo)
Returns true if the specified type info denotes a local interface.

        return nameMapper.isLocalInterface(getTypeName(typeInfo));
    
public booleanisLocalInterfaceOfEjb(java.lang.Object typeInfo, java.lang.String ejbName)
Returns true if the specified type info denotes the local interface of the bean with the specified ejb name.

        String typeName = getTypeName(typeInfo);
        String localInterface = nameMapper.getLocalInterfaceForEjbName(ejbName);
        return (localInterface != null) && localInterface.equals(typeName);
    
public static booleanisNumberType(java.lang.Object type)
Returns true if type is a NumerType, which means it is either a numeric primitive or a numeric wrapper class.

        return isNumericType(type) ||
               isNumericWrapperType(type) ||
               bigDecimalType.equals(type) ||
               bigIntegerType.equals(type);
    
public static booleanisNumericType(java.lang.Object type)
Returns true if type is a primitive numeric type such as byte, int etc.

        return numericTypes.contains(type);
    
public static booleanisNumericWrapperType(java.lang.Object type)
Returns true if type is a wrapper class of a primitive numeric type such as java.lang.Byte, java.lang.Integer etc.

        return numericWrapperTypes.contains(type);
    
public booleanisOrderableType(java.lang.Object type)
Returns true if type is an orderable type

        return isNumberType(type) || isDateTimeType(type) || isStringType(type);
    
public booleanisRelationship(java.lang.Object fieldInfo)
Returns true if the specified field info denotes a relationship field.

        return (fieldInfo != null) && (fieldInfo instanceof RelationshipElement);
    
public booleanisRemoteInterface(java.lang.Object typeInfo)
Returns true if the specified type info denotes a remote interface.

        return nameMapper.isRemoteInterface(getTypeName(typeInfo));
    
public booleanisRemoteInterfaceOfEjb(java.lang.Object typeInfo, java.lang.String ejbName)
Returns true if the specified type info denotes the remote interface of the bean with the specified ejb name.

        String typeName = getTypeName(typeInfo);
        String remoteInterface = nameMapper.getRemoteInterfaceForEjbName(ejbName);
        return (remoteInterface != null) && remoteInterface.equals(typeName);
        
    
public static booleanisStringType(java.lang.Object type)
Returns true if type denotes java.lang.String.

        return type.equals(stringType);
    
public static java.lang.ObjectunaryNumericPromotion(java.lang.Object type)
Implements unray numeric promotion as defined in the Java Language Specification section 5.6.1

        if (isNumericType(type)) {
            if (type.equals(byteType) || type.equals(shortType) || 
                type.equals(charType)) {
                return intType;
            }
            else {
                return type;
            }
        }
        return errorType;