CS 1705 Library

cs1705
Class CSVDataTable

java.lang.Object
  extended by cs1705.CSVDataTable
All Implemented Interfaces:
DataTable

public class CSVDataTable
extends Object
implements DataTable

CSVDataTable is an implementation of DataTable that allows the user to interact with CSV Files. The CSVDataTable class loads a CSV file into a DataTable format that can be maniupulated using the methods of the DataTable class.

Version:
September 19, 2007
Author:
Matthew Thornton

Nested Class Summary
 
Nested classes/interfaces inherited from interface cs1705.DataTable
DataTable.HashedRow, DataTable.Row
 
Constructor Summary
CSVDataTable()
          This constructor creates an empty DataTable with no columns.
CSVDataTable(File file)
          Create a new data table using CSV data from a file.
CSVDataTable(File file, boolean firstRowNames)
          Create a new data table using CSV data from a file.
CSVDataTable(File file, boolean firstRowNames, char delimiter)
          Create a new data table using CSV data on a website.
CSVDataTable(InputStream in, boolean firstRowNames, char delimiter)
          Create a new data table using CSV data from an input stream.
CSVDataTable(String url)
          Create a new data table using CSV data on a website.
CSVDataTable(String url, boolean firstRowNames)
          Create a new data table using CSV data on a website.
CSVDataTable(String url, boolean firstRowNames, char delimiter)
          Create a new data table using CSV data on a website.
 
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.
 char getDelimiter()
          Get the delimiter used by this data set.
 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.
 String getUrl()
           
 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 CSVDataTable in a CSV file format with the first row the column names and the remaining rows the data in the table.
 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> columns)
          This method sets an arbitrary name for each of the columns in the table.
 void setDelimiter(char delimiter)
          Set the delimiter used by this data set, which will affect how this object will be written to a file in the future.
 void setPrimaryKey(String key)
          This method sets the column name that acts as the primary key for the table.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVDataTable

public CSVDataTable()
This constructor creates an empty DataTable with no columns. The key is set to the empty string.


CSVDataTable

public CSVDataTable(String url)
Create a new data table using CSV data on a website. The URL parameter is a link to a data file stored on the web. All data rows will be loaded (i.e., the first row will not be treated as column names), and lines will be parsed assuming the delimiter is a comma.

Parameters:
url - the URL path to the CSV file.

CSVDataTable

public CSVDataTable(String url,
                    boolean firstRowNames)
Create a new data table using CSV data on a website. The URL parameter is a link to a data file stored on the web. All data rows will be loaded, and lines will be parsed assuming the delimiter is a comma.

Parameters:
url - the URL path to the CSV file.
firstRowNames - If true, then the first row in the data source should be treated as containing the names for the columns in the data set.

CSVDataTable

public CSVDataTable(String url,
                    boolean firstRowNames,
                    char delimiter)
Create a new data table using CSV data on a website. The URL parameter is a link to a data file stored on the web.

Parameters:
url - the URL path to the CSV file.
firstRowNames - If true, then the first row in the data source should be treated as containing the names for the columns in the data set.
delimiter - The separator between fields on the same line (e.g., a comma, tab, colon, etc.).

CSVDataTable

public CSVDataTable(File file)
Create a new data table using CSV data from a file. All data rows will be loaded (i.e., the first row will not be treated as column names), and lines will be parsed assuming the delimiter is a comma.

Parameters:
file - the CSV file to read.

CSVDataTable

public CSVDataTable(File file,
                    boolean firstRowNames)
Create a new data table using CSV data from a file. All data rows will be loaded, and lines will be parsed assuming the delimiter is a comma.

Parameters:
file - the CSV file to read.
firstRowNames - If true, then the first row in the data source should be treated as containing the names for the columns in the data set.

CSVDataTable

public CSVDataTable(File file,
                    boolean firstRowNames,
                    char delimiter)
Create a new data table using CSV data on a website. The URL parameter is a link to a data file stored on the web.

Parameters:
file - the CSV file to read.
firstRowNames - If true, then the first row in the data source should be treated as containing the names for the columns in the data set.
delimiter - The separator between fields on the same line (e.g., a comma, tab, colon, etc.).

CSVDataTable

public CSVDataTable(InputStream in,
                    boolean firstRowNames,
                    char delimiter)
Create a new data table using CSV data from an input stream. The URL parameter is a link to a data file stored on the web.

