Class DefaultConverter

  • All Implemented Interfaces:
    PropertyConverter

    public final class DefaultConverter
    extends java.lang.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 $
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultConverter()
      Creates a DefaultConverter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private java.lang.String arrayToString​(java.lang.Object[] pArray, java.lang.String pFormat)  
      private java.lang.Object toArray​(java.lang.String pString, java.lang.Class pType, java.lang.String pFormat)  
      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.
      private java.lang.Object[] toObjectArray​(java.lang.Object pObject)  
      java.lang.String toString​(java.lang.Object pObject, java.lang.String pFormat)
      Converts the object to a string, using pObject.toString().
      private java.lang.Class<?> unBoxType​(java.lang.Class<?> pType)  
      • Methods inherited from class java.lang.Object

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

      • DefaultConverter

        public DefaultConverter()
        Creates a DefaultConverter.
    • Method Detail

      • toObject

        public 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.
        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.
      • toArray

        private java.lang.Object toArray​(java.lang.String pString,
                                         java.lang.Class pType,
                                         java.lang.String pFormat)
      • toString

        public java.lang.String toString​(java.lang.Object pObject,
                                         java.lang.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.
      • arrayToString

        private java.lang.String arrayToString​(java.lang.Object[] pArray,
                                               java.lang.String pFormat)
      • toObjectArray

        private java.lang.Object[] toObjectArray​(java.lang.Object pObject)
      • unBoxType

        private java.lang.Class<?> unBoxType​(java.lang.Class<?> pType)