org.afcs.warts.util
Class TablePrinter

java.lang.Object
  extended byorg.afcs.warts.util.TablePrinter

public final class TablePrinter
extends java.lang.Object

The TablePrinter class prints a 2-D array of strings in a plain-text table with each column aligned to the left or right. The column names, widths, and alignments are specified at initialization, and then one of the prettyPrint() methods is called to format a specific dataset, returning the result as a string.

LICENSE: This code is released to the public domain and may be used for any purpose whatsoever without permission or acknowledgment.

Version:
Last Modified 18 September 2003
Author:
Warren Hedley ( whedley at sdsc dot edu )

Constructor Summary
TablePrinter(java.lang.String[] columnNames, int[] columnWidths)
          Initialises a new instance with the specified column names and widths.
TablePrinter(java.lang.String[] columnNames, int[] columnWidths, boolean[] columnLeftAligned)
          Initialises a new instance with the specified column names, widths, and alignments.
 
Method Summary
 java.lang.String prettyPrint(java.util.List data)
          Returns a string containing the specified dataset, which must be a list of java.util.List instances, in a plain text table, formatted according to the parameters specified at initialization.
 java.lang.String prettyPrint(java.lang.String[][] data)
          Returns a string containing the specified dataset, formatted according to the parameters specified at initialization.
 java.lang.String toString()
          Returns a text description of the current instance that can be used for debugging purposes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TablePrinter

public TablePrinter(java.lang.String[] columnNames,
                    int[] columnWidths)
Initialises a new instance with the specified column names and widths.

Parameters:
columnNames - The names of each column.
columnWidths - The widths of each column.

TablePrinter

public TablePrinter(java.lang.String[] columnNames,
                    int[] columnWidths,
                    boolean[] columnLeftAligned)
Initialises a new instance with the specified column names, widths, and alignments.

Parameters:
columnNames - The names of each column.
columnWidths - The widths of each column.
columnLeftAligned - Whether of not each column should be left-aligned. If null, all columns are left-aligned.
Throws:
java.lang.NullPointerException - If columnNames or columnWidths is null.
java.lang.IllegalArgumentException - If columnNames, columnWidths, or columnLeftAligned (if specified) are different lengths.
Method Detail

prettyPrint

public java.lang.String prettyPrint(java.util.List data)
Returns a string containing the specified dataset, which must be a list of java.util.List instances, in a plain text table, formatted according to the parameters specified at initialization.

Parameters:
data - The data to format. Each element in the list should be a java.util.List. Empty strings will be included in the output for rows where the number column specified is less than the number of columns in the table.
Returns:
A plain text table containing entries from the specified dataset, formatted according to the parameters specified at initialization.
Throws:
java.lang.NullPointerException - If data is null.

prettyPrint

public java.lang.String prettyPrint(java.lang.String[][] data)
Returns a string containing the specified dataset, formatted according to the parameters specified at initialization.

Parameters:
data - The data to format. Empty strings will be included in the output for rows where the number column specified is less than the number of columns in the table.
Returns:
A plain text table containing entries from the specified dataset, formatted according to the parameters specified at initialization.
Throws:
java.lang.NullPointerException - If data is null.

toString

public java.lang.String toString()
Returns a text description of the current instance that can be used for debugging purposes.

Returns:
A text description of the current instance that can be used for debugging purposes.