Parameters:
in - the input stream that contains the CSV data.
firstRowNames - If true, then the first row in the data source should be treated as containing the names for the columns in the data set.
delimiter - The separator between fields on the same line (e.g., a comma, tab, colon, etc.).
Method Detail

rowCount

public int rowCount()
Description copied from interface: DataTable
This method returns the number of rows in the data table.

Specified by:
rowCount in interface DataTable
Returns:
the number of rows in the data table.

colCount

public int colCount()
Description copied from interface: DataTable
This method returns the number of columns in the data table.

Specified by:
colCount in interface DataTable
Returns:
the number of columns in the data table.

getCell

public String getCell(int row,
                      String column)
Description copied from interface: DataTable
This method returns the String value in the table at a specified row/column location.

Specified by:
getCell in interface DataTable
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

public String getCell(int row,
                      int column)
Description copied from interface: DataTable
This method returns the String value in the table at a specified row/column location.

Specified by:
getCell in interface DataTable
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

public String getCell(String row,
                      String column)
Description copied from interface: DataTable
This method returns the String value in the table at a specified row/column location.

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

getCell

public String getCell(String row,
                      int column)
Description copied from interface: DataTable
This method returns the String value in the table at a specified row/column location.

Specified by:
getCell in interface DataTable
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

public int getIntCell(int row,
                      String column)
               throws NumberFormatException
Description copied from interface: DataTable
This method returns the int value in the table at a specified row/column location.

Specified by:
getIntCell in interface DataTable
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

public int getIntCell(int row,
                      int column)
               throws NumberFormatException
Description copied from interface: DataTable
This method returns the int value in the table at a specified row/column location.

Specified by:
getIntCell in interface DataTable
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

public int getIntCell(String row,
                      String column)
               throws NumberFormatException
Description copied from interface: DataTable
This method returns the int value in the table at a specified row/column location.

Specified by:
getIntCell in interface DataTable
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

public int getIntCell(String row,
                      int column)
               throws NumberFormatException
Description copied from interface: DataTable
This method returns the int value in the table at a specified row/column location.

Specified by:
getIntCell in interface DataTable
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

public double getDoubleCell(int row,
                            String column)
                     throws NumberFormatException
Description copied from interface: DataTable
This method returns the double value in the table at a specified row/column location.

Specified by:
getDoubleCell in interface DataTable
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

public double getDoubleCell(int row,
                            int column)
                     throws NumberFormatException
Description copied from interface: DataTable
This method returns the double value in the table at a specified row/column location.

Specified by:
getDoubleCell in interface DataTable
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

public double getDoubleCell(String row,
                            String column)
                     throws NumberFormatException
Description copied from interface: DataTable
This method returns the double value in the table at a specified row/column location.

Specified by:
getDoubleCell in interface DataTable
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

public double getDoubleCell(String row,
                            int column)
                     throws NumberFormatException
Description copied from interface: DataTable
This method returns the double value in the table at a specified row/column location.

Specified by:
getDoubleCell in interface DataTable
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

public DataTable.Row getRow(int row)
Description copied from interface: DataTable
This method returns a map that represents the desired row (indicated by the primary key).

Specified by:
getRow in interface DataTable
Parameters:
row - index of row to retrieve (starting from zero)
Returns:
the Map representing the desired row.

getRow

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

Specified by:
getRow in interface DataTable
Parameters:
row - primary key of row to retrieve
Returns:
the Map representing the desired row.

setCell

