J avolution v3.7 (J2SE 1.5+)

javolution.lang
Class TypeFormat

java.lang.Object
  extended by javolution.lang.TypeFormat

public final class TypeFormat
extends java.lang.Object

This class provides utility methods to parse CharSequence into primitive types and to format primitive types into an Appendable.

Methods from this class do not create temporary objects and are typically faster than standard library methods (e.g TypeFormat#parseDouble is up to 15x faster than Double.parseDouble).

For non-primitive objects, formatting is typically performed using specialized TextFormat instances.

Version:
3.7, January 1, 2006
Author:
Jean-Marie Dautelle

Method Summary
static java.lang.Appendable format(boolean b, java.lang.Appendable csq)
          Formats the specified boolean and appends the resulting text to the Appendable argument.
static java.lang.Appendable format(double value, java.lang.Appendable csq)
          Formats the specified double value.
static java.lang.Appendable format(double value, int digits, boolean scientific, boolean showZero, java.lang.Appendable csq)
          Formats the specified double value according to the specified formatting arguments.
static java.lang.Appendable format(float value, java.lang.Appendable csq)
          Formats the specified float value.
static java.lang.Appendable format(int i, java.lang.Appendable csq)
          Formats the specified int and appends the resulting text (decimal representation) to the Appendable argument.
static java.lang.Appendable format(int i, int radix, java.lang.Appendable csq)
          Formats the specified int in the specified radix and appends the resulting text to the Appendable argument.
static java.lang.Appendable format(long l, java.lang.Appendable csq)
          Formats the specified long and appends the resulting text (decimal representation) to the Appendable argument.
static java.lang.Appendable format(long l, int radix, java.lang.Appendable csq)
          Formats the specified long in the specified radix and appends the resulting text to the Appendable argument.
static boolean parseBoolean(java.lang.CharSequence csq)
          Parses the specified character sequence as a boolean.
static boolean parseBoolean(java.lang.CharSequence csq, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a boolean.
static byte parseByte(java.lang.CharSequence csq)
          Parses the specified character sequence as a signed decimal byte.
static byte parseByte(java.lang.CharSequence csq, int radix)
          Parses the specified character sequence as a signed byte in the specified radix.
static byte parseByte(java.lang.CharSequence csq, int radix, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a signed byte in the specified radix.
static double parseDouble(java.lang.CharSequence csq)
          Parses the specified character sequence as a double.
static double parseDouble(java.lang.CharSequence csq, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a double.
static float parseFloat(java.lang.CharSequence csq)
          Parses the specified character sequence as a float.
static float parseFloat(java.lang.CharSequence csq, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a float.
static int parseInt(java.lang.CharSequence csq)
          Parses the specified character sequence as a signed int.
static int parseInt(java.lang.CharSequence csq, int radix)
          Parses the specified character sequence as a signed int in the specified radix.
static int parseInt(java.lang.CharSequence csq, int radix, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a signed int in the specified radix.
static long parseLong(java.lang.CharSequence csq)
          Parses the specified character sequence as a decimal long.
static long parseLong(java.lang.CharSequence csq, int radix)
          Parses the specified character sequence as a signed long in the specified radix.
static long parseLong(java.lang.CharSequence csq, int radix, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a signed long in the specified radix.
static short parseShort(java.lang.CharSequence csq)
          Parses the specified character sequence as a signed decimal short.
static short parseShort(java.lang.CharSequence csq, int radix)
          Parses the specified character sequence as a signed short in the specified radix.
static short parseShort(java.lang.CharSequence csq, int radix, TextFormat.Cursor cursor)
          Parses the specified character sequence from the specified position as a signed short in the specified radix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseBoolean

public static boolean parseBoolean(java.lang.CharSequence csq)
Parses the specified character sequence as a boolean.

Parameters:
csq - the character sequence to parse.
Returns:
the corresponding boolean value.
Throws:
java.lang.IllegalArgumentException - if the specified character sequence is different from "true" or "false" ignoring cases.

parseBoolean

public static boolean parseBoolean(java.lang.CharSequence csq,
                                   TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position as a boolean.

Parameters:
csq - the character sequence to parse.
cursor - the current cursor position (being maintained).
Returns:
the next boolean value.
Throws:
java.lang.IllegalArgumentException - if the character sequence from the specified position is different from "true" or "false" ignoring cases.

parseByte

public static byte parseByte(java.lang.CharSequence csq)
Parses the specified character sequence as a signed decimal byte.

Parameters:
csq - the character sequence to parse.
Returns:
parseByte(csq, 10)
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable byte.
See Also:
parseByte(CharSequence, int)

parseByte

public static byte parseByte(java.lang.CharSequence csq,
                             int radix)
Parses the specified character sequence as a signed byte in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
Returns:
the corresponding byte.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable byte.

parseByte

public static byte parseByte(java.lang.CharSequence csq,
                             int radix,
                             TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position as a signed byte in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
cursor - the current cursor position (being maintained).
Returns:
the corresponding byte.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable byte.

parseShort

public static short parseShort(java.lang.CharSequence csq)
Parses the specified character sequence as a signed decimal short.

Parameters:
csq - the character sequence to parse.
Returns:
parseShort(csq, 10)
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable short.
See Also:
parseShort(CharSequence, int)

parseShort

public static short parseShort(java.lang.CharSequence csq,
                               int radix)
Parses the specified character sequence as a signed short in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
Returns:
the corresponding short.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable short.

parseShort

public static short parseShort(java.lang.CharSequence csq,
                               int radix,
                               TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position as a signed short in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
cursor - the current cursor position (being maintained).
Returns:
the corresponding short.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable short.

parseInt

public static int parseInt(java.lang.CharSequence csq)
Parses the specified character sequence as a signed int.

Parameters:
csq - the character sequence to parse.
Returns:
parseInt(csq, 10)
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable int.
See Also:
parseInt(CharSequence, int)

parseInt

public static int parseInt(java.lang.CharSequence csq,
                           int radix)
Parses the specified character sequence as a signed int in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
Returns:
the corresponding int.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable int.

parseInt

public static int parseInt(java.lang.CharSequence csq,
                           int radix,
                           TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position as a signed int in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
cursor - the current cursor position (being maintained).
Returns:
the corresponding int.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable int.

parseLong

public static long parseLong(java.lang.CharSequence csq)
Parses the specified character sequence as a decimal long.

Parameters:
csq - the character sequence to parse.
Returns:
parseLong(csq, 10)
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable long.
See Also:
parseLong(CharSequence, int)

parseLong

public static long parseLong(java.lang.CharSequence csq,
                             int radix)
Parses the specified character sequence as a signed long in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
Returns:
the corresponding long.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable long.

parseLong

public static long parseLong(java.lang.CharSequence csq,
                             int radix,
                             TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position as a signed long in the specified radix.

Parameters:
csq - the character sequence to parse.
radix - the radix to be used while parsing.
cursor - the current cursor position (being maintained).
Returns:
the corresponding long.
Throws:
java.lang.NumberFormatException - if the specified character sequence does not contain a parsable long.

parseFloat

public static float parseFloat(java.lang.CharSequence csq)
Parses the specified character sequence as a float.

Parameters:
csq - the character sequence to parse.
Returns:
the float number represented by the specified character sequence.
Throws:
java.lang.NumberFormatException - if the character sequence does not contain a parsable float. /

parseFloat

public static float parseFloat(java.lang.CharSequence csq,
                               TextFormat.Cursor cursor)
Parses the specified character sequence from the specified position as a float.

Parameters:
csq - the character sequence to parse.
cursor - the current cursor position (being maintained).
Returns:
the float number represented by the specified character sequence.
Throws:
java.lang.NumberFormatException - if the character sequence does not contain a parsable float. /

parseDouble

public static double parseDouble(java.lang.CharSequence csq)
                          throws java.lang.NumberFormatException
Parses the specified character sequence as a double.

Parameters:
csq - the character sequence to parse.
Returns:
the double number represented by this character sequence.
Throws:
java.lang.NumberFormatException - if the character sequence does not contain a parsable double. /

parseDouble

public static double parseDouble(java.lang.CharSequence csq,
                                 TextFormat.Cursor cursor)
                          throws java.lang.NumberFormatException
Parses the specified character sequence from the specified position as a double.

Parameters:
csq - the character sequence to parse.
cursor - the current cursor position (being maintained).
Returns:
the double number represented by this character sequence.
Throws:
java.lang.NumberFormatException - if the character sequence does not contain a parsable double. /

format

public static java.lang.Appendable format(boolean b,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified boolean and appends the resulting text to the Appendable argument.

Parameters:
b - a boolean.
csq - the Appendable to append.
Returns:
the specified StringBuffer object.
Throws:
java.io.IOException - if an I/O exception occurs.
See Also:
parseBoolean(java.lang.CharSequence)

format

public static java.lang.Appendable format(int i,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified int and appends the resulting text (decimal representation) to the Appendable argument.

Note: This method is preferred to Appendable.append(int) as it does not create temporary String objects (several times faster for small numbers).

Parameters:
i - the int number.
csq - the Appendable to append.
Returns:
the specified Appendable object.
Throws:
java.io.IOException - if an I/O exception occurs.
See Also:
parseInt(java.lang.CharSequence)

format

public static java.lang.Appendable format(int i,
                                          int radix,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified int in the specified radix and appends the resulting text to the Appendable argument.

Parameters:
i - the int number.
radix - the radix.
csq - the Appendable to append.
Returns:
the specified Appendable object.
Throws:
java.lang.IllegalArgumentException - if radix is not in [2 .. 36] range.
java.io.IOException - if an I/O exception occurs.
See Also:
parseInt(CharSequence, int)

format

public static java.lang.Appendable format(long l,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified long and appends the resulting text (decimal representation) to the Appendable argument.

Note: This method is preferred to Appendable.append(long) as it does not create temporary String objects (several times faster for small numbers).

Parameters:
l - the long number.
csq - the Appendable to append.
Returns:
the specified Appendable object.
Throws:
java.io.IOException - if an I/O exception occurs.
See Also:
parseLong(java.lang.CharSequence)

format

public static java.lang.Appendable format(long l,
                                          int radix,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified long in the specified radix and appends the resulting text to the Appendable argument.

Parameters:
l - the long number.
radix - the radix.
csq - the Appendable to append.
Returns:
the specified Appendable object.
Throws:
java.lang.IllegalArgumentException - if radix is not in [2 .. 36] range.
java.io.IOException - if an I/O exception occurs.
See Also:
parseLong(CharSequence, int)

format

public static java.lang.Appendable format(float value,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified float value.

Parameters:
value - the float value.
csq - the Appendable to append.
Returns:
format(value, 10, Math.abs(value) > 1E7, false, sb)
Throws:
java.io.IOException - if an I/O exception occurs.
See Also:
/

format

public static java.lang.Appendable format(double value,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified double value.

Note : This method is preferred to Double.toString(double) or even String.valueOf(double) as it does not create temporary String or FloatingDecimal objects (several times faster, e.g. 15x faster for Double.MAX_VALUE).

Parameters:
value - the double value.
csq - the Appendable to append.
Returns:
format(value, 17, Math.abs(value) > 1E7, false, sb)
Throws:
java.io.IOException - if an I/O exception occurs.
See Also:
/

format

public static java.lang.Appendable format(double value,
                                          int digits,
                                          boolean scientific,
                                          boolean showZero,
                                          java.lang.Appendable csq)
                                   throws java.io.IOException
Formats the specified double value according to the specified formatting arguments.

Parameters:
value - the double value.
digits - the number of significative digits (excludes exponent).
scientific - true to forces the use of the scientific notation (e.g. 1.23E3); false otherwise.
showZero - true if trailing fractional zeros are represented; false otherwise.
csq - the Appendable to append.
Returns:
the specified Appendable object.
Throws:
java.lang.IllegalArgumentException - if ((digits > 19) || (digits <= 0)))
java.io.IOException - if an I/O exception occurs. /

J avolution v3.7 (J2SE 1.5+)

Copyright © 2006 Javolution.