CS 1705 Library

cs1705
Interface DataTable

All Known Implementing Classes:
CSVDataTable

public interface DataTable

A database table or spreadsheet-like abstraction that consists of rows, each of which has an unordered collection of columns. Each row has a row number which, by default, is the primary key of the table. Each row's columns contain string values. Each row can be named as desired by the user. The default column names are "0", "1", "2", ....

A primary key for a table is a column where each value uniquely determines the row. For example, if you had a table of grades, the student's name would be the primary key column. The primary key can be set to whichever row is desired, but the column must uniquely identify each row in the table.

The purpose of this class is to actually pull data from the internet for use in the class, however, it also can be used as a table, where one adds, removes, and updates data on one's own. The table updates can be saved when desired.

The following invariants should be maintained for any implementation:

Version:
2007.11.13
Author:
Matthew Thornton

Nested Class Summary
static class DataTable.HashedRow
          This is a default concrete implementation of the Row interface.
static interface DataTable.Row
          This interface defines the model used for each row in the table.
 
Method Summary
 void addColumn(String columnName)
          This method adds a column to the table.
 void addColumn(String columnName, String defaultValue)
          This method adds a column to the table, setting this column to the specified default value in every row.
 void addRow(DataTable.Row row)
          This method adds a row to the table.
 double avg(String column)
          This method computes the average of a column that has numeric values.
 int colCount()
          This method returns the number of columns in the data table.
 void copyRowsFrom(DataTable dataSet)
          This method copies an existing DataTable's rows into the DataTable.
 int count(String column, String value)
          This method counts the number of rows in the specified column that have the value given by the value parameter.
 ArrayList<DataTable.Row> getAllRows()
          This method returns a representation of the Data Table.
 String getCell(int row, int column)
          This method returns the String value in the table at a specified row/column location.
 String getCell(int row, String column)
          This method returns the String value in the table at a specified row/column location.
 String getCell(String row, int column)
          This method returns the String value in the table at a specified row/column location.
 String getCell(String row, String column)
          This method returns the String value in the table at a specified row/column location.
 ArrayList<String> getColumnNames()
          This method returns a list of the names of all of the columns of the data table.
 double getDoubleCell(int row, int column)
          This method returns the double value in the table at a specified row/column location.
 double getDoubleCell(int row, String column)
          This method returns the double value in the table at a specified row/column location.
 double getDoubleCell(String row, int column)
          This method returns the double value in the table at a specified row/column location.
 double getDoubleCell(String row, String column)
          This method returns the double value in the table at a specified row/column location.
 int getIntCell(int row, int column)
          This method returns the int value in the table at a specified row/column location.
 int getIntCell(int row, String column)
          This method returns the int value in the table at a specified row/column location.
 int getIntCell(String row, int column)
          This method returns the int value in the table at a specified row/column location.
 int getIntCell(String row, String column)
          This method returns the int value in the table at a specified row/column location.
 String getPrimaryKey()
          This method returns the column name that acts as the primary key for the table.
 DataTable.Row getRow(int row)
          This method returns a map that represents the desired row (indicated by the primary key).
 DataTable.Row getRow(String row)
          This method returns a Map that represents the desired row.
 double max(String column)
          This method computes the maximum value of a column that has numeric values.
 double min(String column)
          This method computes the minimum value of a column that has numeric values.
 void remapColumnNames(Map<String,String> columnMap)
          This method replaces each column name in the table with a matching key in columnMap with the value of the mapping.
 DataTable.Row removeRow(int row)
          This method removes the row identified by row number.
 DataTable.Row removeRow(String row)
          This method removes the row (to be determined by the key).
 int rowCount()
          This method returns the number of rows in the data table.
 void save(String filename)
          This method saves the data table that has been generated/manipulated in the format desired for the implementation of the interface.
 void setCell(int row, int column, double value)
          This method allows you to update a desired row and column with new data as a double.
 void setCell(int row, int column, int value)
          This method allows you to update a desired row and column with new data as an int.
 void setCell(int row, int column, String value)
          This method allows you to update a desired row and column with new data as a String.
 void setCell(int row, String column, double value)
          This method allows you to update a desired row and column with new data as a double.
 void setCell(int row, String column, int value)
          This method allows you to update a desired row and column with new data as an int.
 void setCell(int row, String column, String value)
          This method allows you to update a desired row and column with new data as a String.
 void setCell(String row, int column, double value)
          This method allows you to update a desired row and column with new data as a double.
 void setCell(String row, int column, int value)
          This method allows you to update a desired row and column with new data as an int.
 void setCell(String row, int column, String value)
          This method allows you to update a desired row and column with new data as a String.
 void setCell(String row, String column, double value)
          This method allows you to update a desired row and column with new data as a double.
 void setCell(String row, String column, int value)
          This method allows you to update a desired row and column with new data as an int.
 void setCell(String row, String column, String value)
          This method allows you to update a desired row and column with new data as a String.
 void setColumnNames(ArrayList<String> columnNames)
          This method sets an arbitrary name for each of the columns in the table.
 void setPrimaryKey(String key)
          This method sets the column name that acts as the primary key for the table.
 