public void setCell(int row,
                    String column,
                    String value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a String.

Specified by:
setCell in interface DataTable
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

public void setCell(int row,
                    String column,
                    int value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as an int.

Specified by:
setCell in interface DataTable
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

public void setCell(int row,
                    String column,
                    double value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a double.

Specified by:
setCell in interface DataTable
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

public void setCell(int row,
                    int column,
                    String value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a String.

Specified by:
setCell in interface DataTable
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

public void setCell(int row,
                    int column,
                    int value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as an int.

Specified by:
setCell in interface DataTable
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

public void setCell(int row,
                    int column,
                    double value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a double.

Specified by:
setCell in interface DataTable
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

public void setCell(String row,
                    String column,
                    String value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a String.

Specified by:
setCell in interface DataTable
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

public void setCell(String row,
                    String column,
                    int value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as an int.

Specified by:
setCell in interface DataTable
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

public void setCell(String row,
                    String column,
                    double value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a double.

Specified by:
setCell in interface DataTable
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

public void setCell(String row,
                    int column,
                    String value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a String.

Specified by:
setCell in interface DataTable
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

public void setCell(String row,
                    int column,
                    int value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as an int.

Specified by:
setCell in interface DataTable
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

public void setCell(String row,
                    int column,
                    double value)
Description copied from interface: DataTable
This method allows you to update a desired row and column with new data as a double.

Specified by:
setCell in interface DataTable
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

public DataTable.Row removeRow(int row)
Description copied from interface: DataTable
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.

Specified by:
removeRow in interface DataTable
Parameters:
row - table row to be deleted.
Returns:
the Map representing the Row that was removed from the table.

removeRow

public DataTable.Row removeRow(String row)
Description copied from interface: DataTable
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.

Specified by:
removeRow in interface DataTable
Parameters:
row - table row to be deleted.
Returns:
the Map representing the Row that was removed from the table.

addColumn

public void addColumn(String columnName)
Description copied from interface: DataTable
This method adds a column to the table. For each row in the table, the column's value will be null.

Specified by:
addColumn in interface DataTable
Parameters:
columnName - the name of the column to be added.

addColumn

public void addColumn(String columnName,
                      String defaultValue)
Description copied from interface: DataTable
This method adds a column to the table, setting this column to the specified default value in every row.

Specified by:
addColumn in interface DataTable
Parameters:
columnName - the name of the column to be added.
defaultValue - the default value to use for the column.

addRow

public void addRow(DataTable.Row row)
Description copied from interface: DataTable
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.

Specified by:
addRow in interface DataTable
Parameters:
row - The row to add

setPrimaryKey

public void setPrimaryKey(String key)
Description copied from interface: DataTable
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.

Specified by:
setPrimaryKey in interface DataTable
Parameters:
key - the column name to be set as the primary key.

getPrimaryKey

public String getPrimaryKey()
Description copied from interface: DataTable
This method returns the column name that acts as the primary key for the table.

Specified by:
getPrimaryKey in interface DataTable
Returns:
the column name that acts as the primary key. The default is the row number.

getColumnNames

public ArrayList<String> getColumnNames()
Description copied from interface: DataTable
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.

Specified by:
getColumnNames in interface DataTable
Returns:
an array list with a reporting of all of the column names in the table.

setColumnNames

public void setColumnNames(ArrayList<String> columns)
Description copied from interface: DataTable
This method sets an arbitrary name for each of the columns in the table.

Specified by:
setColumnNames in interface DataTable
Parameters:
columns - names of columns to be set in the table.

remapColumnNames

public void remapColumnNames(Map<String,String> columnMap)
Description copied from interface: DataTable
This method replaces each column name in the table with a matching key in columnMap with the value of the mapping.

Specified by:
remapColumnNames in interface DataTable
Parameters:
columnMap - names of columns to be replaced with the associated value to replace it with.

getAllRows

public ArrayList<DataTable.Row> getAllRows()
Description copied from interface: DataTable
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.

Specified by:
getAllRows in interface DataTable
Returns:
an array list representing the data table.

getUrl

public String getUrl()

copyRowsFrom

public void copyRowsFrom(DataTable dataSet)
Description copied from interface: DataTable
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.

Specified by:
copyRowsFrom in interface DataTable
Parameters:
dataSet - a DataTable object to copy into the current DataTable.

count

public int count(String column,
                 String value)
Description copied from interface: DataTable
This method counts the number of rows in the specified column that have the value given by the value parameter.

Specified by:
count in interface DataTable
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

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

Specified by:
avg in interface DataTable
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

public double max(String column)
           throws NumberFormatException
Description copied from interface: DataTable
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.

Specified by:
max in interface DataTable
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

public double min(String column)
           throws NumberFormatException
Description copied from interface: DataTable
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.

Specified by:
min in interface DataTable
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

public void save(String filename)
This method saves the CSVDataTable in a CSV file format with the first row the column names and the remaining rows the data in the table. The delimiter for the CSV is determined either the value that was used in creating the data table or the value that is set with the setDelimiter() method.

Specified by:
save in interface DataTable
Parameters:
filename - the path to the file to save the CSVDataTable to.
Throws:
IOException - when there is a problem writing to the file.

setDelimiter

public void setDelimiter(char delimiter)
Set the delimiter used by this data set, which will affect how this object will be written to a file in the future.

Parameters:
delimiter - The separator between fields on the same line (e.g., a comma, tab, colon, etc.).

getDelimiter

public char getDelimiter()
Get the delimiter used by this data set.

Returns:
the current delimiter

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

Copyright © 2009 Virginia Tech.