sdsu.util
Class Table

java.lang.Object
  |
  +--sdsu.util.Table
Direct Known Subclasses:
LabeledTable

public class Table
extends java.lang.Object
implements java.lang.Cloneable, Stringizable, java.io.Serializable

This class is a two dimensional table. Indexing of rows and columns starts at 0. The table is growable. Surplus rows and columns are allocated to reduce cost of growing the table. Surplus rows/columns can not be accessed until rows or columns are added. Table can be converted from/to string/streams. In string representation default column separator is ',' and default row separator is ';'. The recreated table object will only contain string representations of the original elements in the table.
Version Info
1.1 Add support for Stringizable, metaData, changed exception thrown by fromString

Version:
1.1 30 August 1997
Author:
Roger Whitney (whitney@cs.sdsu.edu)
See Also:
SimpleTokenizer, Stringizer, TokenCharacters, Serialized Form

Constructor Summary
Table()
          Create a new table with no rows and no columns
Table(int initNumberOfRows, int initNumberOfColumns)
          Create a new table with given number of rows and columns.
 
Method Summary
 void addColumn(java.util.Vector newColumn)
          Add a new column to the end of the Table.
 void addColumns(Table newColumns)
          Add a table as new columns.
 void addRow(java.util.Vector newRow)
          Add a new row to the end of the Table.
 void addRows(Table newRows)
          Add table "newRows" at the bottom of the Table.
 java.lang.Object clone()
          Clones this table.
 java.util.Vector columnAt(int column)
          Returns the given column in the table
 Table columnsAt(int startColumn, int endColumn)
          Creates a new table with the specified columns of current table
 Table columnsAt(java.lang.Object key, int row)
          Creates a new table containing all the columns that have the value key in the indicated row
 boolean contains(java.lang.Object element)
          Returns true if the specified object is in the table
 java.lang.Object elementAt(int row, int column)
          Returns the object in table at given row and column.
 java.util.Enumeration elements()
          Returns an enumeration of the non-null elements in row order.
static Table fromColumnVector(java.util.Vector column)
          Create a table using the vector column as the only column in the table
static Table fromRowVector(java.util.Vector row)
          Create a table using the vector row as the only row in the table
 void fromString(java.lang.String tableString)
          Loads a Table from a string.
 LabeledData getMetaData()
          Returns name-valeu pair of data used to parse a string version of a LabeledData object
 java.awt.Point indexOf(java.lang.Object element)
          Searches for the specified object, in row order.
 void insertColumnsAt(Table newColumns, int startIndex)
          Insert table "newColumns" in the Table.
 void insertRowsAt(Table newRows, int startIndex)
          Insert table "newRows" in the Table.
 void load(java.io.InputStream in)
          Loads a Table from an inputstream.
 void load(java.io.Reader in)
          Loads a Table from an inputstream.
 int numberOfColumns()
          Returns the number of columns that can currently be accessed
 int numberOfRows()
          Returns the number of rows that can currently be accessed
 void removeColumnAt(int columnIndex)
          Removes the indicated column from the table
 void removeColumnsAt(int firstColumn, int endColumn)
          Removes the indicated columns from the table
 void removeColumnsAt(java.lang.Object key, int row)
          Removes the indicated columns that have key in the indicated row
 void removeRowAt(int rowIndex)
          Removes the indicated row from the table
 void removeRowsAt(int firstRow, int endRow)
          Removes the indicated rows from the table
 void removeRowsAt(java.lang.Object key, int column)
          Removes the indicated rows that have key in the indicated column
 java.util.Vector rowAt(int row)
          Returns the given row in the table
 Table rowsAt(int startRow, int endRow)
          Creates a new table with the specified rows of current table
 Table rowsAt(java.lang.Object key, int column)
          Returns a new table containing all the rows that have the value key in the indicated column
 void save(java.io.OutputStream out, java.lang.String header)
          Writes ascii representation of a Table to Outputstream
 void setColumnSeparatorChar(char columnSeparatorChar)
          Set character used to separate rows in String representation of a Table
 void setElementAt(java.lang.Object data, int row, int column)
          Places the object in table at given row and column
 void setMetaData(LabeledData metaData)
          Set the current objects metadata to be that found in metaData string
 void setRowSeparatorChar(char rowSeparatorChar)
          Set character used to separate rows in String representation of a Table
 void setTokenCharacters(TokenCharacters newParseTable)
          Sets TokenCharacters used to convert Table from/to strings/streams.
 int size()
          Return the number of items in the table
 java.lang.String toString()
          Converts a Table to a String.
 java.lang.String toString(java.lang.String header)
          Converts a Table to a String with given header information.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Table

