org.ckkloverdos.type.java
Class JavaTypeRegistry

java.lang.Object
  extended by org.ckkloverdos.type.java.JavaTypeRegistry
All Implemented Interfaces:
IToStringAware

public class JavaTypeRegistry
extends java.lang.Object
implements IToStringAware

A JavaType registry, able to remember type aliases. Also, in case a primitive type needs to be handled uniformly as its respective object type (e.g. int and java.lang.Integer), then their classes can be registered with the setRepresentative(Class, Class) or setRepresentative(Class, JavaType) methods. For example:

 JavaTypeRegistry r = new JavaTypeRegistry();
 r.setRepresentative(int.class, Integer.class);
 

Representatives must be registered before anything else.

The common practice for an application is to have a singleton registry that internally uses an instance of JavaTypeRegistry. ObjectJavaTypes uses this idiom.

Author:
Christos KK Loverdos
See Also:
JavaType, ObjectJavaTypes

Constructor Summary
JavaTypeRegistry()
          The only constructor.
 
Method Summary
 JavaTypeRegistry copy()
          Returns a new instance having exactly the same mappings maintained by this registry.
 JavaType getByClass(java.lang.Class c)
          Returns the JavaType wrapping class c.
 JavaType getByName(java.lang.String name)
          Returns the JavaType for the alias represented by name.
 java.lang.Class getRepresentative(java.lang.Class what)
          Returns the representative of class what or null if no representative has been set for class what.
 java.util.Map getRepresentatives()
          Returns the representatives mapping.
 boolean is(java.lang.Class a, java.lang.Class b)
          Checks if the two types are equal, taking representatives into account.
 boolean is(java.lang.Class a, JavaType b)
          Checks if the two types are equal, taking representatives into account.
 boolean is(java.lang.Class a, java.lang.String b)
          Checks if the two types are equal, taking representatives into account.
 boolean is(JavaType a, JavaType b)
          Checks if the two types are equal, taking representatives into account.
 boolean is(JavaType a, java.lang.String b)
          Checks if the two types are equal, taking representatives into account.
 JavaType register(java.lang.Class c)
          Returns the JavaType wrapping class c or creates a new one if it doesnot exist in the registry.
 JavaType register(JavaType javaType)
          Registers javaType for the java class it wraps.
 JavaType register(java.lang.String alias, java.lang.Class c)
          Registers the alias for the java class c.
 JavaType register(java.lang.String alias, JavaType type)
          Registers the alias for the class wrapped by type.
 void setRepresentative(java.lang.Class original, java.lang.Class representative)
          Sets representative to be the representative class of original class.
 void setRepresentative(java.lang.Class original, JavaType representativeType)
          Sets the java class wrapped by representativeType to be the representative class of the original class.
 java.lang.String toString()
           
 void toStringAware(ToString ts)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JavaTypeRegistry

public JavaTypeRegistry()
The only constructor.

Method Detail

setRepresentative

public void setRepresentative(java.lang.Class original,
                              java.lang.Class representative)
Sets representative to be the representative class of original class. This is usually used for primitive classes and their corresponding object classes.

Parameters:
original -
representative -

setRepresentative

public void setRepresentative(java.lang.Class original,
                              JavaType representativeType)
Sets the java class wrapped by representativeType to be the representative class of the original class.

Parameters:
original -
representativeType -

getRepresentative

public java.lang.Class getRepresentative(java.lang.Class what)
Returns the representative of class what or null if no representative has been set for class what.

Parameters:
what -

getRepresentatives

public java.util.Map getRepresentatives()
Returns the representatives mapping. The keys are the original classes and the values are their representatives.


register

public JavaType register(java.lang.Class c)
Returns the JavaType wrapping class c or creates a new one if it doesnot exist in the registry. if the class c has a representative, the JavaType of its representative is returned. In any case, if the JavaType doesnot exist it is created in-place.

Parameters:
c -

register

public JavaType register(JavaType javaType)
Registers javaType for the java class it wraps. If the wrapped java class has a representative, then javaType is registered for the representative instead.

Parameters:
javaType -

register

public JavaType register(java.lang.String alias,
                         java.lang.Class c)
Registers the alias for the java class c. If c has a representative, then the alias is registered for the representative instead.

If this alias registration is happening before any other registration for class c, then a new JavaType is created for class c and registered as well. So, the following code:

 r.register("INTEGER", Integer.class)
 
is equivalent to:
 r.register(Integer.class)
 r.register("INTEGER", Integer.class)
 

Parameters:
alias -
c -
Returns:
the JavaType representing the alias.

register

public JavaType register(java.lang.String alias,
                         JavaType type)
Registers the alias for the class wrapped by type. In effect, this is equivalent to register(alias, type.getJavaClass()).

Parameters:
alias -
type -
Returns:
the JavaType created for the alias.

getByClass

public JavaType getByClass(java.lang.Class c)
Returns the JavaType wrapping class c. If c has a representative, then it returns the JavaType wrapping the representative.

Returns:
the registered JavaType or null if none is found in the registry.

getByName

public JavaType getByName(java.lang.String name)
Returns the JavaType for the alias represented by name. If the alias refers to a java class that has a representative, then the representative's JavaType is returned instead.

Returns:
the registered JavaType or null if none is found in the registry.

copy

public JavaTypeRegistry copy()
Returns a new instance having exactly the same mappings maintained by this registry.


toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

toStringAware

public void toStringAware(ToString ts)
Specified by:
toStringAware in interface IToStringAware

is

public boolean is(JavaType a,
                  JavaType b)
Checks if the two types are equal, taking representatives into account.


is

public boolean is(JavaType a,
                  java.lang.String b)
Checks if the two types are equal, taking representatives into account.


is

public boolean is(java.lang.Class a,
                  JavaType b)
Checks if the two types are equal, taking representatives into account.


is

public boolean is(java.lang.Class a,
                  java.lang.Class b)
Checks if the two types are equal, taking representatives into account.


is

public boolean is(java.lang.Class a,
                  java.lang.String b)
Checks if the two types are equal, taking representatives into account.



Copyright © 1999-2007 Christos KK Loverdos. All Rights Reserved.