Class ClassChanger<S extends Comparable<S>,​T extends Number>


  • public abstract class ClassChanger<S extends Comparable<S>,​T extends Number>
    extends 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
    Author:
    Martin Desruisseaux (IRD)
    • Constructor Detail

      • ClassChanger

        protected ClassChanger​(Class<S> source,
                               Class<T> target)
        Constructs a new class changer.
        Parameters:
        source - Parent class for convert(S)'s input objects.
        target - Parent class for convert(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 - if object 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.
        Overrides:
        toString in class Object
      • register

        public static void register​(ClassChanger<?,​?> converter)
                             throws IllegalStateException
        Registers a new converter. All registered ClassChanger will be taken in account by the toNumber(java.lang.Comparable<K>) 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());
              }
          });
         
        Parameters:
        converter - The ClassChanger to add.
        Throws:
        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).
      • 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(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.
      • toNumber

        public static <K> K toNumber​(Comparable<K> 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 <C extends Comparable> C toComparable​(Number value,
                                                            Class<C> classe)
                                                     throws ClassNotFoundException
        Wraps the specified number as an instance of the specified classe. For example 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.
        Parameters:
        value - The numerical value (may be null).
        classe - The desired classe for return value.
        Throws:
        ClassNotFoundException - if classe 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 converts Double.class to Double.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 converts Double.TYPE to Double.class.
        Parameters:
        c - The primitive class.
        Returns:
        The wrapper class.
        Throws:
        IllegalArgumentException - if the specified class is not a primitive.
      • getFinestClass

        public static Class<? extends Number> getFinestClass​(double value)
        Returns the smallest class capable to hold the specified value.