public Table()
Create a new table with no rows and no columns

Table

public Table(int initNumberOfRows,
             int initNumberOfColumns)
Create a new table with given number of rows and columns. All elements are set to null.
Method Detail

fromRowVector

public static Table fromRowVector(java.util.Vector row)
Create a table using the vector row as the only row in the table

fromColumnVector

public static Table fromColumnVector(java.util.Vector column)
Create a table using the vector column as the only column in the table

getMetaData

public LabeledData getMetaData()
Returns name-valeu pair of data used to parse a string version of a LabeledData object
Specified by:
getMetaData in interface Stringizable

setMetaData

public void setMetaData(LabeledData metaData)
Set the current objects metadata to be that found in metaData string
Specified by:
setMetaData in interface Stringizable

setTokenCharacters

public void setTokenCharacters(TokenCharacters newParseTable)
Sets TokenCharacters used to convert Table from/to strings/streams. Current values for rowSeparatorChar and columnSeparatorChar override separator settings in TokenCharacters object.

setRowSeparatorChar

public void setRowSeparatorChar(char rowSeparatorChar)
Set character used to separate rows in String representation of a Table

setColumnSeparatorChar

public void setColumnSeparatorChar(char columnSeparatorChar)
Set character used to separate rows in String representation of a Table

indexOf

public java.awt.Point indexOf(java.lang.Object element)
Searches for the specified object, in row order.
Returns:
java.awt.Point x is row location, y is column location, -1 indicates object not found

contains

public boolean contains(java.lang.Object element)
Returns true if the specified object is in the table

clone

public java.lang.Object clone()
Clones this table. The elements are not cloned.

elementAt

public java.lang.Object elementAt(int row,
                                  int column)
                           throws java.lang.ArrayIndexOutOfBoundsException
Returns the object in table at given row and column.
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if row or column is not a valid index

rowAt

public java.util.Vector rowAt(int row)
                       throws java.lang.ArrayIndexOutOfBoundsException
Returns the given row in the table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if row is not a valid index

columnAt

public java.util.Vector columnAt(int column)
                          throws java.lang.ArrayIndexOutOfBoundsException
Returns the given column in the table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if column is not a valid index

columnsAt

public Table columnsAt(int startColumn,
                       int endColumn)
                throws java.lang.ArrayIndexOutOfBoundsException
Creates a new table with the specified columns of current table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if startColumn or endColumn is not a valid column index

columnsAt

public Table columnsAt(java.lang.Object key,
                       int row)
                throws java.lang.ArrayIndexOutOfBoundsException
Creates a new table containing all the columns that have the value key in the indicated row
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if row is not a valid row index

rowsAt

public Table rowsAt(int startRow,
                    int endRow)
             throws java.lang.ArrayIndexOutOfBoundsException
Creates a new table with the specified rows of current table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if startRow or endRow is not a valid row index

rowsAt

public Table rowsAt(java.lang.Object key,
                    int column)
             throws java.lang.ArrayIndexOutOfBoundsException
Returns a new table containing all the rows that have the value key in the indicated column
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if column is not a valid index

elements

public java.util.Enumeration elements()
Returns an enumeration of the non-null elements in row order.

setElementAt

public void setElementAt(java.lang.Object data,
                         int row,
                         int column)
                  throws java.lang.ArrayIndexOutOfBoundsException
Places the object in table at given row and column
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if row or column is not a valid index

addRow

public void addRow(java.util.Vector newRow)
Add a new row to the end of the Table. If newRow has more elements than the table has columns, more columns are added to the table.
Parameters:
newRow - vector of item added to the new row

addRows

public void addRows(Table newRows)
Add table "newRows" at the bottom of the Table. Table grows to include all elements of newRows
Parameters:
newRows - table to add to bottom

insertRowsAt

