Package elki.utilities.io
Class FormatUtil
- java.lang.Object
-
- elki.utilities.io.FormatUtil
-
public final class FormatUtil extends java.lang.Object
Utility methods for output formatting of various number objectsFIXME: Handle formatting of infinity and NaN better.
- Since:
- 0.2
- Author:
- Arthur Zimek, Erich Schubert
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
NEWLINE
The system newline setting.static java.text.NumberFormat
NF
Dynamic number formatter, but with language constraint.static java.text.NumberFormat
NF0
Number Formatter (0 digits) for output purposes.static java.text.NumberFormat
NF16
Number Formatter (16 digits) for output purposes.static java.text.NumberFormat
NF2
Number Formatter (2 digits) for output purposes.static java.text.NumberFormat
NF3
Number Formatter (3 digits) for output purposes.static java.text.NumberFormat
NF4
Number Formatter (4 digits) for output purposes.static java.text.NumberFormat
NF6
Number Formatter (6 digits) for output purposes.static java.text.NumberFormat
NF8
Number Formatter (8 digits) for output purposes.static char
NONBREAKING_SPACE
Non-breaking unicode space character.private static char[]
SPACEPADDING
Buffer for whitespace padding.private static int[]
TIME_UNIT_DIGITS
The number of digits used for formattingprivate static java.lang.String[]
TIME_UNIT_NAMES
The strings used in serializationprivate static long[]
TIME_UNIT_SIZES
The time unit sizes: ms, s, m, h, d; all in ms.private static java.lang.String
WHITESPACE_BUFFER
Whitespace.private static int
WHITESPACE_BUFFER_LENGTH
Length of the whitespace buffer.private static int
width
Terminal width cache.private static char[]
ZEROPADDING
Buffer for zero padding.
-
Constructor Summary
Constructors Modifier Constructor Description private
FormatUtil()
Private constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringBuilder
appendSpace(java.lang.StringBuilder buf, int spaces)
Append whitespace to a buffer.static java.lang.StringBuilder
appendZeros(java.lang.StringBuilder buf, int zeros)
Append zeros to a buffer.static boolean
endsWith(java.lang.CharSequence buf, java.lang.CharSequence end)
Similar toString.endsWith(String)
but for buffers.static int
findSplitpoint(java.lang.String s, int width)
Find the first space before position w or if there is none after w.static java.lang.String
format(boolean b)
Formats the boolean b.static java.lang.String
format(boolean[] b, java.lang.String sep)
Formats the boolean array b with ',' as separator.static java.lang.String
format(byte[] a)
Formats the byte array a for printing purposes.static java.lang.String
format(double[] d)
Formats the double array d with ', ' as separator and default precision.static java.lang.String
format(double[][] d)
Formats the double array d with ',' as separator and 2 fraction digits.static java.lang.String
format(double[][] m, int w, int d, java.lang.String pre, java.lang.String pos, java.lang.String csep)
Returns a string representation of this matrix.static java.lang.String
format(double[][] m, java.lang.String pre)
Returns a string representation of this matrix.static java.lang.String
format(double[][] d, java.lang.String pre, java.lang.String pos, java.lang.String csep, java.text.NumberFormat nf)
Formats the array of double arrays d with 'the specified separators and fraction digits.static java.lang.String
format(double[][] m, java.text.NumberFormat nf)
returns String-representation of Matrix.static java.lang.String
format(double[] v, int w, int d)
Returns a string representation of this vector.static java.lang.String
format(double[] d, java.lang.String sep)
Formats the double array d with the specified separator.static java.lang.String
format(double[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the double array d with the specified number format.static java.lang.String
format(double[] d, java.text.NumberFormat nf)
Formats the double array d with the specified number format.static java.lang.String
format(float[] f)
Formats the float array f with ',' as separator and default precision.static java.lang.String
format(float[] d, java.lang.String sep)
Formats the float array d with the specified number format.static java.lang.String
format(float[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the float array d with the specified number format.static java.lang.String
format(int[] a)
Formats the int array a for printing purposes.static java.lang.String
format(int[] a, java.lang.String sep)
Formats the int array a for printing purposes.static java.lang.String
format(long[] a)
Formats the long array a for printing purposes.static java.lang.String
format(java.lang.String[] d, java.lang.String sep)
Formats the string array d with the specified separator.static java.lang.String
format(java.util.Collection<java.lang.String> d, java.lang.String sep)
Formats the String collection with the specified separator.static java.lang.StringBuilder
formatBit(java.lang.StringBuilder buf, boolean b)
Format a boolean value as string "1" or "0".static java.lang.String
formatTimeDelta(long time, java.lang.CharSequence sep)
Formats a time delta in human readable format.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, boolean b)
Format a boolean value as string "true" or "false".static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, boolean[] d, java.lang.String sep)
Formats the boolean array d.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, byte[] d, java.lang.String sep)
Formats the byte array d.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, double[][] d, java.lang.String pre, java.lang.String pos, java.lang.String csep, java.text.NumberFormat nf)
Formats the array of double arrays d with the specified separators and fraction digits.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, double[] d, java.lang.String sep)
Formats the double array d with the default number format.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, double[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the double array d with the specified number format.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, float[] d, java.lang.String sep)
Formats the float array d with the default number format.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, float[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the float array d with the specified number format.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, int[] d, java.lang.String sep)
Formats the int array d.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, long[] d, java.lang.String sep)
Formats the long array d.static java.lang.StringBuilder
formatTo(java.lang.StringBuilder buf, short[] d, java.lang.String sep)
Formats the short array d.static int
getConsoleWidth()
Get the width of the terminal window (on Unix xterms), with a default of 78 characters.static java.text.NumberFormat
makeNumberFormat(int digits)
Initialize a number format with ELKI standard options (US locale, no grouping).private static int
nextPosition(int a, int b)
Helper that is similar toMath.min(a,b)
, except that negative values are considered "invalid".static java.lang.String
pad(java.lang.String o, int len)
Pad a string to a given length by adding whitespace to the right.static java.lang.String
padRightAligned(java.lang.String o, int len)
Pad a string to a given length by adding whitespace to the left.static java.util.List<java.lang.String>
splitAtLastBlank(java.lang.String s, int width)
Splits the specified string at the last blank before width.static int
stringSize(int x)
Compute the number of characters needed to represent the integer x.static int
stringSize(long x)
Compute the number of characters needed to represent the integer x.static java.lang.String
whitespace(int n)
Returns a string with the specified number of whitespace.static java.lang.StringBuilder
whitespace(java.lang.StringBuilder buf, int n)
Returns a string with the specified number of whitespace.
-
-
-
Field Detail
-
NF
public static final java.text.NumberFormat NF
Dynamic number formatter, but with language constraint.
-
NF0
public static final java.text.NumberFormat NF0
Number Formatter (0 digits) for output purposes.
-
NF2
public static final java.text.NumberFormat NF2
Number Formatter (2 digits) for output purposes.
-
NF3
public static final java.text.NumberFormat NF3
Number Formatter (3 digits) for output purposes.
-
NF4
public static final java.text.NumberFormat NF4
Number Formatter (4 digits) for output purposes.
-
NF6
public static final java.text.NumberFormat NF6
Number Formatter (6 digits) for output purposes.
-
NF8
public static final java.text.NumberFormat NF8
Number Formatter (8 digits) for output purposes.
-
NF16
public static final java.text.NumberFormat NF16
Number Formatter (16 digits) for output purposes.
-
WHITESPACE_BUFFER
private static final java.lang.String WHITESPACE_BUFFER
Whitespace. The string should cover the commonly used length.- See Also:
- Constant Field Values
-
WHITESPACE_BUFFER_LENGTH
private static final int WHITESPACE_BUFFER_LENGTH
Length of the whitespace buffer.
-
NEWLINE
public static final java.lang.String NEWLINE
The system newline setting.
-
NONBREAKING_SPACE
public static final char NONBREAKING_SPACE
Non-breaking unicode space character.
-
TIME_UNIT_SIZES
private static final long[] TIME_UNIT_SIZES
The time unit sizes: ms, s, m, h, d; all in ms.
-
TIME_UNIT_NAMES
private static final java.lang.String[] TIME_UNIT_NAMES
The strings used in serialization
-
TIME_UNIT_DIGITS
private static final int[] TIME_UNIT_DIGITS
The number of digits used for formatting
-
width
private static int width
Terminal width cache.
-
ZEROPADDING
private static final char[] ZEROPADDING
Buffer for zero padding.
-
SPACEPADDING
private static final char[] SPACEPADDING
Buffer for whitespace padding.
-
-
Method Detail
-
makeNumberFormat
public static java.text.NumberFormat makeNumberFormat(int digits)
Initialize a number format with ELKI standard options (US locale, no grouping).- Parameters:
digits
- Number of digits to use- Returns:
- Number format
-
format
public static java.lang.String format(double[] d)
Formats the double array d with ', ' as separator and default precision.- Parameters:
d
- the double array to be formatted- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(double[] d, java.lang.String sep)
Formats the double array d with the specified separator.- Parameters:
d
- the double array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(double[] d, java.text.NumberFormat nf)
Formats the double array d with the specified number format.- Parameters:
d
- the double array to be formattednf
- the number format to be used for formatting- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(double[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the double array d with the specified number format.- Parameters:
d
- the double array to be formattedsep
- separator between the single values of the array, e.g., ','nf
- the number format to be used for formatting- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(double[] v, int w, int d)
Returns a string representation of this vector.- Parameters:
w
- column widthd
- number of digits after the decimal- Returns:
- a string representation of this matrix
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, double[] d, java.lang.String sep)
Formats the double array d with the default number format.- Parameters:
buf
- String builder to append tod
- the double array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, double[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the double array d with the specified number format.- Parameters:
buf
- String builder to append tod
- the double array to be formattedsep
- separator between the single values of the array, e.g., ','nf
- the number format to be used for formatting- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, float[] d, java.lang.String sep)
Formats the float array d with the default number format.- Parameters:
buf
- String builder to append tod
- the float array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, float[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the float array d with the specified number format.- Parameters:
buf
- String builder to append tod
- the float array to be formattedsep
- separator between the single values of the array, e.g., ','nf
- the number format to be used for formatting- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, int[] d, java.lang.String sep)
Formats the int array d.- Parameters:
buf
- String builder to append tod
- the int array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, long[] d, java.lang.String sep)
Formats the long array d.- Parameters:
buf
- String builder to append tod
- the long array to be formattedsep
- separator between the single values of the long array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, short[] d, java.lang.String sep)
Formats the short array d.- Parameters:
buf
- String builder to append tod
- the int array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, byte[] d, java.lang.String sep)
Formats the byte array d.- Parameters:
buf
- String builder to append tod
- the byte array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, boolean[] d, java.lang.String sep)
Formats the boolean array d.- Parameters:
buf
- String builder to append tod
- the boolean array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- Output buffer buf
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, boolean b)
Format a boolean value as string "true" or "false".- Parameters:
buf
- Buffer to append tob
- Boolean to Format- Returns:
- Same buffer
-
formatBit
public static java.lang.StringBuilder formatBit(java.lang.StringBuilder buf, boolean b)
Format a boolean value as string "1" or "0".- Parameters:
buf
- Buffer to append tob
- Boolean to Format- Returns:
- Same buffer
-
format
public static java.lang.String format(float[] d, java.lang.String sep, java.text.NumberFormat nf)
Formats the float array d with the specified number format.- Parameters:
d
- the float array to be formattedsep
- separator between the single values of the array, e.g., ','nf
- the number format to be used for formatting- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(float[] d, java.lang.String sep)
Formats the float array d with the specified number format.- Parameters:
d
- the float array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(float[] f)
Formats the float array f with ',' as separator and default precision.- Parameters:
f
- the float array to be formatted- Returns:
- a String representing the float array f
-
format
public static java.lang.String format(int[] a, java.lang.String sep)
Formats the int array a for printing purposes.- Parameters:
a
- the int array to be formattedsep
- the separator between the single values of the array, e.g., ','- Returns:
- a String representing the int array a
-
format
public static java.lang.String format(int[] a)
Formats the int array a for printing purposes.- Parameters:
a
- the int array to be formatted- Returns:
- a String representing the int array a
-
format
public static java.lang.String format(long[] a)
Formats the long array a for printing purposes.- Parameters:
a
- the long array to be formatted- Returns:
- a String representing the long array a
-
format
public static java.lang.String format(byte[] a)
Formats the byte array a for printing purposes.- Parameters:
a
- the byte array to be formatted- Returns:
- a String representing the byte array a
-
format
public static java.lang.String format(boolean[] b, java.lang.String sep)
Formats the boolean array b with ',' as separator.- Parameters:
b
- the boolean array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- a String representing the boolean array b
-
format
public static java.lang.String format(boolean b)
Formats the boolean b.- Parameters:
b
- the boolean to be formatted- Returns:
- a String representing of the boolean b
-
formatTo
public static java.lang.StringBuilder formatTo(java.lang.StringBuilder buf, double[][] d, java.lang.String pre, java.lang.String pos, java.lang.String csep, java.text.NumberFormat nf)
Formats the array of double arrays d with the specified separators and fraction digits.- Parameters:
buf
- Output bufferd
- the double array to be formattedpre
- Row prefix (e.g., " [")pos
- Row postfix (e.g., "]\n")csep
- Separator for columns (e.g., ", ")nf
- the number format to use- Returns:
- Output buffer buf
-
format
public static java.lang.String format(double[][] d)
Formats the double array d with ',' as separator and 2 fraction digits.- Parameters:
d
- the double array to be formatted- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(double[][] d, java.lang.String pre, java.lang.String pos, java.lang.String csep, java.text.NumberFormat nf)
Formats the array of double arrays d with 'the specified separators and fraction digits.- Parameters:
d
- the double matrix to be formattedpre
- Row prefix (e.g., " [")pos
- Row postfix (e.g., "]\n")csep
- Separator for columns (e.g., ", ")nf
- the number format to use- Returns:
- a String representing the double array d
-
format
public static java.lang.String format(double[][] m, int w, int d, java.lang.String pre, java.lang.String pos, java.lang.String csep)
Returns a string representation of this matrix.- Parameters:
w
- column widthd
- number of digits after the decimalpre
- Row prefix (e.g., " [")pos
- Row postfix (e.g., "]\n")csep
- Column separator (e.g., ", ")- Returns:
- a string representation of this matrix
-
format
public static java.lang.String format(double[][] m, java.lang.String pre)
Returns a string representation of this matrix. In each line the specified Stringpre
is prefixed.- Parameters:
pre
- the prefix of each line- Returns:
- a string representation of this matrix
-
format
public static java.lang.String format(double[][] m, java.text.NumberFormat nf)
returns String-representation of Matrix.- Parameters:
nf
- NumberFormat to specify output precision- Returns:
- String representation of this Matrix in precision as specified by given NumberFormat
-
format
public static java.lang.String format(java.util.Collection<java.lang.String> d, java.lang.String sep)
Formats the String collection with the specified separator.- Parameters:
d
- the String collection to formatsep
- separator between the single values of the array, e.g., ' '- Returns:
- a String representing the String Collection d
-
format
public static java.lang.String format(java.lang.String[] d, java.lang.String sep)
Formats the string array d with the specified separator.- Parameters:
d
- the string array to be formattedsep
- separator between the single values of the array, e.g., ','- Returns:
- a String representing the string array d
-
findSplitpoint
public static int findSplitpoint(java.lang.String s, int width)
Find the first space before position w or if there is none after w.- Parameters:
s
- Stringwidth
- Width- Returns:
- index of best whitespace or
-1
if no whitespace was found.
-
nextPosition
private static int nextPosition(int a, int b)
Helper that is similar toMath.min(a,b)
, except that negative values are considered "invalid".- Parameters:
a
- String positionb
- String position- Returns:
Math.min(a,b)
ifa >= 0
andb >= 0
, otherwise whichever is not negative.
-
splitAtLastBlank
public static java.util.List<java.lang.String> splitAtLastBlank(java.lang.String s, int width)
Splits the specified string at the last blank before width. If there is no blank before the given width, it is split at the next.- Parameters:
s
- the string to be splitwidth
- int- Returns:
- string fragments
-
whitespace
public static java.lang.String whitespace(int n)
Returns a string with the specified number of whitespace.- Parameters:
n
- the number of whitespace characters- Returns:
- a string with the specified number of blanks
-
whitespace
public static java.lang.StringBuilder whitespace(java.lang.StringBuilder buf, int n)
Returns a string with the specified number of whitespace.- Parameters:
n
- the number of whitespace characters- Returns:
- a string with the specified number of blanks
-
pad
public static java.lang.String pad(java.lang.String o, int len)
Pad a string to a given length by adding whitespace to the right.- Parameters:
o
- original stringlen
- destination length- Returns:
- padded string of at least length len (and o otherwise)
-
padRightAligned
public static java.lang.String padRightAligned(java.lang.String o, int len)
Pad a string to a given length by adding whitespace to the left.- Parameters:
o
- original stringlen
- destination length- Returns:
- padded string of at least length len (and o otherwise)
-
getConsoleWidth
public static int getConsoleWidth()
Get the width of the terminal window (on Unix xterms), with a default of 78 characters.- Returns:
- Terminal width
-
formatTimeDelta
public static java.lang.String formatTimeDelta(long time, java.lang.CharSequence sep)
Formats a time delta in human readable format.- Parameters:
time
- time delta in ms- Returns:
- Formatted string
-
appendZeros
public static java.lang.StringBuilder appendZeros(java.lang.StringBuilder buf, int zeros)
Append zeros to a buffer.- Parameters:
buf
- Buffer to append tozeros
- Number of zeros to append.- Returns:
- Buffer
-
appendSpace
public static java.lang.StringBuilder appendSpace(java.lang.StringBuilder buf, int spaces)
Append whitespace to a buffer.- Parameters:
buf
- Buffer to append tospaces
- Number of spaces to append.- Returns:
- Buffer
-
stringSize
public static int stringSize(int x)
Compute the number of characters needed to represent the integer x. Reimplementation ofLong.stringSize(long)
, but public and without loop.- Parameters:
x
- Integer value- Returns:
- Number of digits needed
-
stringSize
public static int stringSize(long x)
Compute the number of characters needed to represent the integer x. Reimplementation ofLong.stringSize(long)
, but public and without loop.- Parameters:
x
- Integer value- Returns:
- Number of digits needed
-
endsWith
public static boolean endsWith(java.lang.CharSequence buf, java.lang.CharSequence end)
Similar toString.endsWith(String)
but for buffers.- Parameters:
buf
- Bufferend
- End- Returns:
true
if the buffer ends with the given sequence
-
-