Package com.twelvemonkeys.util.convert
Class NumberConverter
- java.lang.Object
-
- com.twelvemonkeys.util.convert.NumberConverter
-
- All Implemented Interfaces:
PropertyConverter
- Direct Known Subclasses:
DateConverter,TimeConverter
public class NumberConverter extends java.lang.Object implements PropertyConverter
Converts strings to numbers and back.This class has a static cache of
NumberFormats, to avoid creation and parsing of number formats every time one is used.- Version:
- $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/util/convert/NumberConverter.java#2 $
-
-
Field Summary
Fields Modifier and Type Field Description private static java.text.NumberFormatsDefaultFormatprivate static java.util.Map<java.lang.String,java.text.Format>sFormatsprivate static java.text.DecimalFormatSymbolsSYMBOLS
-
Constructor Summary
Constructors Constructor Description NumberConverter()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.text.FormatgetFormat(java.lang.Class pFormatterClass, java.lang.Object... pFormat)private java.text.NumberFormatgetNumberFormat(java.lang.String pFormat)java.lang.ObjecttoObject(java.lang.String pString, java.lang.Class pType, java.lang.String pFormat)Converts the string to a number, using the given format for parsing.java.lang.StringtoString(java.lang.Object pObject, java.lang.String pFormat)Converts the object to a string, using the given format
-
-
-
Method Detail
-
toObject
public java.lang.Object toObject(java.lang.String pString, java.lang.Class pType, java.lang.String pFormat) throws ConversionExceptionConverts the string to a number, using the given format for parsing.- Specified by:
toObjectin interfacePropertyConverter- Parameters:
pString- the string to convert.pType- the type to convert to. PropertyConverter implementations may choose to ignore this parameter.pFormat- the format used for parsing. PropertyConverter implementations may choose to ignore this parameter. Also, implementations that require a parser format, should provide a default format, and allownullas the format argument.- Returns:
- the object created from the given string. May safely be typecast
to
java.lang.Numberor the class of thetypeparameter. - Throws:
ConversionException- See Also:
Number,NumberFormat
-
toString
public java.lang.String toString(java.lang.Object pObject, java.lang.String pFormat) throws ConversionExceptionConverts the object to a string, using the given format- Specified by:
toStringin interfacePropertyConverter- Parameters:
pObject- the object to convert.pFormat- the format used for parsing. PropertyConverter implementations may choose to ignore this parameter. Also, implementations that require a parser format, should provide a default format, and allownullas the format argument.- Returns:
- the string representation of the object, on the correct format.
- Throws:
ConversionException- if the object is not a subclass ofNumber
-
getNumberFormat
private java.text.NumberFormat getNumberFormat(java.lang.String pFormat)
-
getFormat
protected final java.text.Format getFormat(java.lang.Class pFormatterClass, java.lang.Object... pFormat)
-
-