Class Converter

java.lang.Object
com.twelvemonkeys.util.convert.Converter
All Implemented Interfaces:
PropertyConverter

public abstract class Converter extends 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 $
Author:
Harald Kuhr, last modified by $Author: haku $
See Also:
  • Field Details

  • Constructor Details

    • Converter

      protected Converter()
      Creates a Converter.
  • Method Details

    • getInstance

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

      public static void registerConverter(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

      public static void unregisterConverter(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:
    • toObject

      public Object toObject(String pString, 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 Object toObject(String pString, Class pType, 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 String toString(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 String toString(Object pObject, 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.