Use this class to store database-like data. This class uses rows and columns
to organize its data. It has some convenience methods that allow fast loading
and retrieval of the data into and out of string arrays. It is also handy for
reading CSV files.
WARNING: the class assumes that column names are unique, but does not enforce this.
addColumnValue
public void addColumnValue(String column,
Object value)
Adds a value into the Data set at the current row, using a column name to
find the column in which to insert the new value.
column
- the name of the column to set.value
- value to set into column.
addHeader
public void addHeader(String s)
Adds a header name to the Data object.
addRow
public void addRow()
append
public void append(Data d)
Adds the rows of the given Data object to this Data object.
d
- data object to be appended to this one
findValue
public int findValue(String column,
Object value)
Returns the row number where a certain value is.
column
- column to be searched for value.value
- object in Search of.
- row # where value exists.
getColumn
public String[] getColumn(String columnName)
This method will retrieve every entry in a certain column. It returns an
array of strings from the column. Even if the data are not strings, they
will be returned as strings in this method.
columnName
- name of the column.
- array of Strings representing the data.
getColumnAsObjectArray
public List getColumnAsObjectArray(String columnName)
This method will retrieve every entry in a certain column. It returns an
array of Objects from the column.
columnName
- name of the column.
- array of Objects representing the data.
getColumnValue
public Object getColumnValue(String column)
Gets the value in the current row of the given column.
column
- name of the column.
- an Object which holds the value of the column.
getColumnValue
public Object getColumnValue(int column)
Gets the value in the current row of the given column.
column
- index of the column (starts at 0).
- an Object which holds the value of the column.
getColumnValue
public Object getColumnValue(int column,
int row)
getCurrentPos
public int getCurrentPos()
Get the number of the current row.
- integer representing the current row
getDataAsText
public String[] getDataAsText()
Returns all the data in the Data set as an array of strings. Each array
gives a row of data, each column separated by tabs.
getDataFromResultSet
public static Data getDataFromResultSet(ResultSet rs)
throws SQLException
Gets a Data object from a ResultSet.
rs
- ResultSet passed in from a database query
getHeaderCount
public int getHeaderCount()
getHeaders
public String[] getHeaders()
Returns a String array of the column headers.
- array of strings of the column headers.
hasHeader
public boolean hasHeader(String column)
Checks to see if a column exists in the Data object.
column
- Name of column header to check for.
- True or False depending on whether the column exists.
next
public boolean next()
Sets the current position of the Data set to the next row.
- True if there is another row. False if there are no more rows.
previous
public boolean previous()
Sets the current position of the Data set to the previous row.
- True if there is another row. False if there are no more rows.
removeColumn
public void removeColumn(int col)
removeRow
public void removeRow()
Removes the current row.
removeRow
public void removeRow(int index)
replaceHeader
public void replaceHeader(String oldHeader,
String newHeader)
Replaces the given header name with a new header name.
oldHeader
- Old header name.newHeader
- New header name.
reset
public void reset()
Resets the current position of the data set to just before the first
element.
setColumnData
public void setColumnData(String colName,
Object value)
Sets the data for every row in the column.
setColumnData
public void setColumnData(int col,
List data)
setColumnValue
public void setColumnValue(String column,
Object value)
Sets the value in the Data set at the current row, using a column name to
find the column in which to insert the new value.
column
- the name of the column to set.value
- value to set into column.
setCurrentPos
public void setCurrentPos(int r)
Sets the current pos. If value sent to method is not a valid number, the
current position is set to one higher than the maximum.
setData
public void setData(String[] contents,
String delimiter)
Use this method to set the entire data set. It takes an array of strings.
It uses the first row as the headers, and the next rows as the data
elements. Delimiter represents the delimiting character(s) that separate
each item in a data row.
contents
- array of strings, the first element is a list of the column
headers, the next elements each represent a single row of
data.delimiter
- the delimiter character that separates columns within the
string array.
setHeaders
public void setHeaders(String[] h)
Sets the headers for the data set. Each header represents a column of
data. Each row's data can be gotten with the column header name, which
will always be a string.
h
- array of strings representing the column headers.
these must be distinct - duplicates will cause incorrect behaviour
setLine
public void setLine(String[] line)
Sets a row of data using an array of strings as input. Each value in the
array represents a column's value in that row. Assumes the order will be
the same order in which the headers were added to the data set.
line
- array of strings representing column values.
setLine
public void setLine(String[] line,
String deflt)
Sets a row of data using an array of strings as input. Each value in the
array represents a column's value in that row. Assumes the order will be
the same order in which the headers were added to the data set.
line
- array of strings representing column values.deflt
- default value to be placed in data if line is not as long as
headers.
size
public int size()
Gets the number of rows in the Data object.
- number of rows in Data object.
sort
public void sort(String column,
boolean asc)
Sorts the data using a given row as the sorting criteria. A boolean value
indicates whether to sort ascending or descending.
column
- name of column to use as sorting criteria.asc
- boolean value indicating whether to sort ascending or
descending. True for asc, false for desc. Currently this
feature is not enabled and all sorts are asc.
toString
public String toString()