|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.geotools.resources.ClassChanger<S,T>
public abstract class ClassChanger<S extends java.lang.Comparable<S>,T extends java.lang.Number>
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);
Constructor Summary | |
---|---|
protected |
ClassChanger(java.lang.Class<S> source,
java.lang.Class<T> target)
Constructs a new class changer. |
Method Summary | ||
---|---|---|
static
|
cast(java.lang.Number n,
java.lang.Class<N> c)
Casts the number to the specified class. |
|
protected abstract T |
convert(S object)
Returns the numerical value for an object. |
|
static java.lang.Class<? extends java.lang.Number> |
getFinestClass(java.lang.Class<? extends java.lang.Number> c1,
java.lang.Class<? extends java.lang.Number> c2)
Returns the class of the finest type. |
|
static java.lang.Class<? extends java.lang.Number> |
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<? extends java.lang.Number> |
getWidestClass(java.lang.Class<? extends java.lang.Number> c1,
java.lang.Class<? extends java.lang.Number> c2)
Returns the class of the widest type. |
|
static java.lang.Class<? extends java.lang.Number> |
getWidestClass(java.lang.Number n1,
java.lang.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
|
toComparable(java.lang.Number value,
java.lang.Class<C> classe)
Wraps 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)
Converts 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)
Converts 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 |
---|
protected ClassChanger(java.lang.Class<S> source, java.lang.Class<T> target)
source
- Parent class for convert(S)
's input objects.target
- Parent class for convert(S)
's output objects.Method Detail |
---|
protected abstract T convert(S object) throws java.lang.ClassCastException
object
- Object to convert (may be null).
java.lang.ClassCastException
- if object
is not of the expected class.protected abstract S inverseConvert(T value)
value
- The value to wrap.
public java.lang.String toString()
toString
in class java.lang.Object
public static void register(ClassChanger<?,?> converter) throws java.lang.IllegalStateException
ClassChanger
will
be taken in account by the toNumber(java.lang.Comparable>)
method. The example below
register a conversion for the Date
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()); } });
converter
- The ClassChanger
to add.
java.lang.IllegalStateException
- if an other ClassChanger
was already
registered for the same source
class. This is usually
not a concern since the registration usually take place during the
class initialization ("static" constructor).public static java.lang.Class<?> getTransformedClass(java.lang.Class<?> source)
Comparable
subclass
that will be specified as input to convert(S)
. The target class is a
Number
subclass that will be returned as output by convert(S)
.
If no suitable mapping is found, then source
is returned.
public static java.lang.Number toNumber(java.lang.Comparable<?> object) throws java.lang.ClassNotFoundException
toNumber(new Date())
returns the Date.getTime()
value of the specified date object as a Long
.
object
- Object to convert (may be null).
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.
java.lang.ClassNotFoundException
- if object
is not an instance of a registered class.public static <C extends java.lang.Comparable> C toComparable(java.lang.Number value, java.lang.Class<C> classe) throws java.lang.ClassNotFoundException
toComparable(new Long(time), Date.class)
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.
value
- The numerical value (may be null).classe
- The desired classe for return value.
java.lang.ClassNotFoundException
- if classe
is not a registered class.public static java.lang.Class<?> toPrimitive(java.lang.Class<?> c) throws java.lang.IllegalArgumentException
Double.class
to Double.TYPE
.
c
- The wrapper class.
java.lang.IllegalArgumentException
- if the specified class is not a wrapper for a primitive.public static java.lang.Class<?> toWrapper(java.lang.Class<?> c) throws java.lang.IllegalArgumentException
Double.TYPE
to Double.class
.
c
- The primitive class.
java.lang.IllegalArgumentException
- if the specified class is not a primitive.public static <N extends java.lang.Number> N cast(java.lang.Number n, java.lang.Class<N> c) throws java.lang.IllegalArgumentException
Byte
,
Short
, Integer
, Long
, Float
or Double
.
java.lang.IllegalArgumentException
public static java.lang.Class<? extends java.lang.Number> getWidestClass(java.lang.Number n1, java.lang.Number n2) throws java.lang.IllegalArgumentException
n1
and n2
must be instance of any of Byte
, Short
, Integer
, Long
,
Float
or Double
types. At most one of the argument can be null.
java.lang.IllegalArgumentException
public static java.lang.Class<? extends java.lang.Number> getWidestClass(java.lang.Class<? extends java.lang.Number> c1, java.lang.Class<? extends java.lang.Number> c2) throws java.lang.IllegalArgumentException
c1
and c2
must be of any of Byte
, Short
, Integer
, Long
,
Float
or Double
types. At most one of the argument can be null.
java.lang.IllegalArgumentException
public static java.lang.Class<? extends java.lang.Number> getFinestClass(java.lang.Class<? extends java.lang.Number> c1, java.lang.Class<? extends java.lang.Number> c2) throws java.lang.IllegalArgumentException
c1
and c2
must be of any of Byte
, Short
, Integer
, Long
,
Float
or Double
types. At most one of the argument can be null.
java.lang.IllegalArgumentException
public static java.lang.Class<? extends java.lang.Number> getFinestClass(double value)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |