Package com.twelvemonkeys.util.convert
Class Converter
- java.lang.Object
-
- com.twelvemonkeys.util.convert.Converter
-
- All Implemented Interfaces:
PropertyConverter
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
andObject
(theDefaultConverter
) are registered by this class' static initializer. You might remove them using theunregisterConverter
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:
registerConverter(Class, PropertyConverter)
,unregisterConverter(Class)
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.Class,PropertyConverter>
converters
The converters Mapprotected static Converter
sInstance
Our singleton instance
-
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, usingobject.toString()
abstract java.lang.String
toString(java.lang.Object pObject, java.lang.String pFormat)
Converts the object to a string, usingobject.toString()
static void
unregisterConverter(java.lang.Class<?> pType)
Un-registers a converter for a given type.
-
-
-
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
-
-
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
andObject
(theDefaultConverter
) are registered by this class' static initializer. You might remove them using theunregisterConverter
method.- Parameters:
pType
- the (super) type to register a converter forpConverter
- 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 convertpType
- 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 interfacePropertyConverter
- Parameters:
pString
- the string to convertpType
- the type to convert topFormat
- 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, usingobject.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, usingobject.toString()
- Specified by:
toString
in interfacePropertyConverter
- 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.
-
-