Class DefaultConverter

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

public final class DefaultConverter extends Object implements PropertyConverter
Converts strings to objects and back.

This converter first tries to create an object, using the class' single string argument constructor (<type>(String)) if found, otherwise, an attempt to call the class' static valueOf(String) method. If both fails, a ConversionException is thrown.

Version:
$Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/convert/DefaultConverter.java#2 $
Author:
Harald Kuhr, last modified by $Author: haku $
  • Constructor Details

    • DefaultConverter

      public DefaultConverter()
      Creates a DefaultConverter.
  • Method Details

    • toObject

      public Object toObject(String pString, Class pType, String pFormat) throws ConversionException
      Converts the string to an object of the given type.
      Specified by:
      toObject in interface PropertyConverter
      Parameters:
      pString - the string to convert
      pType - the type to convert to
      pFormat - ignored.
      Returns:
      the object created from the given string.
      Throws:
      ConversionException - if the type is null, or if the string cannot be converted into the given type, using a string constructor or static valueOf method.
    • toString

      public String toString(Object pObject, String pFormat) throws ConversionException
      Converts the object to a string, using pObject.toString().
      Specified by:
      toString in interface PropertyConverter
      Parameters:
      pObject - the object to convert.
      pFormat - ignored.
      Returns:
      the string representation of the object, or null if pObject == null
      Throws:
      ConversionException - if the string could not be converted to the specified type and format.