com.davisor.data
Class SimpleNumberType

java.lang.Object
  extended byjava.text.Format
      extended bycom.davisor.data.Type
          extended bycom.davisor.data.LocaleType
              extended bycom.davisor.data.FormatType
                  extended bycom.davisor.data.SimpleNumberType
All Implemented Interfaces:
java.lang.Cloneable, java.util.Comparator, com.davisor.core.Dupable, NumberType, java.io.Serializable
Direct Known Subclasses:
BigDecimalType, BigIntegerType, CountType, DoubleType, FloatType, IntegerType, LongType

public abstract class SimpleNumberType
extends FormatType
implements NumberType

SimpleNumberType provides an abstract base class for all data types that manipulate simple numerical value. As with all the other types, the main function of SimpleNumberType is to convert number strings to number objects and back.

Format syntax

SimpleNumberType uses instances of BetterDecimalFormat and other subclasses of java.text.NumberFormat to parse and format number strings. The formatter is selected and controlled by the current number pattern, locale, and sample values. Please see FormatType and LocaleType superclasses for more information about how to set these attributes. Please see BetterDecimalFormat for more information about the exact number pattern syntax.

A short, inaccurate and only informal simple number format syntax summary would be:

 format := [prefix] mantissa [fraction] [exponent] [suffix]

 mantissa  := [ {"0"} {"#"} "," ] {"0"} {"#"}
 fraction := "." {"0"} {"#"}
 exponent := "E"|"e" [ {"0"} {"#"} | siPrefix ]
 

Since:
JDK1.2
See Also:
BetterDecimalFormat, SimpleNumberValue, Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.text.Format
java.text.Format.Field
 
Field Summary
 
Fields inherited from class com.davisor.data.FormatType
M_pattern, M_sample
 
Fields inherited from class com.davisor.data.LocaleType
DEFAULT_LOCALE, M_locale
 
Fields inherited from class com.davisor.data.Type
METADATA_GROUP, METADATA_NAME, METADATA_PARENT, METADATA_SUBCHANNELS, TYPENAME
 
Constructor Summary
protected SimpleNumberType(SimpleNumberType snt)
          Copy constructor.
protected SimpleNumberType(java.lang.String typeName, java.lang.String dataName, java.lang.String id, java.util.Map metaData, java.util.Locale locale, java.lang.String pattern, java.lang.Object sample)
          Creates a new simple number type.
 
Method Summary
 byte byteValue(byte value)
          Converts a byte value to an another byte value.
 byte byteValue(java.lang.String value, java.text.ParsePosition status)
          Converts a string to a byte value.
protected  java.text.Format createFormatter()
          Creates a new format instance suitable for the purposes of this class.
 double doubleValue(double value)
          Converts a double value to an another double value.
 double doubleValue(java.lang.String value, java.text.ParsePosition status)
          Converts a string to a double value.
 float floatValue(float value)
          Converts a float value to an another float value.
 float floatValue(java.lang.String value, java.text.ParsePosition status)
          Converts a string to a float value.
 int intValue(int value)
          Converts a integer value to an another int value.
 int intValue(java.lang.String value, java.text.ParsePosition status)
          Converts a string to an integer value.
 long longValue(long value)
          Converts a long value to an another long value.
 long longValue(java.lang.String value, java.text.ParsePosition status)
          Converts a string to a long value.
 java.lang.Number numberValue(java.lang.String stringValue, java.text.ParsePosition status)
          Converts a string to a number value.
 short shortValue(short value)
          Converts a short value to an another short value.
 short shortValue(java.lang.String value, java.text.ParsePosition status)
          Converts a string to a short value.
 java.lang.String stringValue(byte value)
          Converts a byte value to a string.
 java.lang.String stringValue(double value)
          Converts a double value to a string.
 java.lang.String stringValue(float value)
          Converts a float value to a string.
 java.lang.String stringValue(int value)
          Converts an integer value to a string.
 java.lang.String stringValue(long value)
          Converts a long value to a string.
 java.lang.String stringValue(java.lang.Number value)
          Converts a Number value to a string.
 java.lang.String stringValue(short value)
          Converts a short value to a string.
 DataValue zero()
          Gets the zero data value for this type.
 