Method Detail

rowCount

int rowCount()
This method returns the number of rows in the data table.

Returns:
the number of rows in the data table.

colCount

int colCount()
This method returns the number of columns in the data table.

Returns:
the number of columns in the data table.

getCell

String getCell(int row,
               String column)
This method returns the String value in the table at a specified row/column location.

Parameters:
row - index of row to retrieve (starting at zero)
column - the name of the desired column
Returns:
the value in that position as a String object

getCell

String getCell(int row,
               int column)
This method returns the String value in the table at a specified row/column location.

Parameters:
row - index of row to retrieve (starting at zero)
column - index of column to retrieve (starting at zero)
Returns:
the value in that position as a String object

getCell

String getCell(String row,
               String column)
This method returns the String value in the table at a specified row/column location.

Parameters:
row - primary key of row
column - the name of the desired column
Returns:
the value in that position as a String object

getCell

String getCell(String row,
               int column)
This method returns the String value in the table at a specified row/column location.

Parameters:
row - primary key of row
column - index of column to retrieve (starting at zero)
Returns:
the value in that position as a String object

getIntCell

int getIntCell(int row,
               String column)
               throws NumberFormatException
This method returns the int value in the table at a specified row/column location.

Parameters:
row - index of row to retrieve (starting at zero)
column - the name of the desired column
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain an int value.

getIntCell

int getIntCell(int row,
               int column)
               throws NumberFormatException
This method returns the int value in the table at a specified row/column location.

Parameters:
row - index of row to retrieve (starting at zero)
column - index of column to retrieve (starting at zero)
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain an int value.

getIntCell

int getIntCell(String row,
               String column)
               throws NumberFormatException
This method returns the int value in the table at a specified row/column location.

Parameters:
row - primary key of row
column - the name of the desired column
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain an int value.

getIntCell

int getIntCell(String row,
               int column)
               throws NumberFormatException
This method returns the int value in the table at a specified row/column location.

Parameters:
row - primary key of row
column - index of column to retrieve (starting at zero)
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain an int value.

getDoubleCell

double getDoubleCell(int row,
                     String column)
                     throws NumberFormatException
This method returns the double value in the table at a specified row/column location.

Parameters:
row - index of row to retrieve (starting at zero)
column - the name of the desired column
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain a double value.

getDoubleCell

double getDoubleCell(int row,
                     int column)
                     throws NumberFormatException
This method returns the double value in the table at a specified row/column location.

Parameters:
row - index of row to retrieve (starting at zero)
column - index of column to retrieve (starting at zero)
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain a double value.

getDoubleCell

double getDoubleCell(String row,
                     String column)
                     throws NumberFormatException
This method returns the double value in the table at a specified row/column location.

Parameters:
row - primary key of row
column - the name of the desired column
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain a double value.

getDoubleCell

double getDoubleCell(String row,
                     int column)
                     throws NumberFormatException
