Package org.geotools.util
Class ClassChanger<S extends Comparable<S>,T extends Number>
- Object
-
- ClassChanger<S,T>
-
public abstract class ClassChanger<S extends Comparable<S>,T extends Number> extends Object
A central place to register transformations between an arbitrary class and aNumber
. For example, it is sometime convenient to considerDate
objects as if they wereLong
objects for computation purpose in generic algorithms. Client can call the following method to convert an arbitrary object to aNumber
:Object someArbitraryObject = new Date(); Number myObjectAsANumber =
ClassChanger.toNumber
(someArbitraryObject);- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
ClassChanger(Class<S> source, Class<T> target)
Constructs a new class changer.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static <N extends Number>
Ncast(Number n, Class<N> c)
Casts the number to the specified class.protected abstract T
convert(S object)
Returns the numerical value for an object.static Class<? extends Number>
getFinestClass(double value)
Returns the smallest class capable to hold the specified value.static Class<? extends Number>
getFinestClass(Class<? extends Number> c1, Class<? extends Number> c2)
Returns the class of the finest type.static Class<?>
getTransformedClass(Class<?> source)
Returns the target class for the specified source class, if a suitable transformation is known.static Class<? extends Number>
getWidestClass(Class<? extends Number> c1, Class<? extends Number> c2)
Returns the class of the widest type.static Class<? extends Number>
getWidestClass(Number n1, Number n2)
Returns the class of the widest type.protected abstract S
inverseConvert(T value)
Returns an instance of the converted classe from a numerical value.static void
register(ClassChanger<?,?> converter)
Registers a new converter.static <C extends Comparable>
CtoComparable(Number value, Class<C> classe)
Wraps the specified number as an instance of the specified classe.static <K> K
toNumber(Comparable<K> object)
Returns the numeric value for the specified object.static Class<?>
toPrimitive(Class<?> c)
Converts a wrapper class to a primitive class.String
toString()
Returns a string representation for this class changer.static Class<?>
toWrapper(Class<?> c)
Converts a primitive class to a wrapper class.
-
-
-
Constructor Detail
-
ClassChanger
protected ClassChanger(Class<S> source, Class<T> target)
Constructs a new class changer.- Parameters:
source
- Parent class forconvert(S)
's input objects.target
- Parent class forconvert(S)
's output objects.
-
-
Method Detail
-
convert
protected abstract T convert(S 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
- ifobject
is not of the expected class.
-
inverseConvert
protected abstract S inverseConvert(T 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.
-
register
public static void register(ClassChanger<?,?> converter) throws IllegalStateException
Registers a new converter. All registeredClassChanger
will be taken in account by thetoNumber(java.lang.Comparable<K>)
method. The example below register a conversion for theDate
class:ClassChanger.register(new ClassChanger(Date.class, Long.class) { protected Long convert(final Comparable o) { return ((Date) o).getTime(); } protected Comparable inverseConvert(final Number number) { return new Date(number.longValue()); } });
- Parameters:
converter
- TheClassChanger
to add.- Throws:
IllegalStateException
- if an otherClassChanger
was already registered for the samesource
class. This is usually not a concern since the registration usually take place during the class initialization ("static" constructor).
-
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 aComparable
subclass that will be specified as input toconvert(S)
. The target class is aNumber
subclass that will be returned as output byconvert(S)
. If no suitable mapping is found, thensource
is returned.
-
toNumber
public static <K> K toNumber(Comparable<K> object) throws ClassNotFoundException
Returns the numeric value for the specified object. For example the codetoNumber(new Date())
returns theDate.getTime()
value of the specified date object as aLong
.- Parameters:
object
- Object to convert (may be null).- Returns:
null
ifobject
was null; otherwiseobject
if the supplied object is already an instance ofNumber
; otherwise a new number with the numerical value.- Throws:
ClassNotFoundException
- ifobject
is not an instance of a registered class.
-
toComparable
public static <C extends Comparable> C toComparable(Number value, Class<C> classe) throws ClassNotFoundException
Wraps the specified number as an instance of the specified classe. For exampletoComparable(new Long(time), Date.class)
is equivalent tonew 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
- ifclasse
is not a registered class.
-
toPrimitive
public static Class<?> toPrimitive(Class<?> c) throws IllegalArgumentException
Converts a wrapper class to a primitive class. For example this method convertsDouble.class
toDouble.TYPE
.- Parameters:
c
- The wrapper class.- Returns:
- The primitive class.
- Throws:
IllegalArgumentException
- if the specified class is not a wrapper for a primitive.
-
toWrapper
public static Class<?> toWrapper(Class<?> c) throws IllegalArgumentException
Converts a primitive class to a wrapper class. For example this method convertsDouble.TYPE
toDouble.class
.- Parameters:
c
- The primitive class.- Returns:
- The wrapper class.
- Throws:
IllegalArgumentException
- if the specified class is not a primitive.
-
cast
public static <N extends Number> N cast(Number n, Class<N> c) throws IllegalArgumentException
Casts the number to the specified class. The class must by one ofByte
,Short
,Integer
,Long
,Float
orDouble
.- Throws:
IllegalArgumentException
-
getWidestClass
public static Class<? extends Number> getWidestClass(Number n1, Number n2) throws IllegalArgumentException
Returns the class of the widest type. Numbersn1
andn2
must be instance of any ofByte
,Short
,Integer
,Long
,Float
orDouble
types. At most one of the argument can be null.- Throws:
IllegalArgumentException
-
getWidestClass
public static Class<? extends Number> getWidestClass(Class<? extends Number> c1, Class<? extends Number> c2) throws IllegalArgumentException
Returns the class of the widest type. Classesc1
andc2
must be of any ofByte
,Short
,Integer
,Long
,Float
orDouble
types. At most one of the argument can be null.- Throws:
IllegalArgumentException
-
getFinestClass
public static Class<? extends Number> getFinestClass(Class<? extends Number> c1, Class<? extends Number> c2) throws IllegalArgumentException
Returns the class of the finest type. Classesc1
andc2
must be of any ofByte
,Short
,Integer
,Long
,Float
orDouble
types. At most one of the argument can be null.- Throws:
IllegalArgumentException
-
-