public void insertRowsAt(Table newRows,
                         int startIndex)
                  throws java.lang.ArrayIndexOutOfBoundsException
Insert table "newRows" in the Table. "newRows" is inserted at row "startIndex" of current table. The row "startIndex" prior to the insert will follow the newly added rows Table grows to include all elements of newRows
Parameters:
startIndex - the first row of newRows becomes row startIndex in current table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if startIndex is not a valid row index

addColumn

public void addColumn(java.util.Vector newColumn)
Add a new column to the end of the Table. If newColumn has more elements than the table has rows, more rows are added to the table.
Parameters:
newColumn - vector of item added to the new column

addColumns

public void addColumns(Table newColumns)
Add a table as new columns.
Parameters:
newColumns - table to add

insertColumnsAt

public void insertColumnsAt(Table newColumns,
                            int startIndex)
                     throws java.lang.ArrayIndexOutOfBoundsException
Insert table "newColumns" in the Table. "newColumns" is inserted at column "startIndex" of current table. The column "startIndex" prior to the insert will follow the newly added columns Table grows to include all elements of newColumns
Parameters:
startIndex - the first column of newColumns becomes column startIndex in current table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if startIndex is not a valid column index

removeRowAt

public void removeRowAt(int rowIndex)
                 throws java.lang.ArrayIndexOutOfBoundsException
Removes the indicated row from the table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if rowIndex is not a valid row index

removeRowsAt

public void removeRowsAt(int firstRow,
                         int endRow)
                  throws java.lang.ArrayIndexOutOfBoundsException
Removes the indicated rows from the table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if firstRow > endRow, or either firstRow or endRow are not a valid row index

removeRowsAt

public void removeRowsAt(java.lang.Object key,
                         int column)
                  throws java.lang.ArrayIndexOutOfBoundsException,
                         java.lang.NullPointerException
Removes the indicated rows that have key in the indicated column
Parameters:
key - the key to be searched for
column - column in table to search for the key
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if column is not a valid column index
java.lang.NullPointerException - thrown if key is null

removeColumnAt

public void removeColumnAt(int columnIndex)
                    throws java.lang.ArrayIndexOutOfBoundsException
Removes the indicated column from the table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if columnIndex is not a valid column index

removeColumnsAt

public void removeColumnsAt(int firstColumn,
                            int endColumn)
                     throws java.lang.ArrayIndexOutOfBoundsException
Removes the indicated columns from the table
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if firstColumn > endColumn, or either firstColumn or endColumn are not a valid column index

removeColumnsAt

public void removeColumnsAt(java.lang.Object key,
                            int row)
                     throws java.lang.ArrayIndexOutOfBoundsException,
                            java.lang.NullPointerException
Removes the indicated columns that have key in the indicated row
Parameters:
key - the key to be searched for
row - row in table to search for the key
Throws:
java.lang.ArrayIndexOutOfBoundsException - thrown if row is not a valid row index
java.lang.NullPointerException - thrown if key is null

size

public int size()
Return the number of items in the table

numberOfColumns

public int numberOfColumns()
Returns the number of columns that can currently be accessed

numberOfRows

public int numberOfRows()
Returns the number of rows that can currently be accessed

toString

public java.lang.String toString()
Converts a Table to a String.
Specified by:
toString in interface Stringizable
Overrides:
toString in class java.lang.Object

toString

public java.lang.String toString(java.lang.String header)
Converts a Table to a String with given header information.
Specified by:
toString in interface Stringizable

save

public void save(java.io.OutputStream out,
                 java.lang.String header)
Writes ascii representation of a Table to Outputstream

load

public void load(java.io.InputStream in)
          throws java.io.IOException
Loads a Table from an inputstream. Data in stream must be in Table format.
Throws:
java.lang.IOException - thrown if there is a error in reading

load

public void load(java.io.Reader in)
          throws java.io.IOException
Loads a Table from an inputstream. Data in stream must be in Table format.
Throws:
java.lang.IOException - thrown if there is a error in reading

fromString

public void fromString(java.lang.String tableString)
                throws ConversionException
Loads a Table from a string. Data in stream must be in Table format.
Specified by:
fromString in interface Stringizable
Throws:
java.lang.IOException - thrown if there is a error reading the tableString format