Methods inherited from class com.davisor.data.FormatType
clearFormatter, equals, getFormat, getFormatter, getPattern, getSample, getValueClass, hashCode, setFormat, setFormat, setLocale, setPattern, setSample
 
Methods inherited from class com.davisor.data.LocaleType
getLocale, setLocale
 
Methods inherited from class com.davisor.data.Type
ceil, check, check, compare, create, create, create, create, create, dup, dup, floor, format, getCompatibility, getDataName, getId, getID, getMetaData, getMetaData, getSpecs, getSQLCode, getSQLName, getSQLName, getTypeName, isNull, parseObject, register, register, setDataName, setId, setID, setMetaData, setMetaData, setSpecs, toData, toData, toData, toData, toString, toXML, xmlEnd, xmlStart
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.davisor.core.Dupable
dup
 

Constructor Detail

SimpleNumberType

protected SimpleNumberType(SimpleNumberType snt)
Copy constructor.


SimpleNumberType

protected SimpleNumberType(java.lang.String typeName,
                           java.lang.String dataName,
                           java.lang.String id,
                           java.util.Map metaData,
                           java.util.Locale locale,
                           java.lang.String pattern,
                           java.lang.Object sample)
Creates a new simple number type.

Parameters:
typeName - type name
dataName - data name
id - type identity
metaData - type meta data map (may be null)
locale - type locale (may be null)
pattern - format pattern (may be null)
sample - format sample (may be null)
See Also:
FormatType.setLocale(java.util.Locale), FormatType.setPattern(java.lang.String), FormatType.setSample(java.lang.Object)
Method Detail

createFormatter

protected java.text.Format createFormatter()
Creates a new format instance suitable for the purposes of this class.

If current pattern is not empty, an instance of BetterDecimalFormat is configured with the current locale and pattern. If current locale is null, US locale is used instead (please note, that in this case US locale is used as the default locale, regardless of system own default locale). If current sample value is an instance of java.lang.Number, the magnitude of that sample is resolved, and the decimal format exponent hint is set with BetterDecimalFormat.setExponentHint(int).

If current pattern is null or empty, but current locale is defined, a locale spesific number format is created with java.text.NumberFormat.getInstance(Locale) and returned.

In any other case, no format can be created, and null is returned.

Specified by:
createFormatter in class FormatType
See Also:
LocaleType.getLocale(), FormatType.getPattern(), FormatType.getSample(), BetterDecimalFormat

byteValue

public byte byteValue(java.lang.String value,
                      java.text.ParsePosition status)
               throws InvalidDataException
Converts a string to a byte value.

Specified by:
byteValue in interface NumberType
Parameters:
value - the string to be converted
status - parse status (may be null)
Returns:
byte value
Throws:
InvalidDataException - if the value cannot be converted

doubleValue

public double doubleValue(java.lang.String value,
                          java.text.ParsePosition status)
                   throws InvalidDataException
Converts a string to a double value.

Specified by:
doubleValue in interface NumberType
Parameters:
value - the string to be converted
status - parse status (may be null)
Returns:
double value
Throws:
InvalidDataException - if the value cannot be converted

floatValue

public float floatValue(java.lang.String value,
                        java.text.ParsePosition status)
                 throws InvalidDataException
Converts a string to a float value.

Specified by:
floatValue in interface NumberType
Parameters:
value - the string to be converted
status - parse status (may be null)
Returns:
float value
Throws:
InvalidDataException - if the value cannot be converted

intValue

public int intValue(java.lang.String value,
                    java.text.ParsePosition status)
             throws InvalidDataException
Converts a string to an integer value.

Specified by:
intValue in interface NumberType
Parameters:
value - the string to be converted
status - parse status (may be null)
Returns:
integer value
Throws:
InvalidDataException - if the value cannot be converted

