Class TypeFormat
- java.lang.Object
-
- org.codehaus.plexus.util.TypeFormat
-
public final class TypeFormat extends Object
This class provides utility methods to parse
CharSequenceinto primitive types and to format primitive types intoStringBuffer.Methods from this utility class do not create temporary objects and are typically faster than standard library methods (e.g
For class instances, formatting is typically performed using specializedparseDouble(java.lang.CharSequence)is up to 15x faster thanDouble.parseDouble).java.text.Format(Localesensitive) and/or using conventional methods (class sensitive). For example:public class Foo { public static Foo valueOf(CharSequence chars) {...} // Parses. public StringBuffer appendTo(StringBuffer sb) {...} // Formats. public String toString() { return appendTo(new StringBuffer()).toString(); } }This class is public domain (not copyrighted).
- Version:
- 4.6, June 22, 2003
- Author:
- Jean-Marie Dautelle
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringBufferformat(boolean b, StringBuffer sb)Formats the specifiedbooleanand appends the resulting text to theStringBufferargument.static StringBufferformat(double d, double precision, StringBuffer sb)Formats the specifieddoubleand appends the resulting text to theStringBufferargument; the number of significative digits is deduced from the specified precision.static StringBufferformat(double d, int digits, StringBuffer sb)Formats the specifieddoubleand appends the resulting text to theStringBufferargument; the number of significand digits is specified as integer argument.static StringBufferformat(double d, StringBuffer sb)Formats the specifieddoubleand appends the resulting text to theStringBufferargument.static StringBufferformat(float f, float precision, StringBuffer sb)Formats the specifiedfloatand appends the resulting text to theStringBufferargument; the number of significative digits is deduced from the specified precision.static StringBufferformat(float f, StringBuffer sb)Formats the specifiedfloatand appends the resulting text to theStringBufferargument.static StringBufferformat(int i, int radix, StringBuffer sb)Formats the specifiedintin the specified radix and appends the resulting text to theStringBufferargument.static StringBufferformat(int i, StringBuffer sb)Formats the specifiedintand appends the resulting text (decimal representation) to theStringBufferargument.static StringBufferformat(long l, int radix, StringBuffer sb)Formats the specifiedlongin the specified radix and appends the resulting text to theStringBufferargument.static StringBufferformat(long l, StringBuffer sb)Formats the specifiedlongand appends the resulting text (decimal representation) to theStringBufferargument.static StringBufferformat(short s, int radix, StringBuffer sb)Formats the specifiedshortin the specified radix and appends the resulting text to theStringBufferargument.static StringBufferformat(short s, StringBuffer sb)Formats the specifiedshortand appends the resulting text (decimal representation) to theStringBufferargument.static intindexOf(CharSequence pattern, CharSequence chars, int fromIndex)Searches for a particular sequence within a character sequence (general purpose parsing function).static booleanparseBoolean(CharSequence chars)Parses the specifiedCharSequenceas aboolean.static doubleparseDouble(CharSequence chars)Parses thisCharSequenceas adouble.static floatparseFloat(CharSequence chars)Parses thisCharSequenceas afloat.static intparseInt(CharSequence chars)Parses the specifiedCharSequenceas a signed decimalint.static intparseInt(CharSequence chars, int radix)Parses the specifiedCharSequenceas a signedintin the specified radix.static longparseLong(CharSequence chars)Parses the specifiedCharSequenceas a signed decimallong.static longparseLong(CharSequence chars, int radix)Parses the specifiedCharSequenceas a signedlongin the specified radix.static shortparseShort(CharSequence chars)Parses the specifiedCharSequenceas a signed decimalshort.static shortparseShort(CharSequence chars, int radix)Parses the specifiedCharSequenceas a signedshortin the specified radix.
-
-
-
Method Detail
-
indexOf
public static int indexOf(CharSequence pattern, CharSequence chars, int fromIndex)
Searches for a particular sequence within a character sequence (general purpose parsing function).- Parameters:
pattern- the character sequence to search for.chars- the character sequence being searched.fromIndex- the index incharsto start the search from.- Returns:
- the index in the range
[fromIndex, chars.length()-pattern.length()]or-1if the character sequence is not found.
-
parseBoolean
public static boolean parseBoolean(CharSequence chars)
Parses the specifiedCharSequenceas aboolean.- Parameters:
chars- the character sequence to parse.- Returns:
- the corresponding
boolean.
-
parseShort
public static short parseShort(CharSequence chars)
Parses the specifiedCharSequenceas a signed decimalshort.- Parameters:
chars- the character sequence to parse.- Returns:
parseShort(chars, 10)- Throws:
NumberFormatException- if the specified character sequence does not contain a parsableshort.- See Also:
parseShort(CharSequence, int)
-
parseShort
public static short parseShort(CharSequence chars, int radix)
Parses the specifiedCharSequenceas a signedshortin the specified radix. The characters in the string must all be digits of the specified radix, except the first character which may be a plus sign'+'or a minus sign'-'.- Parameters:
chars- the character sequence to parse.radix- the radix to be used while parsing.- Returns:
- the corresponding
short. - Throws:
NumberFormatException- if the specified character sequence does not contain a parsableshort.
-
parseInt
public static int parseInt(CharSequence chars)
Parses the specifiedCharSequenceas a signed decimalint.- Parameters:
chars- the character sequence to parse.- Returns:
parseInt(chars, 10)- Throws:
NumberFormatException- if the specified character sequence does not contain a parsableint.- See Also:
parseInt(CharSequence, int)
-
parseInt
public static int parseInt(CharSequence chars, int radix)
Parses the specifiedCharSequenceas a signedintin the specified radix. The characters in the string must all be digits of the specified radix, except the first character which may be a plus sign'+'or a minus sign'-'.- Parameters:
chars- the character sequence to parse.radix- the radix to be used while parsing.- Returns:
- the corresponding
int. - Throws:
NumberFormatException- if the specified character sequence does not contain a parsableint.
-
parseLong
public static long parseLong(CharSequence chars)
Parses the specifiedCharSequenceas a signed decimallong.- Parameters:
chars- the character sequence to parse.- Returns:
parseLong(chars, 10)- Throws:
NumberFormatException- if the specified character sequence does not contain a parsablelong.- See Also:
parseLong(CharSequence, int)
-
parseLong
public static long parseLong(CharSequence chars, int radix)
Parses the specifiedCharSequenceas a signedlongin the specified radix. The characters in the string must all be digits of the specified radix, except the first character which may be a plus sign'+'or a minus sign'-'.- Parameters:
chars- the character sequence to parse.radix- the radix to be used while parsing.- Returns:
- the corresponding
long. - Throws:
NumberFormatException- if the specified character sequence does not contain a parsablelong.
-
parseFloat
public static float parseFloat(CharSequence chars)
Parses thisCharSequenceas afloat.- Parameters:
chars- the character sequence to parse.- Returns:
- the float number represented by the specified character sequence.
- Throws:
NumberFormatException- if the character sequence does not contain a parsablefloat.
-
parseDouble
public static double parseDouble(CharSequence chars) throws NumberFormatException
Parses thisCharSequenceas adouble.- Parameters:
chars- the character sequence to parse.- Returns:
- the double number represented by this character sequence.
- Throws:
NumberFormatException- if the character sequence does not contain a parsabledouble.
-
format
public static StringBuffer format(boolean b, StringBuffer sb)
Formats the specifiedbooleanand appends the resulting text to theStringBufferargument.- Parameters:
b- aboolean.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
parseBoolean(java.lang.CharSequence)
-
format
public static StringBuffer format(short s, StringBuffer sb)
Formats the specifiedshortand appends the resulting text (decimal representation) to theStringBufferargument.Note: This method is preferred to
StringBuffer.append(short)as it does not create temporaryStringobjects (several times faster for small numbers).- Parameters:
s- theshortnumber.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
parseShort(java.lang.CharSequence)
-
format
public static StringBuffer format(short s, int radix, StringBuffer sb)
Formats the specifiedshortin the specified radix and appends the resulting text to theStringBufferargument.- Parameters:
s- theshortnumber.radix- the radix.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
throws IllegalArgumentException if radix is not in [2 .. 36] range.
-
format
public static StringBuffer format(int i, StringBuffer sb)
Formats the specifiedintand appends the resulting text (decimal representation) to theStringBufferargument.Note: This method is preferred to
StringBuffer.append(int)as it does not create temporaryStringobjects (several times faster for small numbers).- Parameters:
i- theintnumber.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
parseInt(java.lang.CharSequence)
-
format
public static StringBuffer format(int i, int radix, StringBuffer sb)
Formats the specifiedintin the specified radix and appends the resulting text to theStringBufferargument.- Parameters:
i- theintnumber.radix- the radix.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
throws IllegalArgumentException if radix is not in [2 .. 36] range.
-
format
public static StringBuffer format(long l, StringBuffer sb)
Formats the specifiedlongand appends the resulting text (decimal representation) to theStringBufferargument.Note: This method is preferred to
StringBuffer.append(long)as it does not create temporaryStringobjects (several times faster for small numbers).- Parameters:
l- thelongnumber.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
parseLong(java.lang.CharSequence)
-
format
public static StringBuffer format(long l, int radix, StringBuffer sb)
Formats the specifiedlongin the specified radix and appends the resulting text to theStringBufferargument.- Parameters:
l- thelongnumber.radix- the radix.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - See Also:
throws IllegalArgumentException if radix is not in [2 .. 36] range.
-
format
public static StringBuffer format(float f, StringBuffer sb)
Formats the specifiedfloatand appends the resulting text to theStringBufferargument.- Parameters:
f- thefloatnumber.sb- theStringBufferto append.- Returns:
format(f, 0.0f, sb)- See Also:
format(float, float, StringBuffer)
-
format
public static StringBuffer format(float f, float precision, StringBuffer sb)
Formats the specifiedfloatand appends the resulting text to theStringBufferargument; the number of significative digits is deduced from the specified precision. All digits at least as significant as the specified precision are represented. For example:format(5.6f, 0.01f, sb)appends"5.60"format(5.6f, 0.1f, sb)appends"5.6"format(5.6f, 1f, sb)appends"6"
0.0f, the precision is assumed to be the intrinsicfloatprecision (64 bits IEEE 754 format); no formatting is performed, all significant digits are displayed and trailing zeros are removed.- Parameters:
f- thefloatnumber.precision- the maximum weight of the last digit represented.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - Throws:
IllegalArgumentException- if the specified precision is negative or would result in too many digits (19+).
-
format
public static StringBuffer format(double d, StringBuffer sb)
Formats the specifieddoubleand appends the resulting text to theStringBufferargument.Note : This method is preferred to
StringBuffer.append(double)or evenString.valueOf(double)as it does not create temporaryStringorFloatingDecimalobjects (several times faster, e.g. 15x faster forDouble.MAX_VALUE).- Parameters:
d- thedoublenumber.sb- theStringBufferto append.- Returns:
format(d, 0.0, sb)- See Also:
format(double, double, StringBuffer)
-
format
public static StringBuffer format(double d, int digits, StringBuffer sb)
Formats the specifieddoubleand appends the resulting text to theStringBufferargument; the number of significand digits is specified as integer argument.- Parameters:
d- thedoublenumber.digits- the number of significand digits (excludes exponent).sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - Throws:
IllegalArgumentException- if the number of digits is not in range[1..19].
-
format
public static StringBuffer format(double d, double precision, StringBuffer sb)
Formats the specifieddoubleand appends the resulting text to theStringBufferargument; the number of significative digits is deduced from the specified precision. All digits at least as significant as the specified precision are represented. For example:format(5.6, 0.01, sb)appends"5.60"format(5.6, 0.1, sb)appends"5.6"format(5.6, 1, sb)appends"6"
0.0, the precision is assumed to be the intrinsicdoubleprecision (64 bits IEEE 754 format); no formatting is performed, all significant digits are displayed and trailing zeros are removed.- Parameters:
d- thedoublenumber.precision- the maximum weight of the last digit represented.sb- theStringBufferto append.- Returns:
- the specified
StringBufferobject. - Throws:
IllegalArgumentException- if the specified precision is negative or would result in too many digits (19+).
-
-