org.jqurantree.analysis
Class AnalysisTable

java.lang.Object
  extended by org.jqurantree.analysis.AnalysisTable
All Implemented Interfaces:
java.lang.Iterable<java.lang.Object[]>

public class AnalysisTable
extends java.lang.Object
implements java.lang.Iterable<java.lang.Object[]>

The AnalyisTable is a general purpose class which may be used to tabulate, sort, group and export results. The table organizes a set of cells into rows and columns, with each column having a unique column name. The analysis table may be used as follows:

  1. A new analysis table is created by specifying a list of column names.
  2. Rows are added to the table by a Java program. These are typically the results of a search, or other program you have written to collect data.
  3. After rows have been added, the results can be analysed.

Once the table has been populated, the following operations may be performed:

Author:
Kais Dukes

Constructor Summary
AnalysisTable(java.lang.String... columnNames)
          Creates an empty analysis table with the specified columns and without any rows.
 
Method Summary
 void add(java.lang.Object... values)
          Adds a new row to the analysis table using of the specified values.
 int getColumnCount()
          Gets the number of columns in the table.
 int getColumnIndex(java.lang.String columnName)
          Gets the index of a column in the table, by column name.
 java.lang.String getColumnName(int columnIndex)
          Gets the name of a column in the table, by column index.
 int getInteger(int rowIndex, int columnIndex)
          Converts the value of a cell to an int, given the cell's row index and column index.
 int getInteger(int rowIndex, java.lang.String columnName)
          Converts the value of a cell to an int, given the cell's row index and column name.
 int getRowCount()
          Gets the number of rows in the table.
 java.lang.String getString(int rowIndex, int columnIndex)
          Converts the value of a cell to a string, given the cell's row index and column index.
 java.lang.String getString(int rowIndex, java.lang.String columnName)
          Converts the value of a cell to a string, given the cell's row index and column name.
 java.lang.Object getValue(int rowIndex, int columnIndex)
          Gets the value of a cell in the table, given the cell's row index and column index.
 java.lang.Object getValue(int rowIndex, java.lang.String columnName)
          Gets the value of a cell in the table, given the cell's row index and column name.
 AnalysisTable group(java.lang.String... columnNames)
          Creates a new table by grouping this analysis table by the specified list of columns.
 java.util.Iterator<java.lang.Object[]> iterator()
          Returns an iterator used to enumerate through all rows in the table.
 void sort(java.lang.String columnName)
          Sorts all rows in ascending order, by the specified column.
 void sort(java.lang.String columnName, SortOrder direction)
          Sorts all rows by a column, in the specified sort order.
 java.lang.String toString()
          Converts the analysis table to a string.
 java.lang.String toString(int rowCount)
          Converts the top rows of the analysis table to a string.
 void writeFile(java.lang.String filename)
          Saves the contents of the analysis table to disk, by writing out a delimited file.
 void writeFile(java.lang.String filename, char delimiter)
          Saves the contents of the analysis table to disk, by writing out a delimited file.
 void writeFile(java.lang.String filename, char delimiter, int rowCount)
          Saves the top rows of the analysis table to disk, by writing out a delimited file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AnalysisTable

public AnalysisTable(java.lang.String... columnNames)
Creates an empty analysis table with the specified columns and without any rows.

Parameters:
columnNames - an array of column names.
Method Detail

add

public void add(java.lang.Object... values)
Adds a new row to the analysis table using of the specified values.

Parameters:
values - the values that make up the row. The number of values specified should match the number of columns in the table.

getRowCount

public int getRowCount()
Gets the number of rows in the table.

Returns:
a positive number if the table contains any data, otherwise zero.

getColumnCount

public int getColumnCount()
Gets the number of columns in the table.

Returns:
a positive value indicating the number of columns.

getColumnName

public java.lang.String getColumnName(int columnIndex)
Gets the name of a column in the table, by column index.

Parameters:
columnIndex - the zero-based index of the column.
Returns:
The name of the specified column.

getColumnIndex

public int getColumnIndex(java.lang.String columnName)
Gets the index of a column in the table, by column name.

