org.objectweb.cjdbc.console.text.formatter
Class TableFormatter

java.lang.Object
  extended byorg.objectweb.cjdbc.console.text.formatter.TableFormatter

public class TableFormatter
extends java.lang.Object

Utility class to format a table which can be pretty displayed in the text console.

Author:
Jeff Mesnil

Constructor Summary
TableFormatter()
           
 
Method Summary
private static void appendCellsTo(java.lang.String[][] cells, java.lang.StringBuffer buf, int[] longestLengths)
          Append a table of cells to the buffer
private static void appendHeaderAndCells(java.lang.String[] headers, java.lang.String[][] cells, java.lang.StringBuffer buf, int[] longestLengths, java.lang.String horizontalRule)
          Append table's headers and cells in one pass.
private static void appendHeadersTo(java.lang.String[] headers, java.lang.StringBuffer buf, int[] longestLengths)
          Append table's headers to the buffer
private static void appendStringTo(java.lang.StringBuffer buf, java.lang.String string, int n)
          Append n times the string to buf.
private static java.lang.String createHorizontalRule(int[] longestLengths)
          Create an horizontal rule following the format: +-----+-------+-----+ where the size of each part is determinged based on the longest length for each column
private static int[] findLongestDataLengths(java.lang.String[] headers, java.lang.String[][] cells, boolean headersAsRow)
          Find the longest lengths for the data.
static java.lang.String format(java.lang.String[] headers, java.lang.String[][] cells, boolean headersAsRow)
          Format as a table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TableFormatter

public TableFormatter()
Method Detail

format

public static java.lang.String format(java.lang.String[] headers,
                                      java.lang.String[][] cells,
                                      boolean headersAsRow)
Format as a table.

Parameters:
headers - the headers of the table
cells - the cells of the table
headersAsRow - true if the headers must be displayed on the same row, false if the headers must be displayed on the same column
Returns:
a String representing a pretty formatted table

createHorizontalRule

private static java.lang.String createHorizontalRule(int[] longestLengths)
Create an horizontal rule following the format: +-----+-------+-----+ where the size of each part is determinged based on the longest length for each column

Parameters:
longestLengths - an array of int corresponding to the longest length of cells for a given column
Returns:
a String to used as an horizontal rule

appendStringTo

private static void appendStringTo(java.lang.StringBuffer buf,
                                   java.lang.String string,
                                   int n)
Append n times the string to buf.

Parameters:
buf - a StringBuffer
string - the String to append
n - the number of times the string should be appended

findLongestDataLengths

private static int[] findLongestDataLengths(java.lang.String[] headers,
                                            java.lang.String[][] cells,
                                            boolean headersAsRow)
Find the longest lengths for the data. Used internally to create an horizontal rule and to append whitespaces in each cell so that everything is propertly formatted.

Parameters:
headers - headers of the table
cells - cells of the table
headersAsRow - true if the headers must be displayed on the same row, false if the headers must be displayed on the same column
Returns:
an array of int containing the longest length for each column

appendCellsTo

private static void appendCellsTo(java.lang.String[][] cells,
                                  java.lang.StringBuffer buf,
                                  int[] longestLengths)
Append a table of cells to the buffer

Parameters:
cells - the table of cells to append
buf - the StringBuffer where to append
longestLengths - used to fill each cell with whitespaces so that everything is properly formatted

appendHeadersTo

private static void appendHeadersTo(java.lang.String[] headers,
                                    java.lang.StringBuffer buf,
                                    int[] longestLengths)
Append table's headers to the buffer

Parameters:
headers - the headers of the table
buf - the StringBuffer where to append
longestLengths - used to fill each cell with whitespaces so that everything is properly formatted

appendHeaderAndCells

private static void appendHeaderAndCells(java.lang.String[] headers,
                                         java.lang.String[][] cells,
                                         java.lang.StringBuffer buf,
                                         int[] longestLengths,
                                         java.lang.String horizontalRule)
Append table's headers and cells in one pass. used when the headers should be displayed on the same colum (i.e. headersAsRow was false).

Parameters:
headers - the headers of the table
buf - the StringBuffer where to append
longestLengths - used to fill each cell with whitespaces so that everything is properly formatted
horizontalRule - an horizontal rule


Copyright © 2002, 2005 - ObjectWeb Consortium - All Rights Reserved.