at.spardat.xma.datasource
Class TabularData

java.lang.Object
  extended byat.spardat.xma.datasource.TabularData
All Implemented Interfaces:
ITabularData
Direct Known Subclasses:
TabularDomData

public class TabularData
extends java.lang.Object
implements ITabularData

A TabularDatatable stores data in a row/column matrix of Atoms in memory, with column header names. It can be read from a File or an InputStream and written to an OutputStream or File in CSV format. The "UTF-8" encoding is used.


Constructor Summary
TabularData()
          Creates an empty table.
TabularData(java.lang.String[] columnNames, Atom[][] rows)
          Creates a table containing given data
 
Method Summary
 void addColumn(java.lang.String name)
          Add a column.
 void addRow(TabularDataRow r)
          Adds a row in the table.
 void clear()
          Clears the table data.
 Atom getCell(int row, int col)
          Get the value from a cell specified bei row and col.
 Atom getCell(int row, java.lang.String columnName)
          Returns the value from a cell specified bei row and columnName.
 int getColumnIndex(java.lang.String colName)
          Returns the index of the column with the name colName.
 java.lang.String getColumnName(int col)
          Returns the header name of the n'th column in the table
 TabularDataRow getRow(int row)
          Gets the table row with the index row.
 int hashCode()
          Maps this TabularData to an integer hash code.
 int numCols()
          Returns the number of columns in this table
static TabularData readFrom(java.io.File file)
          Creates a table by reading it in from a UTF-8 encoded CSV file.
static TabularData readFrom(java.io.InputStream inputStream)
          Creates a table by reading it from an InputStream in UTF-8-encoded CSV format.
 void save(java.io.File file)
          Store the table in to a given file, in CSV format.
 int size()
          Returns the number of rows in this table
 void write(java.io.OutputStream outputStream)
          Writes a TabularData to an OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TabularData

public TabularData()
Creates an empty table.


TabularData

public TabularData(java.lang.String[] columnNames,
                   Atom[][] rows)
            throws java.lang.IllegalArgumentException
Creates a table containing given data

Parameters:
columnNames - - The column header names
rows - - The data as a matrix of strings
Method Detail

readFrom

public static TabularData readFrom(java.io.File file)
                            throws java.io.IOException
Creates a table by reading it in from a UTF-8 encoded CSV file. In CSV format, each record is stored on a separate line, with the column header names on the first line. Fields on a line are separated by commas. Spaces before and after the comma are not allowed. Every value must be delimted using double quotes.

Parameters:
file - the file to read from
Throws:
java.io.IOException

readFrom

public static TabularData readFrom(java.io.InputStream inputStream)
                            throws java.io.IOException
Creates a table by reading it from an InputStream in UTF-8-encoded CSV format. This method closes the inputStream when finished.

Parameters:
inputStream - - The InputStream to read from.
Returns:
The table read.
Throws:
java.io.IOException - and RuntimeExceptions if inputStream could not be read.

addColumn

public void addColumn(java.lang.String name)
               throws java.lang.IllegalArgumentException
Add a column. Note that adding columns is only allowed after construction, when there are no rows in the table.

Parameters:
name - the name of the column to be added.
Throws:
java.lang.IllegalArgumentException - if the name is not valid or a column with that name already exists.
java.lang.IllegalStateException - if there are already rows in the table.

size

public int size()
Description copied from interface: ITabularData
Returns the number of rows in this table

Specified by:
size in interface ITabularData
See Also:
ITabularData.size()

numCols

public int numCols()
Description copied from interface: ITabularData
Returns the number of columns in this table

Specified by:
numCols in interface ITabularData
See Also:
ITabularData.numCols()

getRow

public TabularDataRow getRow(int row)
Gets the table row with the index row.

Parameters:
row - the index of the row.
Returns:
TabularDataRow, never null
Throws:
java.lang.RuntimeException - if row out of bounds.

addRow

public void addRow(TabularDataRow r)
            throws java.lang.IllegalArgumentException
Adds a row in the table.

Parameters:
r - the row to add.
Throws:
java.lang.IllegalArgumentException - if no columns have been defined yet.

getCell

public Atom getCell(int row,
                    int col)
Get the value from a cell specified bei row and col.

Specified by:
getCell in interface ITabularData
Parameters:
row - - The row index.
col - - The column index.
Returns:
non null Atom.
See Also:
ITabularData.getCell(int, int)

getCell

public Atom getCell(int row,
                    java.lang.String columnName)
Returns the value from a cell specified bei row and columnName.

Specified by:
getCell in interface ITabularData
Parameters:
row - - The row index.
columnName - - The name of the column.
Returns:
non null Atom.

getColumnIndex

public int getColumnIndex(java.lang.String colName)
Returns the index of the column with the name colName.

Specified by:
getColumnIndex in interface ITabularData
Parameters:
colName - the name of the column
Returns:
the index of the column or -1, if there is no such column.

getColumnName

public java.lang.String getColumnName(int col)
Returns the header name of the n'th column in the table

Specified by:
getColumnName in interface ITabularData
Parameters:
col - the column index
Throws:
java.lang.IllegalArgumentException - if col is out of range.

write

public void write(java.io.OutputStream outputStream)
           throws java.io.IOException
Writes a TabularData to an OutputStream.

Parameters:
outputStream - - The OutputStream to write the TabularData to.
Throws:
java.io.IOException

save

public void save(java.io.File file)
          throws java.io.IOException
Store the table in to a given file, in CSV format.

Parameters:
file - - The file to write to.
Throws:
java.io.IOException

clear

public void clear()
Clears the table data.


hashCode

public int hashCode()
Maps this TabularData to an integer hash code. Since this hash code is used for probabilistic uptodate checks, be sure to include any table data in the calculation and ensure that the generated hash is equally distributed over the int domain.

See Also:
Object.hashCode()