Class TypeFormat
- java.lang.Object
-
- org.codehaus.plexus.util.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 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
(Locale
sensitive) 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
-
-
Field Summary
Fields Modifier and Type Field Description private static char[]
DIGITS
Holds the characters used to represent numbers.private static double[]
DOUBLE_POW_10
private static double
DOUBLE_RELATIVE_ERROR
private static float
FLOAT_RELATIVE_ERROR
private static int[]
INT_POW_10
private static java.lang.String[]
LEADING_ZEROS
private static double
LOG_10
private static long[]
LONG_POW_10
-
Constructor Summary
Constructors Modifier Constructor Description private
TypeFormat()
Default constructor (forbids derivation).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringBuffer
format(boolean b, java.lang.StringBuffer sb)
Formats the specifiedboolean
and appends the resulting text to theStringBuffer
argument.private static java.lang.StringBuffer
format(double d, double precision, boolean precisionOnLastDigit, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument; the number of significative digits is deduced from the specified precision.static java.lang.StringBuffer
format(double d, double precision, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument; the number of significative digits is deduced from the specified precision.static java.lang.StringBuffer
format(double d, int digits, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument; the number of significand digits is specified as integer argument.static java.lang.StringBuffer
format(double d, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument.static java.lang.StringBuffer
format(float f, float precision, java.lang.StringBuffer sb)
Formats the specifiedfloat
and appends the resulting text to theStringBuffer
argument; the number of significative digits is deduced from the specified precision.static java.lang.StringBuffer
format(float f, java.lang.StringBuffer sb)
Formats the specifiedfloat
and appends the resulting text to theStringBuffer
argument.static java.lang.StringBuffer
format(int i, int radix, java.lang.StringBuffer sb)
Formats the specifiedint
in the specified radix and appends the resulting text to theStringBuffer
argument.static java.lang.StringBuffer
format(int i, java.lang.StringBuffer sb)
Formats the specifiedint
and appends the resulting text (decimal representation) to theStringBuffer
argument.static java.lang.StringBuffer
format(long l, int radix, java.lang.StringBuffer sb)
Formats the specifiedlong
in the specified radix and appends the resulting text to theStringBuffer
argument.static java.lang.StringBuffer
format(long l, java.lang.StringBuffer sb)
Formats the specifiedlong
and appends the resulting text (decimal representation) to theStringBuffer
argument.static java.lang.StringBuffer
format(short s, int radix, java.lang.StringBuffer sb)
Formats the specifiedshort
in the specified radix and appends the resulting text to theStringBuffer
argument.static java.lang.StringBuffer
format(short s, java.lang.StringBuffer sb)
Formats the specifiedshort
and appends the resulting text (decimal representation) to theStringBuffer
argument.private static void
format2(int i, int radix, java.lang.StringBuffer sb)
private static void
format2(long l, int radix, java.lang.StringBuffer sb)
static int
indexOf(java.lang.CharSequence pattern, java.lang.CharSequence chars, int fromIndex)
Searches for a particular sequence within a character sequence (general purpose parsing function).private static double
multE(double value, int E)
Returns the product of the specified value with10
raised at the specified power exponent.static boolean
parseBoolean(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as aboolean
.static double
parseDouble(java.lang.CharSequence chars)
Parses thisCharSequence
as adouble
.static float
parseFloat(java.lang.CharSequence chars)
Parses thisCharSequence
as afloat
.static int
parseInt(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as a signed decimalint
.static int
parseInt(java.lang.CharSequence chars, int radix)
Parses the specifiedCharSequence
as a signedint
in the specified radix.static long
parseLong(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as a signed decimallong
.static long
parseLong(java.lang.CharSequence chars, int radix)
Parses the specifiedCharSequence
as a signedlong
in the specified radix.static short
parseShort(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as a signed decimalshort
.static short
parseShort(java.lang.CharSequence chars, int radix)
Parses the specifiedCharSequence
as a signedshort
in the specified radix.
-
-
-
Field Detail
-
DIGITS
private static final char[] DIGITS
Holds the characters used to represent numbers.
-
INT_POW_10
private static final int[] INT_POW_10
-
LONG_POW_10
private static final long[] LONG_POW_10
-
LOG_10
private static final double LOG_10
-
FLOAT_RELATIVE_ERROR
private static final float FLOAT_RELATIVE_ERROR
-
DOUBLE_RELATIVE_ERROR
private static final double DOUBLE_RELATIVE_ERROR
-
LEADING_ZEROS
private static java.lang.String[] LEADING_ZEROS
-
DOUBLE_POW_10
private static final double[] DOUBLE_POW_10
-
-
Method Detail
-
indexOf
public static int indexOf(java.lang.CharSequence pattern, java.lang.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 inchars
to start the search from.- Returns:
- the index in the range
[fromIndex, chars.length()-pattern.length()]
or-1
if the character sequence is not found.
-
parseBoolean
public static boolean parseBoolean(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as aboolean
.- Parameters:
chars
- the character sequence to parse.- Returns:
- the corresponding
boolean
.
-
parseShort
public static short parseShort(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as a signed decimalshort
.- Parameters:
chars
- the character sequence to parse.- Returns:
parseShort(chars, 10)
- Throws:
java.lang.NumberFormatException
- if the specified character sequence does not contain a parsableshort
.- See Also:
parseShort(CharSequence, int)
-
parseShort
public static short parseShort(java.lang.CharSequence chars, int radix)
Parses the specifiedCharSequence
as a signedshort
in 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:
java.lang.NumberFormatException
- if the specified character sequence does not contain a parsableshort
.
-
parseInt
public static int parseInt(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as a signed decimalint
.- Parameters:
chars
- the character sequence to parse.- Returns:
parseInt(chars, 10)
- Throws:
java.lang.NumberFormatException
- if the specified character sequence does not contain a parsableint
.- See Also:
parseInt(CharSequence, int)
-
parseInt
public static int parseInt(java.lang.CharSequence chars, int radix)
Parses the specifiedCharSequence
as a signedint
in 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:
java.lang.NumberFormatException
- if the specified character sequence does not contain a parsableint
.
-
parseLong
public static long parseLong(java.lang.CharSequence chars)
Parses the specifiedCharSequence
as a signed decimallong
.- Parameters:
chars
- the character sequence to parse.- Returns:
parseLong(chars, 10)
- Throws:
java.lang.NumberFormatException
- if the specified character sequence does not contain a parsablelong
.- See Also:
parseLong(CharSequence, int)
-
parseLong
public static long parseLong(java.lang.CharSequence chars, int radix)
Parses the specifiedCharSequence
as a signedlong
in 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:
java.lang.NumberFormatException
- if the specified character sequence does not contain a parsablelong
.
-
parseFloat
public static float parseFloat(java.lang.CharSequence chars)
Parses thisCharSequence
as afloat
.- Parameters:
chars
- 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 parsablefloat
.
-
parseDouble
public static double parseDouble(java.lang.CharSequence chars) throws java.lang.NumberFormatException
Parses thisCharSequence
as adouble
.- Parameters:
chars
- 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 parsabledouble
.
-
format
public static java.lang.StringBuffer format(boolean b, java.lang.StringBuffer sb)
Formats the specifiedboolean
and appends the resulting text to theStringBuffer
argument.- Parameters:
b
- aboolean
.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
parseBoolean(java.lang.CharSequence)
-
format
public static java.lang.StringBuffer format(short s, java.lang.StringBuffer sb)
Formats the specifiedshort
and appends the resulting text (decimal representation) to theStringBuffer
argument.Note: This method is preferred to
StringBuffer.append(short)
as it does not create temporaryString
objects (several times faster for small numbers).- Parameters:
s
- theshort
number.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
parseShort(java.lang.CharSequence)
-
format
public static java.lang.StringBuffer format(short s, int radix, java.lang.StringBuffer sb)
Formats the specifiedshort
in the specified radix and appends the resulting text to theStringBuffer
argument.- Parameters:
s
- theshort
number.radix
- the radix.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
throws IllegalArgumentException if radix is not in [2 .. 36] range.
-
format
public static java.lang.StringBuffer format(int i, java.lang.StringBuffer sb)
Formats the specifiedint
and appends the resulting text (decimal representation) to theStringBuffer
argument.Note: This method is preferred to
StringBuffer.append(int)
as it does not create temporaryString
objects (several times faster for small numbers).- Parameters:
i
- theint
number.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
parseInt(java.lang.CharSequence)
-
format
public static java.lang.StringBuffer format(int i, int radix, java.lang.StringBuffer sb)
Formats the specifiedint
in the specified radix and appends the resulting text to theStringBuffer
argument.- Parameters:
i
- theint
number.radix
- the radix.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
throws IllegalArgumentException if radix is not in [2 .. 36] range.
-
format2
private static void format2(int i, int radix, java.lang.StringBuffer sb)
-
format
public static java.lang.StringBuffer format(long l, java.lang.StringBuffer sb)
Formats the specifiedlong
and appends the resulting text (decimal representation) to theStringBuffer
argument.Note: This method is preferred to
StringBuffer.append(long)
as it does not create temporaryString
objects (several times faster for small numbers).- Parameters:
l
- thelong
number.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
parseLong(java.lang.CharSequence)
-
format
public static java.lang.StringBuffer format(long l, int radix, java.lang.StringBuffer sb)
Formats the specifiedlong
in the specified radix and appends the resulting text to theStringBuffer
argument.- Parameters:
l
- thelong
number.radix
- the radix.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - See Also:
throws IllegalArgumentException if radix is not in [2 .. 36] range.
-
format2
private static void format2(long l, int radix, java.lang.StringBuffer sb)
-
format
public static java.lang.StringBuffer format(float f, java.lang.StringBuffer sb)
Formats the specifiedfloat
and appends the resulting text to theStringBuffer
argument.- Parameters:
f
- thefloat
number.sb
- theStringBuffer
to append.- Returns:
format(f, 0.0f, sb)
- See Also:
format(float, float, StringBuffer)
-
format
public static java.lang.StringBuffer format(float f, float precision, java.lang.StringBuffer sb)
Formats the specifiedfloat
and appends the resulting text to theStringBuffer
argument; 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 intrinsicfloat
precision (64 bits IEEE 754 format); no formatting is performed, all significant digits are displayed and trailing zeros are removed.- Parameters:
f
- thefloat
number.precision
- the maximum weight of the last digit represented.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - Throws:
java.lang.IllegalArgumentException
- if the specified precision is negative or would result in too many digits (19+).
-
format
public static java.lang.StringBuffer format(double d, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument.Note : This method is preferred to
StringBuffer.append(double)
or evenString.valueOf(double)
as it does not create temporaryString
orFloatingDecimal
objects (several times faster, e.g. 15x faster forDouble.MAX_VALUE
).- Parameters:
d
- thedouble
number.sb
- theStringBuffer
to append.- Returns:
format(d, 0.0, sb)
- See Also:
format(double, double, StringBuffer)
-
format
public static java.lang.StringBuffer format(double d, int digits, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument; the number of significand digits is specified as integer argument.- Parameters:
d
- thedouble
number.digits
- the number of significand digits (excludes exponent).sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - Throws:
java.lang.IllegalArgumentException
- if the number of digits is not in range[1..19]
.
-
format
public static java.lang.StringBuffer format(double d, double precision, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument; 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 intrinsicdouble
precision (64 bits IEEE 754 format); no formatting is performed, all significant digits are displayed and trailing zeros are removed.- Parameters:
d
- thedouble
number.precision
- the maximum weight of the last digit represented.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object. - Throws:
java.lang.IllegalArgumentException
- if the specified precision is negative or would result in too many digits (19+).
-
format
private static java.lang.StringBuffer format(double d, double precision, boolean precisionOnLastDigit, java.lang.StringBuffer sb)
Formats the specifieddouble
and appends the resulting text to theStringBuffer
argument; the number of significative digits is deduced from the specified precision.- Parameters:
d
- thedouble
number.precision
- the maximum weight of the last digit represented.precisionOnLastDigit
- indicates if the number of digits is deduced from the specified precision.sb
- theStringBuffer
to append.- Returns:
- the specified
StringBuffer
object.
-
multE
private static final double multE(double value, int E)
Returns the product of the specified value with10
raised at the specified power exponent.- Parameters:
value
- the value.E
- the exponent.- Returns:
value * 10^E
-
-