com.twelvemonkeys.util.convert
Class Converter

java.lang.Object
  extended by 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:
registerConverter(Class, PropertyConverter), unregisterConverter(Class)

Field Summary
protected  Map<Class,PropertyConverter> converters
          The converters Map
protected static Converter sInstance
          Our singleton instance
 
Constructor Summary
protected Converter()
          Creates a Converter.
 
Method Summary
static Converter getInstance()
          Gets the Converter instance.
static void registerConverter(Class<?> pType, PropertyConverter pConverter)
          Registers a converter for a given type.
 Object toObject(String pString, Class pType)
          Converts the string to an object of the given type.
abstract  Object toObject(String pString, Class pType, String pFormat)
          Converts the string to an object of the given type, parsing after the given format.
 String toString(Object pObject)
          Converts the object to a string, using object.toString()
abstract  String toString(Object pObject, String pFormat)
          Converts the object to a string, using object.toString()
static void unregisterConverter(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 Map<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(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(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 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.


Copyright © 2014. All Rights Reserved.