This method returns the double value in the table at a specified row/column location.

Parameters:
row - primary key of row
column - index of column to retrieve (starting at zero)
Returns:
the value in that position as an int
Throws:
NumberFormatException - if the cell does not contain a double value.

getRow

DataTable.Row getRow(int row)
This method returns a map that represents the desired row (indicated by the primary key).

Parameters:
row - index of row to retrieve (starting from zero)
Returns:
the Map representing the desired row.

getRow

DataTable.Row getRow(String row)
This method returns a Map that represents the desired row. The map is a map of column names to values.

Parameters:
row - primary key of row to retrieve
Returns:
the Map representing the desired row.

setCell

void setCell(int row,
             String column,
             String value)
This method allows you to update a desired row and column with new data as a String.

Parameters:
row - index of row to change (starting from zero)
column - name of the column to change
value - the value that is to replace the current value

setCell

void setCell(int row,
             String column,
             int value)
This method allows you to update a desired row and column with new data as an int.

Parameters:
row - index of row to change (starting from zero)
column - name of the column to change
value - the value that is to replace the current value

setCell

void setCell(int row,
             String column,
             double value)
This method allows you to update a desired row and column with new data as a double.

Parameters:
row - index of row to change (starting from zero)
column - name of the column to change
value - the value that is to replace the current value

setCell

void setCell(int row,
             int column,
             String value)
This method allows you to update a desired row and column with new data as a String.

Parameters:
row - index of row to change (starting from zero)
column - index of column to change (starting from zero)
value - the value that is to replace the current value

setCell

void setCell(int row,
             int column,
             int value)
This method allows you to update a desired row and column with new data as an int.

Parameters:
row - index of row to change (starting from zero)
column - index of column to change (starting from zero)
value - the value that is to replace the current value

setCell

void setCell(int row,
             int column,
             double value)
This method allows you to update a desired row and column with new data as a double.

Parameters:
row - index of row to change (starting from zero)
column - index of column to change (starting from zero)
value - the value that is to replace the current value

setCell

void setCell(String row,
             String column,
             String value)
This method allows you to update a desired row and column with new data as a String.

Parameters:
row - primary key of the row to change
column - name of the column to change
value - the value that is to replace the current value

setCell

void setCell(String row,
             String column,
             int value)
This method allows you to update a desired row and column with new data as an int.

Parameters:
row - primary key of the row to change
column - name of the column to change
value - the value that is to replace the current value

setCell

void setCell(String row,
             String column,
             double value)
This method allows you to update a desired row and column with new data as a double.

Parameters:
row - primary key of the row to change
column - name of the column to change
value - the value that is to replace the current value

setCell

void setCell(String row,
             int column,
             String value)
This method allows you to update a desired row and column with new data as a String.

Parameters:
row - primary key of the row to change
column - index of column to change (starting from zero)
value - the value that is to replace the current value

setCell

void setCell(String row,
             int column,
             int value)
This method allows you to update a desired row and column with new data as an int.

Parameters:
row - primary key of the row to change
column - index of column to change (starting from zero)
value - the value that is to replace the current value

setCell

void setCell(String row,
             int column,
             double value)
This method allows you to update a desired row and column with new data as a double.

Parameters:
row - primary key of the row to change
column - index of column to change (starting from zero)
value - the value that is to replace the current value

removeRow

DataTable.Row removeRow(int row)
This method removes the row identified by row number. The method permanently deletes the row from the table and when the table is queried the row will not be accessible either. As with in others, this is independent of the primary key and just the arbitrary ordering of the table. The method also renumbers the rows so that if the primary key remains the default, then the row numbers will be consistent between using the integer-based row numbers and their String equivalents.

Parameters:
row - table row to be deleted.
Returns:
the Map representing the Row that was removed from the table.

removeRow

DataTable.Row removeRow(String row)
This method removes the row (to be determined by the key). The method permanently deletes the row from the table and when the table is queried the row will not be accessible either. The method also renumbers the rows so that if the primary key remains the default, then the row numbers will be consistent between using the integer-based row numbers and their String equivalents.