Parameters:
columnName - the name of column.
Returns:
the zero-based index of the specified column, or -1 if the column name does not occur.

getValue

public java.lang.Object getValue(int rowIndex,
                                 int columnIndex)
Gets the value of a cell in the table, given the cell's row index and column index.

Parameters:
rowIndex - the zero-based index of the row containing the cell.
columnIndex - the zero-based index of the column containing the cell.
Returns:
the value of the cell at the specified position.

getValue

public java.lang.Object getValue(int rowIndex,
                                 java.lang.String columnName)
Gets the value of a cell in the table, given the cell's row index and column name.

Parameters:
rowIndex - the zero-based index of the row containing the cell.
columnName - the name of the column containing the cell.
Returns:
the value of the cell at the specified position.

getInteger

public int getInteger(int rowIndex,
                      int columnIndex)
Converts the value of a cell to an int, given the cell's row index and column index.

Parameters:
rowIndex - the zero-based index of the row containing the cell.
columnIndex - the zero-based index of the column containing the cell.
Returns:
the value of the cell at the specified position.

getInteger

public int getInteger(int rowIndex,
                      java.lang.String columnName)
Converts the value of a cell to an int, given the cell's row index and column name.

Parameters:
rowIndex - the zero-based index of the row containing the cell.
columnName - the name of the column containing the cell.
Returns:
the value of the cell at the specified position.

getString

public java.lang.String getString(int rowIndex,
                                  int columnIndex)
Converts the value of a cell to a string, given the cell's row index and column index.

Parameters:
rowIndex - the zero-based index of the row containing the cell.
columnIndex - the zero-based index of the column containing the cell.
Returns:
the value of the cell at the specified position.

getString

public java.lang.String getString(int rowIndex,
                                  java.lang.String columnName)
Converts the value of a cell to a string, given the cell's row index and column name.

Parameters:
rowIndex - the zero-based index of the row containing the cell.
columnName - the name of the column containing the cell.
Returns:
the value of the cell at the specified position.

toString

public java.lang.String toString()
Converts the analysis table to a string. The table will be correctly formatted and all columns will be aligned.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of the analysis table.

toString

public java.lang.String toString(int rowCount)
Converts the top rows of the analysis table to a string.

Parameters:
rowCount - the number of rows to use when converting the table. This value should be a positive number.
Returns:
a string representing the top rows of the table.

writeFile

public void writeFile(java.lang.String filename)
Saves the contents of the analysis table to disk, by writing out a delimited file. The file will be written using the default delimiter, the tab character.

Parameters:
filename - the name of the file to write to.

writeFile

public void writeFile(java.lang.String filename,
                      char delimiter)
Saves the contents of the analysis table to disk, by writing out a delimited file. The file will be written using the specified delimiter.

Parameters:
filename - the name of the file to write to.
delimiter - the delimiter character used to write the file

writeFile

public void writeFile(java.lang.String filename,
                      char delimiter,
                      int rowCount)
Saves the top rows of the analysis table to disk, by writing out a delimited file. The file will be written using the specified delimiter.

Parameters:
filename - the name of the file to write to.
delimiter - the delimiter character used to write the file
rowCount - the number of rows to write to file. This value should be a positive number.

iterator

public java.util.Iterator<java.lang.Object[]> iterator()
Returns an iterator used to enumerate through all rows in the table.

Specified by:
iterator in interface java.lang.Iterable<java.lang.Object[]>
Returns:
an iterator.

sort

public void sort(java.lang.String columnName)
Sorts all rows in ascending order, by the specified column.

Parameters:
columnName - the name of the column to sort by.

sort

public void sort(java.lang.String columnName,
                 SortOrder direction)
Sorts all rows by a column, in the specified sort order.

Parameters:
columnName - the name of the column to sort by.
direction - the order to sort the rows in, ascending or descending.

group

public AnalysisTable group(java.lang.String... columnNames)
Creates a new table by grouping this analysis table by the specified list of columns. The new table will contain the specified columns, together with an additional column named Count, which contains the number of items in each group.

Parameters:
columnNames - the list of columns to group by.
Returns:
the grouped analysis table.


Copyright© Kais Dukes, 2009. All Rights Reserved.