org.geotools.resources
Class ClassChanger

java.lang.Object
  extended byorg.geotools.resources.ClassChanger

public abstract class ClassChanger
extends java.lang.Object

A central place to register transformations between an arbitrary class and a Number. For example, it is sometime convenient to consider Date objects as if they were Long objects for computation purpose in generic algorithms. Client can call the following method to convert an arbitrary object to a Number:

 Object someArbitraryObject = new Date();
 Number myObjectAsANumber = ClassChanger.toNumber(someArbitraryObject);
 

Since:
2.0
Version:
$Id: ClassChanger.java 17672 2006-01-19 00:25:55Z desruisseaux $
Author:
Martin Desruisseaux

Constructor Summary
protected ClassChanger(java.lang.Class source, java.lang.Class target)
          Construct a new class changer.
 
Method Summary
static java.lang.Number cast(java.lang.Number n, java.lang.Class c)
          Cast the number to the specified class.
protected abstract  java.lang.Number convert(java.lang.Comparable object)
          Returns the numerical value for an object.
static java.lang.Class getFinestClass(java.lang.Class c1, java.lang.Class c2)
          Returns the class of the finest type.
static java.lang.Class getFinestClass(double value)
          Returns the smallest class capable to hold the specified value.
static java.lang.Class getTransformedClass(java.lang.Class source)
          Returns the target class for the specified source class, if a suitable transformation is known.
static java.lang.Class getWidestClass(java.lang.Class c1, java.lang.Class c2)
          Returns the class of the widest type.
static java.lang.Class getWidestClass(java.lang.Number n1, java.lang.Number n2)
          Returns the class of the widest type.
protected abstract  java.lang.Comparable inverseConvert(java.lang.Number value)
          Returns an instance of the converted classe from a numerical value.
static void register(ClassChanger converter)
          Register a new transformation.
static java.lang.Comparable toComparable(java.lang.Number value, java.lang.Class classe)
          Wrap the specified number as an instance of the specified classe.
static java.lang.Number toNumber(java.lang.Comparable object)
          Returns the numeric value for the specified object.
static java.lang.Class toPrimitive(java.lang.Class c)
          Convert a wrapper class to a primitive class.
 java.lang.String toString()
          Returns a string representation for this class changer.
static java.lang.Class toWrapper(java.lang.Class c)
          Convert a primitive class to a wrapper class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ClassChanger

protected ClassChanger(java.lang.Class source,
                       java.lang.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 java.lang.Number convert(java.lang.Comparable object)
                                     throws java.lang.ClassCastException
Returns the numerical value for an object.

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

inverseConvert

protected abstract java.lang.Comparable inverseConvert(java.lang.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 java.lang.String toString()
Returns a string representation for this class changer.


register

public static void register(ClassChanger converter)
                     throws java.lang.IllegalStateException
Register a new transformation. All registered ClassChanger will be taken in account by the toNumber(java.lang.Comparable) method. The example below register a transformation for the Date class:
  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());
      }
  });
 

Parameters:
converter - The ClassChanger to add.
Throws:
java.lang.IllegalStateException - if an other ClassChanger was already registered for the same class. This is usually not a concern since the registration usually take place during the class initialization ("static" constructor).

getTransformedClass

public static java.lang.Class getTransformedClass(java.lang.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 will be returned as output by convert(java.lang.Comparable). If no suitable mapping is found, then is returned.


toNumber

public static java.lang.Number toNumber(java.lang.Comparable object)
                                 throws java.lang.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:
if was null; otherwise if the supplied object is already an instance of Number; otherwise a new number with the numerical value.
Throws:
java.lang.ClassNotFoundException - if is not an instance of a registered class.

toComparable

public static java.lang.Comparable toComparable(java.lang.Number value,
                                                java.lang.Class classe)
                                         throws java.lang.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:
java.lang.ClassNotFoundException - if is not a registered class.

toPrimitive

public static java.lang.Class toPrimitive(java.lang.Class c)
                                   throws java.lang.IllegalArgumentException
Convert a wrapper class to a primitive class. For example this method converts Double.class to Double.TYPE.

Parameters:
c - The wrapper class.
Returns:
The primitive class.
Throws:
java.lang.IllegalArgumentException - if the specified class is not a wrapper for a primitive.

toWrapper

public static java.lang.Class toWrapper(java.lang.Class c)
                                 throws java.lang.IllegalArgumentException
Convert a primitive class to a wrapper class. For example this method converts Double.TYPE to Double.class.

Parameters:
c - The primitive class.
Returns:
The wrapper class.
Throws:
java.lang.IllegalArgumentException - if the specified class is not a primitive.

cast

public static java.lang.Number cast(java.lang.Number n,
                                    java.lang.Class c)
Cast the number to the specified class. The class must by one of Byte, Short, Integer, Long, Float or Double.


getWidestClass

public static java.lang.Class getWidestClass(java.lang.Number n1,
                                             java.lang.Number n2)
Returns the class of the widest type. Numbers and must be instance of any of Byte, Short, Integer, Long, Float or Double types. At most one of the argument can be null.


getWidestClass

public static java.lang.Class getWidestClass(java.lang.Class c1,
                                             java.lang.Class c2)
Returns the class of the widest type. Classes and must be of any of Byte, Short, Integer, Long, Float or Double types. At most one of the argument can be null.


getFinestClass

public static java.lang.Class getFinestClass(java.lang.Class c1,
                                             java.lang.Class c2)
Returns the class of the finest type. Classes and must be of any of Byte, Short, Integer, Long, Float or Double types. At most one of the argument can be null.


getFinestClass

public static java.lang.Class getFinestClass(double value)
Returns the smallest class capable to hold the specified value.



Copyright © GeoTools. All Rights Reserved.