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 StringBuffer.
Methods from this utility class do not create temporary objects and are typically faster than standard library
methods (e.g parseDouble(java.lang.CharSequence) is up to 15x faster than Double.parseDouble).
For class instances, formatting is typically performed using specialized 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).
| Modifier and Type | Field and 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 |
| Modifier | Constructor and Description |
|---|---|
private |
TypeFormat()
Default constructor (forbids derivation).
|
| Modifier and Type | Method and Description |
|---|---|
static java.lang.StringBuffer |
format(boolean b,
java.lang.StringBuffer sb)
Formats the specified
boolean and appends the resulting text to the StringBuffer
argument. |
private static java.lang.StringBuffer |
format(double d,
double precision,
boolean precisionOnLastDigit,
java.lang.StringBuffer sb)
Formats the specified
double and appends the resulting text to the StringBuffer
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 specified
double and appends the resulting text to the StringBuffer
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 specified
double and appends the resulting text to the StringBuffer
argument; the number of significand digits is specified as integer argument. |
static java.lang.StringBuffer |
format(double d,
java.lang.StringBuffer sb)
Formats the specified
double and appends the resulting text to the StringBuffer
argument. |
static java.lang.StringBuffer |
format(float f,
float precision,
java.lang.StringBuffer sb)
Formats the specified
float and appends the resulting text to the StringBuffer
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 specified
float and appends the resulting text to the StringBuffer
argument. |
static java.lang.StringBuffer |
format(int i,
int radix,
java.lang.StringBuffer sb)
Formats the specified
int in the specified radix and appends the resulting text to the
StringBuffer argument. |
static java.lang.StringBuffer |
format(int i,
java.lang.StringBuffer sb)
Formats the specified
int and appends the resulting text (decimal representation) to the
StringBuffer argument. |
static java.lang.StringBuffer |
format(long l,
int radix,
java.lang.StringBuffer sb)
Formats the specified
long in the specified radix and appends the resulting text to the
StringBuffer argument. |
static java.lang.StringBuffer |
format(long l,
java.lang.StringBuffer sb)
Formats the specified
long and appends the resulting text (decimal representation) to the
StringBuffer argument. |
static java.lang.StringBuffer |
format(short s,
int radix,
java.lang.StringBuffer sb)
Formats the specified
short in the specified radix and appends the resulting text to the
StringBuffer argument. |
static java.lang.StringBuffer |
format(short s,
java.lang.StringBuffer sb)
Formats the specified
short and appends the resulting text (decimal representation) to the
StringBuffer 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 with
10 raised at the specified power exponent. |
static boolean |
parseBoolean(java.lang.CharSequence chars)
Parses the specified
CharSequence as a boolean. |
static double |
parseDouble(java.lang.CharSequence chars)
Parses this
CharSequence as a double. |
static float |
parseFloat(java.lang.CharSequence chars)
Parses this
CharSequence as a float. |
static int |
parseInt(java.lang.CharSequence chars)
Parses the specified
CharSequence as a signed decimal int. |
static int |
parseInt(java.lang.CharSequence chars,
int radix)
Parses the specified
CharSequence as a signed int in the specified radix. |
static long |
parseLong(java.lang.CharSequence chars)
Parses the specified
CharSequence as a signed decimal long. |
static long |
parseLong(java.lang.CharSequence chars,
int radix)
Parses the specified
CharSequence as a signed long in the specified radix. |
static short |
parseShort(java.lang.CharSequence chars)
Parses the specified
CharSequence as a signed decimal short. |
static short |
parseShort(java.lang.CharSequence chars,
int radix)
Parses the specified
CharSequence as a signed short in the specified radix. |
private static final char[] DIGITS
private static final int[] INT_POW_10
private static final long[] LONG_POW_10
private static final double LOG_10
private static final float FLOAT_RELATIVE_ERROR
private static final double DOUBLE_RELATIVE_ERROR
private static java.lang.String[] LEADING_ZEROS
private static final double[] DOUBLE_POW_10
public static int indexOf(java.lang.CharSequence pattern,
java.lang.CharSequence chars,
int fromIndex)
pattern - the character sequence to search for.chars - the character sequence being searched.fromIndex - the index in chars to start the search from.[fromIndex, chars.length()-pattern.length()] or -1 if
the character sequence is not found.public static boolean parseBoolean(java.lang.CharSequence chars)
CharSequence as a boolean.chars - the character sequence to parse.boolean.public static short parseShort(java.lang.CharSequence chars)
CharSequence as a signed decimal short.chars - the character sequence to parse.parseShort(chars, 10)java.lang.NumberFormatException - if the specified character sequence does not contain a parsable short.parseShort(CharSequence, int)public static short parseShort(java.lang.CharSequence chars,
int radix)
CharSequence as a signed short 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 '-'.chars - the character sequence to parse.radix - the radix to be used while parsing.short.java.lang.NumberFormatException - if the specified character sequence does not contain a parsable short.public static int parseInt(java.lang.CharSequence chars)
CharSequence as a signed decimal int.chars - the character sequence to parse.parseInt(chars, 10)java.lang.NumberFormatException - if the specified character sequence does not contain a parsable int.parseInt(CharSequence, int)public static int parseInt(java.lang.CharSequence chars,
int radix)
CharSequence as a signed int 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 '-'.chars - the character sequence to parse.radix - the radix to be used while parsing.int.java.lang.NumberFormatException - if the specified character sequence does not contain a parsable int.public static long parseLong(java.lang.CharSequence chars)
CharSequence as a signed decimal long.chars - the character sequence to parse.parseLong(chars, 10)java.lang.NumberFormatException - if the specified character sequence does not contain a parsable long.parseLong(CharSequence, int)public static long parseLong(java.lang.CharSequence chars,
int radix)
CharSequence as a signed long 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 '-'.chars - the character sequence to parse.radix - the radix to be used while parsing.long.java.lang.NumberFormatException - if the specified character sequence does not contain a parsable long.public static float parseFloat(java.lang.CharSequence chars)
CharSequence as a float.chars - the character sequence to parse.java.lang.NumberFormatException - if the character sequence does not contain a parsable float.public static double parseDouble(java.lang.CharSequence chars)
throws java.lang.NumberFormatException
CharSequence as a double.chars - the character sequence to parse.java.lang.NumberFormatException - if the character sequence does not contain a parsable double.public static java.lang.StringBuffer format(boolean b,
java.lang.StringBuffer sb)
boolean and appends the resulting text to the StringBuffer
argument.b - a boolean.sb - the StringBuffer to append.StringBuffer object.parseBoolean(java.lang.CharSequence)public static java.lang.StringBuffer format(short s,
java.lang.StringBuffer sb)
short and appends the resulting text (decimal representation) to the
StringBuffer argument.
Note: This method is preferred to StringBuffer.append(short)
as it does not create temporary String objects (several times faster for small
numbers).
s - the short number.sb - the StringBuffer to append.StringBuffer object.parseShort(java.lang.CharSequence)public static java.lang.StringBuffer format(short s,
int radix,
java.lang.StringBuffer sb)
short in the specified radix and appends the resulting text to the
StringBuffer argument.s - the short number.radix - the radix.sb - the StringBuffer to append.StringBuffer object.throws IllegalArgumentException if radix is not in [2 .. 36] range.public static java.lang.StringBuffer format(int i,
java.lang.StringBuffer sb)
int and appends the resulting text (decimal representation) to the
StringBuffer argument.
Note: This method is preferred to StringBuffer.append(int)
as it does not create temporary String objects (several times faster for small
numbers).
i - the int number.sb - the StringBuffer to append.StringBuffer object.parseInt(java.lang.CharSequence)public static java.lang.StringBuffer format(int i,
int radix,
java.lang.StringBuffer sb)
int in the specified radix and appends the resulting text to the
StringBuffer argument.i - the int number.radix - the radix.sb - the StringBuffer to append.StringBuffer object.throws IllegalArgumentException if radix is not in [2 .. 36] range.private static void format2(int i,
int radix,
java.lang.StringBuffer sb)
public static java.lang.StringBuffer format(long l,
java.lang.StringBuffer sb)
long and appends the resulting text (decimal representation) to the
StringBuffer argument.
Note: This method is preferred to StringBuffer.append(long)
as it does not create temporary String objects (several times faster for small
numbers).
l - the long number.sb - the StringBuffer to append.StringBuffer object.parseLong(java.lang.CharSequence)public static java.lang.StringBuffer format(long l,
int radix,
java.lang.StringBuffer sb)
long in the specified radix and appends the resulting text to the
StringBuffer argument.l - the long number.radix - the radix.sb - the StringBuffer to append.StringBuffer object.throws IllegalArgumentException if radix is not in [2 .. 36] range.private static void format2(long l,
int radix,
java.lang.StringBuffer sb)
public static java.lang.StringBuffer format(float f,
java.lang.StringBuffer sb)
float and appends the resulting text to the StringBuffer
argument.f - the float number.sb - the StringBuffer to append.format(f, 0.0f, sb)format(float, float, StringBuffer)public static java.lang.StringBuffer format(float f,
float precision,
java.lang.StringBuffer sb)
float and appends the resulting text to the StringBuffer
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 intrinsic float precision
(64 bits IEEE 754 format); no formatting is performed, all significant digits are displayed and trailing zeros
are removed.f - the float number.precision - the maximum weight of the last digit represented.sb - the StringBuffer to append.StringBuffer object.java.lang.IllegalArgumentException - if the specified precision is negative or would result in too many digits (19+).public static java.lang.StringBuffer format(double d,
java.lang.StringBuffer sb)
double and appends the resulting text to the StringBuffer
argument.
Note : This method is preferred to StringBuffer.append(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).
d - the double number.sb - the StringBuffer to append.format(d, 0.0, sb)format(double, double, StringBuffer)public static java.lang.StringBuffer format(double d,
int digits,
java.lang.StringBuffer sb)
double and appends the resulting text to the StringBuffer
argument; the number of significand digits is specified as integer argument.d - the double number.digits - the number of significand digits (excludes exponent).sb - the StringBuffer to append.StringBuffer object.java.lang.IllegalArgumentException - if the number of digits is not in range [1..19].public static java.lang.StringBuffer format(double d,
double precision,
java.lang.StringBuffer sb)
double and appends the resulting text to the StringBuffer
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 intrinsic double precision
(64 bits IEEE 754 format); no formatting is performed, all significant digits are displayed and trailing zeros
are removed.d - the double number.precision - the maximum weight of the last digit represented.sb - the StringBuffer to append.StringBuffer object.java.lang.IllegalArgumentException - if the specified precision is negative or would result in too many digits (19+).private static java.lang.StringBuffer format(double d,
double precision,
boolean precisionOnLastDigit,
java.lang.StringBuffer sb)
double and appends the resulting text to the StringBuffer
argument; the number of significative digits is deduced from the specified precision.d - the double number.precision - the maximum weight of the last digit represented.precisionOnLastDigit - indicates if the number of digits is deduced from the specified precision.sb - the StringBuffer to append.StringBuffer object.private static final double multE(double value,
int E)
10 raised at the specified power exponent.value - the value.E - the exponent.value * 10^E