Class FormatUtil


  • public final class FormatUtil
    extends java.lang.Object
    Utility methods for output formatting of various number objects

    FIXME: 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 formatting
      private static java.lang.String[] TIME_UNIT_NAMES
      The strings used in serialization
      private 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 to String.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 to Math.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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.
    • Constructor Detail

      • FormatUtil

        private FormatUtil()
        Private constructor. Static methods only.
    • 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 formatted
        sep - 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 formatted
        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[] 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 formatted
        sep - 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 width
        d - 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 to
        d - the double array to be formatted
        sep - 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 to
        d - the double array to be formatted
        sep - 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 to
        d - the float array to be formatted
        sep - 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 to
        d - the float array to be formatted
        sep - 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 to
        d - the int array to be formatted
        sep - 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 to
        d - the long array to be formatted
        sep - 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 to
        d - the int array to be formatted
        sep - 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 to
        d - the byte array to be formatted
        sep - 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 to
        d - the boolean array to be formatted
        sep - 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 to
        b - 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 to
        b - 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 formatted
        sep - 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 formatted
        sep - 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 formatted
        sep - 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 formatted
        sep - 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 buffer
        d - the double array to be formatted
        pre - 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 formatted
        pre - 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 width
        d - number of digits after the decimal
        pre - 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 String pre 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 format
        sep - 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 formatted
        sep - 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 - String
        width - 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 to Math.min(a,b), except that negative values are considered "invalid".
        Parameters:
        a - String position
        b - String position
        Returns:
        Math.min(a,b) if a >= 0 and b >= 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 split
        width - 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 string
        len - 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 string
        len - 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 to
        zeros - 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 to
        spaces - 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 of Long.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 of Long.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 to String.endsWith(String) but for buffers.
        Parameters:
        buf - Buffer
        end - End
        Returns:
        true if the buffer ends with the given sequence