deegree 2.1 (2007/11/08 09:57 build-328-official)

org.deegree.model.csct.resources
Class ClassChanger

java.lang.Object
  extended by org.deegree.model.csct.resources.ClassChanger

public abstract class ClassChanger
extends Object

Transforme un objet d'une classe vers une autre. Cette classe sert principalement � convertir en Number des objets d'une autre classe, par exemple Date. Une m�thode statique, toNumber(java.lang.Comparable), se charge d'effectuer ce genre de conversion en prenant en compte toutes les classes qui auront �t� d�clar�es � ClassChanger.

Pour d�clarer une nouvelle classe, on peut proc�der comme suit. L'exemple ci-dessous inscrit une classe qui convertira des objets Date en objets Long. Notez qu'il ne s'agit que d'un exemple. Ce convertisseur n'a pas besoin d'�tre d�clar� car ClassChanger comprend d�j� les objets Date par d�faut.

 ClassChanger.register( new ClassChanger( Date.class, Long.class ) {
     protected Number convert( final Comparable o ) {
         return new Long( ( (Date) o ).getTime() );
     }
 
     protected Comparable inverseConvert( final Number number ) {
         return new Date( number.longValue() );
     }
 } );
 

Version:
1.0
Author:
Martin Desruisseaux

Constructor Summary
protected ClassChanger(Class source, Class target)
          Construct a new class changer.
 
Method Summary
protected abstract  Number convert(Comparable object)
          Returns the numerical value for an object.
static Class getTransformedClass(Class source)
          Returns the target class for the specified source class, if a suitable transformation is known.
protected abstract  Comparable inverseConvert(Number value)
          Returns an instance of the converted classe from a numerical value.
static void register(ClassChanger converter)
          Inscrit un nouvel objet ClassChanger.
static Comparable toComparable(Number value, Class classe)
          Wrap the specified number as an instance of the specified classe.
static Number toNumber(Comparable object)
          Returns the numeric value for the specified object.
 String toString()
          Returns a string representation for this class changer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassChanger

protected ClassChanger(Class source,
                       Class target)
Construct a new class changer.

Parameters:
source - Parent class for convert(java.lang.Comparable)'s input objects.
target - Parent class for convert(java.lang.Comparable)'s output objects.
Method Detail

convert

protected abstract Number convert(Comparable object)
                           throws ClassCastException
Returns the numerical value for an object.

Parameters:
object - Object to convert (may be null).
Returns:
The object's numerical value.
Throws:
ClassCastException - if object is not of the expected class.

inverseConvert

protected abstract Comparable inverseConvert(Number value)
Returns an instance of the converted classe from a numerical value.

Parameters:
value - The value to wrap.
Returns:
An instance of the source classe.

toString

public String toString()
Returns a string representation for this class changer.

Overrides:
toString in class Object

register

public static void register(ClassChanger converter)
                     throws IllegalStateException
Inscrit un nouvel objet ClassChanger. Les objets ClassChanger inscrits ici seront pris en compte par la m�thode toNumber(java.lang.Comparable). Si un objet ClassChanger existait d�j� pour une m�me classe, une exception sera lanc�e. Cette sp�cification est justifi�e par le fait qu'on enregistre souvent un objet ClassChanger lors de l'initialisation d'une classe qui vient d'�tre charg�e pour la premi�re fois. En interdisant tout changements aux objets ClassChanger apr�s l'initialisation d'une classe, on �vite que la fa�on de convertir des objets en nombres r�els ne change au cours d'une ex�cution de la machine virtuelle. Notez que si converter ne peut pas prendre en charge une m�me classe que celle d'un autre objet ClassChanger, il peut toutefois prendre en charge une classe parente ou une classe fille.

Parameters:
converter - Convertisseur � ajouter � la liste des convertisseurs d�j� existants.
Throws:
IllegalStateException - si un autre objet ClassChanger prennait d�j� en charge la m�me classe (l'argument classe d�clar� au constructeur) que converter.

getTransformedClass

public static Class getTransformedClass(Class source)
Returns the target class for the specified source class, if a suitable transformation is known. The source class is a Comparable subclass that will be specified as input to convert(java.lang.Comparable). The target class is a Number subclass that wimm be returned as output by convert(java.lang.Comparable). If no suitable mapping is found, then source is returned.


toNumber

public static Number toNumber(Comparable object)
                       throws ClassNotFoundException
Returns the numeric value for the specified object. For example the code toNumber(new Date()) returns the Date.getTime() value of the specified date object as a Long.

Parameters:
object - Object to convert (may be null).
Returns:
null if object was null; otherwise object if the supplied object is already an instance of Number; otherwise a new number with the numerical value.
Throws:
ClassNotFoundException - if object is not an instance of a registered class.

toComparable

public static Comparable toComparable(Number value,
                                      Class classe)
                               throws ClassNotFoundException
Wrap the specified number as an instance of the specified classe. For example toComparable(Date.class, new Long(time)) is equivalent to new Date(time). There is of course no point to use this method if the destination class is know at compile time. This method is useful for creating instance of classes choosen dynamically at run time.

Parameters:
value - The numerical value (may be null).
classe - The desired classe for return value.
Throws:
ClassNotFoundException - if classe is not a registered class.

deegree 2.1 (2007/11/08 09:57 build-328-official)

an open source project founded by lat/lon, Bonn, Germany.
For more information visit: http://deegree.sourceforge.net