public interface DataTable extends Iterable<DataTable.Row>
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:
At the end of each method, the getRow* methods can reference a row by its primary key on the next instruction.
The table will have data from one data source at a time. In other words, setting the dataset to another source will wipe out the previously held data in the DataTable.
Modifier and Type | Interface and Description |
---|---|
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.
|
Modifier and Type | Method and Description |
---|---|
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.
|
List<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.
|
List<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(List<String> columnNames)
This method sets an arbitrary name for each of the columns
in the table.
|
void |
setColumnNames(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.
|
forEach, iterator, spliterator
int rowCount()
int colCount()
String getCell(int row, String column)
row
- index of row to retrieve (starting at zero)column
- the name of the desired columnString getCell(int row, int column)
row
- index of row to retrieve (starting at zero)column
- index of column to retrieve (starting at zero)String getCell(String row, String column)
row
- primary key of rowcolumn
- the name of the desired columnString getCell(String row, int column)
row
- primary key of rowcolumn
- index of column to retrieve (starting at zero)int getIntCell(int row, String column) throws NumberFormatException
row
- index of row to retrieve (starting at zero)column
- the name of the desired columnNumberFormatException
- if the cell does not contain an int
value.int getIntCell(int row, int column) throws NumberFormatException
row
- index of row to retrieve (starting at zero)column
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain an int
value.int getIntCell(String row, String column) throws NumberFormatException
row
- primary key of rowcolumn
- the name of the desired columnNumberFormatException
- if the cell does not contain an int
value.int getIntCell(String row, int column) throws NumberFormatException
row
- primary key of rowcolumn
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain an int
value.double getDoubleCell(int row, String column) throws NumberFormatException
row
- index of row to retrieve (starting at zero)column
- the name of the desired columnNumberFormatException
- if the cell does not contain a double
value.double getDoubleCell(int row, int column) throws NumberFormatException
row
- index of row to retrieve (starting at zero)column
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain a double
value.double getDoubleCell(String row, String column) throws NumberFormatException
row
- primary key of rowcolumn
- the name of the desired columnNumberFormatException
- if the cell does not contain a double
value.double getDoubleCell(String row, int column) throws NumberFormatException
row
- primary key of rowcolumn
- index of column to retrieve (starting at zero)NumberFormatException
- if the cell does not contain a double
value.DataTable.Row getRow(int row)
row
- index of row to retrieve (starting from zero)DataTable.Row getRow(String row)
row
- primary key of row to retrievevoid setCell(int row, String column, String value)
row
- index of row to change (starting from zero)column
- name of the column to changevalue
- the value that is to replace the current valuevoid setCell(int row, String column, int value)
row
- index of row to change (starting from zero)column
- name of the column to changevalue
- the value that is to replace the current valuevoid setCell(int row, String column, double value)
row
- index of row to change (starting from zero)column
- name of the column to changevalue
- the value that is to replace the current valuevoid setCell(int row, int column, String value)
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 valuevoid setCell(int row, int column, int value)
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 valuevoid setCell(int row, int column, double value)
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 valuevoid setCell(String row, String column, String value)
row
- primary key of the row to changecolumn
- name of the column to changevalue
- the value that is to replace the current valuevoid setCell(String row, String column, int value)
row
- primary key of the row to changecolumn
- name of the column to changevalue
- the value that is to replace the current valuevoid setCell(String row, String column, double value)
row
- primary key of the row to changecolumn
- name of the column to changevalue
- the value that is to replace the current valuevoid setCell(String row, int column, String value)
row
- primary key of the row to changecolumn
- index of column to change (starting from zero)value
- the value that is to replace the current valuevoid setCell(String row, int column, int value)
row
- primary key of the row to changecolumn
- index of column to change (starting from zero)value
- the value that is to replace the current valuevoid setCell(String row, int column, double value)
row
- primary key of the row to changecolumn
- index of column to change (starting from zero)value
- the value that is to replace the current valueDataTable.Row removeRow(int row)
row
- table row to be deleted.DataTable.Row removeRow(String row)
row
- table row to be deleted.void addColumn(String columnName)
columnName
- the name of the column to be added.void addColumn(String columnName, String defaultValue)
columnName
- the name of the column to be added.defaultValue
- the default value to use for the column.void addRow(DataTable.Row row)
row
- The row to addvoid setPrimaryKey(String key)
key
- the column name to be set as the primary key.String getPrimaryKey()
List<String> getColumnNames()
void setColumnNames(List<String> columnNames)
columnNames
- names of columns to be set in the table.void setColumnNames(String... columnNames)
columnNames
- names of columns to be set in the table.void remapColumnNames(Map<String,String> columnMap)
columnMap
- names of columns to be replaced with the
associated value to replace it with.List<DataTable.Row> getAllRows()
void copyRowsFrom(DataTable dataSet)
dataSet
- a DataTable object to copy into the current DataTable.int count(String column, String value)
column
- the desired column to count on.value
- the desired value to count.double avg(String column) throws NumberFormatException
column
- the column to compute the average of.NumberFormatException
- if the column has a string that cannot
be parsed as a number.double max(String column) throws NumberFormatException
column
- the column to compute the max number of.NumberFormatException
- if the column has a string that cannot
be parsed as a number.double min(String column) throws NumberFormatException
column
- the column to compute the min number of.NumberFormatException
- if the column has a string that cannot
be parsed as a number.void save(String filename)
filename
- the path to the file that is to be written.