Parameters:
row - table row to be deleted.
Returns:
the Map representing the Row that was removed from the table.

addColumn

void addColumn(String columnName)
This method adds a column to the table. For each row in the table, the column's value will be null.

Parameters:
columnName - the name of the column to be added.

addColumn

void addColumn(String columnName,
               String defaultValue)
This method adds a column to the table, setting this column to the specified default value in every row.

Parameters:
columnName - the name of the column to be added.
defaultValue - the default value to use for the column.

addRow

void addRow(DataTable.Row row)
This method adds a row to the table. For each column, the Map should have a value for the column name. If not, that column value is null. The method also renumbers the rows so that if the primary key remains the default, then the row numbers will be consistent between using the integer-based row numbers and their String equivalents.

Parameters:
row - The row to add

setPrimaryKey

void setPrimaryKey(String key)
This method sets the column name that acts as the primary key for the table. If the column name is not in the table, then the primary key stays as the current primary key. The method also renumbers the rows so that if the primary key remains the default, then the row numbers will be consistent between using the integer-based row numbers and their String equivalents.

Parameters:
key - the column name to be set as the primary key.

getPrimaryKey

String getPrimaryKey()
This method returns the column name that acts as the primary key for the table.

Returns:
the column name that acts as the primary key. The default is the row number.

getColumnNames

ArrayList<String> getColumnNames()
This method returns a list of the names of all of the columns of the data table. The column names are returned as an array list with each position representing one of the columns in the table.

Returns:
an array list with a reporting of all of the column names in the table.

setColumnNames

void setColumnNames(ArrayList<String> columnNames)
This method sets an arbitrary name for each of the columns in the table.

Parameters:
columnNames - names of columns to be set in the table.

remapColumnNames

void remapColumnNames(Map<String,String> columnMap)
This method replaces each column name in the table with a matching key in columnMap with the value of the mapping.

Parameters:
columnMap - names of columns to be replaced with the associated value to replace it with.

getAllRows

ArrayList<DataTable.Row> getAllRows()
This method returns a representation of the Data Table. The representation is an array list where each position represents a row. Each row is a map with an unordered collection of columns.

Returns:
an array list representing the data table.

copyRowsFrom

void copyRowsFrom(DataTable dataSet)
This method copies an existing DataTable's rows into the DataTable. The primary key remains what it was before the copy and the incoming DataTable completely replaces the current DataTable.

Parameters:
dataSet - a DataTable object to copy into the current DataTable.

count

int count(String column,
          String value)
This method counts the number of rows in the specified column that have the value given by the value parameter.

Parameters:
column - the desired column to count on.
value - the desired value to count.
Returns:
the count of the number of rows that have the value given by the value parameter.

avg

double avg(String column)
           throws NumberFormatException
This method computes the average of a column that has numeric values. Throws an exception if a non-number is found in the column.

Parameters:
column - the column to compute the average of.
Returns:
the average of the numerical values in the column.
Throws:
NumberFormatException - if the column has a string that cannot be parsed as a number.

max

double max(String column)
           throws NumberFormatException
This method computes the maximum value of a column that has numeric values. Throws an exception if a non-number is found in the column.

Parameters:
column - the column to compute the max number of.
Returns:
the max of the numerical values in the column.
Throws:
NumberFormatException - if the column has a string that cannot be parsed as a number.

min

double min(String column)
           throws NumberFormatException
This method computes the minimum value of a column that has numeric values. Throws an exception if a non-number is found in the column.

Parameters:
column - the column to compute the min number of.
Returns:
the min of the numerical values in the column.
Throws:
NumberFormatException - if the column has a string that cannot be parsed as a number.

save

void save(String filename)
This method saves the data table that has been generated/manipulated in the format desired for the implementation of the interface.

Parameters:
filename - the path to the file that is to be written.

Last updated: Wed, Apr 1, 2009 • 12:29 AM EDT

Copyright © 2009 Virginia Tech.