longValue

public long longValue(java.lang.String value,
                      java.text.ParsePosition status)
               throws InvalidDataException
Converts a string to a long value.

Specified by:
longValue in interface NumberType
Parameters:
value - the string to be converted
status - parse status (may be null)
Returns:
long value
Throws:
InvalidDataException - if the value cannot be converted

numberValue

public java.lang.Number numberValue(java.lang.String stringValue,
                                    java.text.ParsePosition status)
                             throws InvalidDataException
Converts a string to a number value. The current format, if not set, defaults to LocaleType default locale format. null or empty string value will evaluate to null boolean value.

If the given string value contains trailing characters that can not be interpreted as a number value, a warning message will be printed to system error channel. This warning is neccessary since number string miss-interpretation is one of the most common and difficult practical data interpretation problems. In particular, different number formatting rules in different locales cause a lot of confusion, and the warning messages will significantly help to track these problems down.

Specified by:
numberValue in interface NumberType
Parameters:
stringValue - the string to be converted (may be null)
status - parse status (may be null)
Returns:
number value corresponding to given value (may be null)
Throws:
InvalidDataExeption - if value cannot be decoded
InvalidDataException - if the value cannot be converted to a long value.
See Also:
FormatType.getFormatter()

shortValue

public short shortValue(java.lang.String value,
                        java.text.ParsePosition status)
                 throws InvalidDataException
Converts a string to a short value.

Specified by:
shortValue in interface NumberType
Parameters:
value - the string to be converted
status - parse status (may be null)
Returns:
short value
Throws:
InvalidDataException - if the value cannot be converted

stringValue

public java.lang.String stringValue(byte value)
                             throws InvalidDataException
Converts a byte value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

stringValue

public java.lang.String stringValue(double value)
                             throws InvalidDataException
Converts a double value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

stringValue

public java.lang.String stringValue(float value)
                             throws InvalidDataException
Converts a float value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

stringValue

public java.lang.String stringValue(int value)
                             throws InvalidDataException
Converts an integer value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

stringValue

public java.lang.String stringValue(long value)
                             throws InvalidDataException
Converts a long value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

stringValue

public java.lang.String stringValue(short value)
                             throws InvalidDataException
Converts a short value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

stringValue

public java.lang.String stringValue(java.lang.Number value)
                             throws InvalidDataException
Converts a Number value to a string.

Specified by:
stringValue in interface NumberType
Parameters:
value - the value to be converted
Throws:
InvalidDataException - if the value cannot be converted
See Also:
FormatType.getFormatter()

byteValue

public byte byteValue(byte value)
Converts a byte value to an another byte value.

Specified by:
byteValue in interface NumberType
Parameters:
value - the value to be converted

doubleValue

public double doubleValue(double value)
Converts a double value to an another double value.

Specified by:
doubleValue in interface NumberType
Parameters:
value - the value to be converted

floatValue

public float floatValue(float value)
Converts a float value to an another float value.

Specified by:
floatValue in interface NumberType
Parameters:
value - the value to be converted

intValue

public int intValue(int value)
Converts a integer value to an another int value.

Specified by:
intValue in interface NumberType
Parameters:
value - the value to be converted

longValue

public long longValue(long value)
Converts a long value to an another long value.

Specified by:
longValue in interface NumberType
Parameters:
value - the value to be converted

shortValue

public short shortValue(short value)
Converts a short value to an another short value.

Specified by:
shortValue in interface NumberType
Parameters:
value - the value to be converted

zero

public DataValue zero()
Gets the zero data value for this type.

Specified by:
zero in class Type
Returns:
a new zero value DoubleData object sharing this type
See Also:
DataValue.deinterpolate(com.davisor.data.DataValue, com.davisor.data.DataValue), DataValue.interpolate(double, com.davisor.data.DataValue)


Copyright © 2001-2004 Davisor Oy. All Rights Reserved.