Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package net.java.ao.types;
  
  import net.java.ao.Common;
  
 import static java.sql.Types.*;
 import static net.java.ao.types.LogicalTypes.*;
 import static net.java.ao.types.SchemaProperties.schemaType;
 import static net.java.ao.types.TypeQualifiers.UNLIMITED_LENGTH;
 import static net.java.ao.types.TypeQualifiers.qualifiers;

Central managing class for the ActiveObjects type system. The type system in AO is designed to allow extensibility and control over how specific data types are handled internally. All database-agnostic, type-specific tasks are delegated to the actual type instances. This class acts as a container for every available type, indexing them based on corresponding Java type and JDBC integer type.

This container is thread safe and so may be used from within multiple contexts.

 
 public class TypeManager
 {
     private static final ImmutableSet<IntegerUNLIMITED_TEXT_TYPES = ImmutableSet.of();
 
     private final ImmutableMultimap<Class<?>, TypeInfo<?>> classIndex;
     private final ImmutableMultimap<IntegerTypeInfo<?>> jdbcTypeIndex;
 
     private TypeManager(Builder builder)
     {
         this. = ImmutableMultimap.copyOf(builder.classIndex);
         this. = ImmutableMultimap.copyOf(builder.jdbcTypeIndex);
     }
 
     public <T> TypeInfo<T> getType(Class<T> javaType)
     {
         return getType(javaTypequalifiers());
     }
 
     @SuppressWarnings("unchecked")
     public <T> TypeInfo<T> getType(Class<T> javaTypeTypeQualifiers qualifiers)
     {
         if (RawEntity.class.isAssignableFrom(javaType))
         {
             final Class<RawEntity<Object>> entityType = (Class<RawEntity<Object>>) javaType;
             final Class<ObjectprimaryKeyClass = Common.getPrimaryKeyClassType(entityType);
             final TypeInfo<ObjectprimaryKeyTypeInfo = getType(primaryKeyClass);
             final LogicalType<RawEntity<Object>> logicalType =
                 LogicalTypes.entityType(entityTypeprimaryKeyTypeInfoprimaryKeyClass);
             return (TypeInfo<T>) new TypeInfo<RawEntity<Object>>(logicalType,
                 primaryKeyTypeInfo.getSchemaProperties(),
                 primaryKeyTypeInfo.getQualifiers());
         }
         for (Class<?> clazz = javaTypeclazz != nullclazz = clazz.getSuperclass())
         {
             if (.containsKey(clazz))
             {
                 return (TypeInfo<T>) findTypeWithQualifiers(.get(clazz), qualifiers);
             }
         }
         throw new RuntimeException("Unrecognized type: " + javaType.getName());
     }
 
     public TypeInfo<?> getTypeFromSchema(int jdbcTypeTypeQualifiers qualifiers)
     {
         if (.containsKey(jdbcType))
         {
             // If it is an unlimited text type, add unlimited string length to the qualifiers
             if (.contains(jdbcType))
             {
                 qualifiers = qualifiers.stringLength(.);
             }
             return findTypeWithQualifiers(.get(jdbcType), qualifiers);
         }
         else
         {
             return null;
         }
     }
 
     private TypeInfo<?> findTypeWithQualifiers(Iterable<TypeInfo<?>> typesTypeQualifiers qualifiers)
     {
         TypeInfo<?> acceptableType = null;
         for (TypeInfo<?> type : types)
         {
             TypeQualifiers typeQualifiers = type.getQualifiers();
             if (typeQualifiers.equals(qualifiers))
             {
                 // The preferred type mapping is one that exactly matches the requested type qualifiers
                 // (e.g. the mapping for unlimited-length strings is always used if string length == UNLIMITED).
                 return type;
             }
             else
             {
                // Otherwise we should return the next best match, which is any type whose qualifiers can be set
                // in a way that accommodates the requested qualifiers. The constraint we have is whether the type
                // supports unlimited string length or not.
                if (typeQualifiers.isUnlimitedStringLengthSupportCompatible(qualifiers))
                {
                    acceptableType = type;
                }
            }
        }
        return acceptableType != null ? acceptableType.withQualifiers(qualifiers) : null;
    }
    public static TypeManager derby()
    {
        return new TypeManager.Builder()
                .addMapping(blobType(), schemaType("BLOB"))
                .addMapping(booleanType(), schemaType("SMALLINT").jdbcWriteType().precisionAllowed(true), qualifiers().precision(1))
                .addMapping(dateType(), schemaType("DATETIME"))
                .addMapping(doubleType(), schemaType("DOUBLE"))
                .addMapping(integerType(), schemaType("INTEGER"))
                .addMapping(longType(), schemaType("BIGINT"))
                .addStringTypes("VARCHAR""CLOB".)
                .build();
    }
    public static TypeManager hsql()
    {
        return new TypeManager.Builder()
                .addMapping(blobType(), schemaType("LONGVARBINARY"))
                .addMapping(booleanType(), schemaType("BOOLEAN"))
                .addMapping(dateType(), schemaType("DATETIME"))
                .addMapping(doubleType(), schemaType("DOUBLE"))
                .addMapping(integerType(), schemaType("INTEGER"))
                .addMapping(longType(), schemaType("BIGINT"))
                .addStringTypes("VARCHAR""LONGVARCHAR", 16 * 1024 * 1024)//LONGVARCHAR defaults to 16M
                .build();
    }
    public static TypeManager mysql()
    {
        return new TypeManager.Builder()
                .addMapping(blobType(), schemaType("BLOB"))
                .addMapping(booleanType(), schemaType("BOOLEAN"))
                .addMapping(dateType(), schemaType("DATETIME"))
                .addMapping(doubleType(), schemaType("DOUBLE"))
                .addMapping(integerType(), schemaType("INTEGER"))
                .addMapping(longType(), schemaType("BIGINT"))
                .addStringTypes("VARCHAR""LONGTEXT".)
                .build();
    }
    public static TypeManager postgres()
    {
        return new TypeManager.Builder()
                .addMapping(blobType(), schemaType("BYTEA"))
                .addMapping(booleanType(), schemaType("BOOLEAN"))
                .addMapping(dateType(), schemaType("TIMESTAMP"))
                .addMapping(doubleType(), schemaType("DOUBLE PRECISION"))
                .addMapping(integerType(), schemaType("INTEGER"))
                .addMapping(longType(), schemaType("BIGINT"))
                .addStringTypes("VARCHAR""TEXT", 1024 * 1024 * 1024)//TEXT defaults to 1G
                .build();
    }
    public static TypeManager sqlServer()
    {
        return new TypeManager.Builder()
                .addMapping(blobType(), schemaType("IMAGE"))
                .addMapping(booleanType(), schemaType("BIT"))
                .addMapping(dateType(), schemaType("DATETIME"))
                .addMapping(doubleType(), schemaType("FLOAT"))
                .addMapping(integerType(), schemaType("INTEGER"))
                .addMapping(longType(), schemaType("BIGINT"))
                .addStringTypes("VARCHAR""NTEXT".)
                .build();
    }
    public static TypeManager oracle()
    {
        return new TypeManager.Builder()
                .addMapping(blobType(), schemaType("BLOB"))
                .addMapping(booleanType(), schemaType("NUMBER").precisionAllowed(true), qualifiers().precision(1))
                .addMapping(dateType(), schemaType("TIMESTAMP"))
                .addMapping(doubleType(), schemaType("DOUBLE PRECISION"))
                .addMapping(integerType(), schemaType("NUMBER").precisionAllowed(true), qualifiers().precision(11))
                .addMapping(longType(), schemaType("NUMBER").precisionAllowed(true), qualifiers().precision(20))
                .addStringTypes("VARCHAR""CLOB".)
                .build();
    }
    public static class Builder
    {
        private final SetMultimap<Class<?>, TypeInfo<?>> classIndex = HashMultimap.create();
        private final SetMultimap<IntegerTypeInfo<?>> jdbcTypeIndex = HashMultimap.create();
        public TypeManager build()
        {
            return new TypeManager(this);
        }
        public <T> Builder addMapping(LogicalType<T> logicalTypeSchemaProperties schemaProperties)
        {
            return addMapping(logicalTypeschemaPropertiesqualifiers());
        }
        public <T> Builder addMapping(LogicalType<T> logicalTypeSchemaProperties schemaPropertiesTypeQualifiers qualifiers)
        {
            TypeInfo<T> typeInfo = new TypeInfo<T>(logicalTypeschemaPropertiesqualifiers);
            for (Class<?> clazz : logicalType.getTypes())
            {
                .put(clazztypeInfo);
            }
            for (Integer jdbcType : logicalType.getJdbcReadTypes())
            {
                .put(jdbcTypetypeInfo);
            }
            return this;
        }
        public Builder addStringTypes(String limitedStringSqlTypeString unlimitedStringSqlTypefinal int precision)
        {
            addMapping(stringType(), schemaType(limitedStringSqlType).stringLengthAllowed(true),
                       qualifiers().stringLength(.));
            addMapping(stringType(), schemaType(unlimitedStringSqlType).stringLengthAllowed(true).defaultValueAllowed(false),
                       qualifiers().stringLength().precision(precision));
            addMapping(enumType(), schemaType(limitedStringSqlType).stringLengthAllowed(true),
                       qualifiers().stringLength(.));
            addMapping(uriType(), schemaType(limitedStringSqlType).stringLengthAllowed(true),
                       qualifiers().stringLength(.));
            addMapping(uriType(), schemaType(unlimitedStringSqlType).stringLengthAllowed(true).defaultValueAllowed(false),
                       qualifiers().stringLength().precision(precision));
            addMapping(urlType(), schemaType(limitedStringSqlType).stringLengthAllowed(true),
                       qualifiers().stringLength(.));
            addMapping(urlType(), schemaType(unlimitedStringSqlType).stringLengthAllowed(true).defaultValueAllowed(false),
                       qualifiers().stringLength().precision(precision));
            return this;
        }
    }
New to GrepCode? Check out our FAQ X