net.java.ao.types
Class TypeManager

java.lang.Object
  extended by net.java.ao.types.TypeManager

public class TypeManager
extends Object

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 singleton 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.

Author:
Daniel Spiewak
See Also:
DatabaseType

Method Summary
 void addType(DatabaseType<?> type)
          Adds a new type to the container.
static TypeManager getInstance()
          Retrieves the singleton instance of the container.
<T> DatabaseType<T>
getType(Class<T> javaType)
          Returns the corresponding DatabaseType for a given Java class.
 DatabaseType<?> getType(int sqlType)
          Returns the corresponding DatabaseType for a given JDBC integer type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

addType

public void addType(DatabaseType<?> type)
Adds a new type to the container. Once the type is added, it will be availble to every EntityManager instance. This method is used internally to set up the default types (such as int, String and so on). Any custom types should be added using this method.

Parameters:
type - The type instance to add to the container.

getType

public <T> DatabaseType<T> getType(Class<T> javaType)

Returns the corresponding DatabaseType for a given Java class. This is the primary mechanism used by ActiveObjects internally to obtain type instances. Code external to the framework may also make use of this method to obtain the relevant type information or to just test if a type is in fact available. Types are internally prioritized by entry order. The first type to respond true to the DatabaseType.isHandlerFor(Class) method will be returned.

It's worth noting that this method worst case runs in O(n) time. This is because a linear search must be made through the raw list of available types. However, once the type has been found it is placed into a hash indexed by class type. Thus for most types, this method will run in constant time (O(1)).

Parameters:
javaType - The Class type for which a type instance should be returned.
Returns:
The type instance which corresponds to the specified class.
Throws:
RuntimeException - If no type was found correspondant to the given class.
See Also:
getType(int)

getType

public DatabaseType<?> getType(int sqlType)

Returns the corresponding DatabaseType for a given JDBC integer type. Code external to the framework may also make use of this method to obtain the relevant type information or to just test if a type is in fact available. Types are internally prioritized by entry order. The first type to respond true to the DatabaseType.isHandlerFor(int) method will be returned.

It's worth noting that this method worst case runs in O(n) time. This is because a linear search must be made through the raw list of available types. However, once the type has been found it is placed into a hash indexed by int value. Thus for most types, this method will run in constant time (O(1)).

Parameters:
sqlType - The JDBC Types constant for which a type instance should be retrieved.
Returns:
The type instance which corresponds to the specified type constant.
Throws:
RuntimeException - If no type was found correspondant to the given type constant.
See Also:
getType(Class)

getInstance

public static TypeManager getInstance()
Retrieves the singleton instance of the container. This method is thread-safe and synchronized using pre-Java 5 mechanisms (meaning it may be a little slower than it could be). For optimal efficiency, do not make repeated calls.

Returns:
The global singleton instance.