Class Converter

  • All Implemented Interfaces:
    PropertyConverter
    Direct Known Subclasses:
    ConverterImpl

    public abstract class Converter
    extends java.lang.Object
    implements PropertyConverter
    The converter (singleton). Converts strings to objects and back. This is the entry point to the converter framework.

    By default, converters for Time, Date and Object (the DefaultConverter) are registered by this class' static initializer. You might remove them using the unregisterConverter method.

    Version:
    $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/convert/Converter.java#1 $
    See Also:
    registerConverter(Class, PropertyConverter), unregisterConverter(Class)
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Converter()
      Creates a Converter.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      static Converter getInstance()
      Gets the Converter instance.
      static void registerConverter​(java.lang.Class<?> pType, PropertyConverter pConverter)
      Registers a converter for a given type.
      java.lang.Object toObject​(java.lang.String pString, java.lang.Class pType)
      Converts the string to an object of the given type.
      abstract java.lang.Object toObject​(java.lang.String pString, java.lang.Class pType, java.lang.String pFormat)
      Converts the string to an object of the given type, parsing after the given format.
      java.lang.String toString​(java.lang.Object pObject)
      Converts the object to a string, using object.toString()
      abstract java.lang.String toString​(java.lang.Object pObject, java.lang.String pFormat)
      Converts the object to a string, using object.toString()
      static void unregisterConverter​(java.lang.Class<?> pType)
      Un-registers a converter for a given type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • sInstance

        protected static final Converter sInstance
        Our singleton instance
      • converters

        protected final java.util.Map<java.lang.Class,​PropertyConverter> converters
        The converters Map
    • Constructor Detail

      • Converter

        protected Converter()
        Creates a Converter.
    • Method Detail

      • getInstance

        public static Converter getInstance()
        Gets the Converter instance.
        Returns:
        the converter instance
      • registerConverter

        public static void registerConverter​(java.lang.Class<?> pType,
                                             PropertyConverter pConverter)
        Registers a converter for a given type. This converter will also be used for all subclasses, unless a more specific version is registered.

        By default, converters for Time, Date and Object (the DefaultConverter) are registered by this class' static initializer. You might remove them using the unregisterConverter method.

        Parameters:
        pType - the (super) type to register a converter for
        pConverter - the converter
        See Also:
        unregisterConverter(Class)
      • unregisterConverter

        public static void unregisterConverter​(java.lang.Class<?> pType)
        Un-registers a converter for a given type. That is, making it unavailable for the converter framework, and making it (potentially) available for garbage collection.
        Parameters:
        pType - the (super) type to remove converter for
        See Also:
        registerConverter(Class,PropertyConverter)
      • toObject

        public java.lang.Object toObject​(java.lang.String pString,
                                         java.lang.Class pType)
                                  throws ConversionException
        Converts the string to an object of the given type.
        Parameters:
        pString - the string to convert
        pType - the type to convert to
        Returns:
        the object created from the given string.
        Throws:
        ConversionException - if the string cannot be converted for any reason.
      • toObject

        public abstract java.lang.Object toObject​(java.lang.String pString,
                                                  java.lang.Class pType,
                                                  java.lang.String pFormat)
                                           throws ConversionException
        Converts the string to an object of the given type, parsing after the given format.
        Specified by:
        toObject in interface PropertyConverter
        Parameters:
        pString - the string to convert
        pType - the type to convert to
        pFormat - the (optional) conversion format
        Returns:
        the object created from the given string.
        Throws:
        ConversionException - if the string cannot be converted for any reason.
      • toString

        public java.lang.String toString​(java.lang.Object pObject)
                                  throws ConversionException
        Converts the object to a string, using object.toString()
        Parameters:
        pObject - the object to convert.
        Returns:
        the string representation of the object, on the correct format.
        Throws:
        ConversionException - if the object cannot be converted to a string for any reason.
      • toString

        public abstract java.lang.String toString​(java.lang.Object pObject,
                                                  java.lang.String pFormat)
                                           throws ConversionException
        Converts the object to a string, using object.toString()
        Specified by:
        toString in interface PropertyConverter
        Parameters:
        pObject - the object to convert.
        pFormat - the (optional) conversion format
        Returns:
        the string representation of the object, on the correct format.
        Throws:
        ConversionException - if the object cannot be converted to a string for any reason.