O2Plib.table
Class SimpleTable

java.lang.Object
  extended by O2Plib.table.SimpleTable
Direct Known Subclasses:
Table

public class SimpleTable
extends java.lang.Object

Class SimpleTable is the base class for tables.

The SimpleTable class creates an empty table of size tRows and tCols counting from 0. It is up to the caller to populate the table. Data is stored in tData[tRows][tCols] with tFields[tCols]. It contains several methods for accessing data in the table.

List of Table access methods
 SimpleTable() - constructor for creating empty table
 SimpleTable() - constructor for creating a table populated w/fields & data
 freeTable() - free Table tData[][] and tFields so can garbage collect it
 trimWhitespace() - trim Table tData[][] and tFields[] data
 setFieldData() - set new field[] list for table
 getRowData() - set new row data[] for specific row in the table
 setRowData() - set new row data[] for specific row in the table
 lookupFieldIdx() - lookup column index of field if exists
 lookupFieldPrefixIdx() - lookup column index of field if exists by prefix
 lookupRowByFieldNameValue() - lookup the row of data by field(name,value).
 lookupRowByFieldNameValuePairs() - lookup row of data-pair matches.
 getListOfRowsIdxByFieldNameValuePairs() - get list of rows of data-pair matches.
 getListOfRowsDataByFieldNameValuePairs() - get list of rows data of data-pair matches.
 getFieldRowDatum() - get field in row by Table row index
 getFieldIdxRowDatum() - get data by field idx with row index
 getRowDataByFieldNameValue() - get row of data by field(name,value).
 setFieldRowDatum() - set datum by field name with row index 
 setFieldIdxRowDatum() - set datum by field idx with row index
 appendRowToTable() - append row of data to table. 
 removeRowFromTable() - remove row of data from table.
 deleteRowsFromTable() - delete rows from the table and resize the table.
 addColumnToTable() - add new column to table with specified field name.
 cvtTblToDelimData() - convert the table to a delimited string table.
 cvtTblToHTML() - convert the table to a HTML string
 cvtTblToXML() - convert the table to a XML string.

This work was produced by Peter Lemkin of the National Cancer Institute, an agency of the United States Government. As a work of the United States Government there is no associated copyright. It is offered as open source software under the Mozilla Public License (version 1.1) subject to the limitations noted in the accompanying LEGAL file. This notice must be included with the code. The MAExplorer Mozilla and Legal files are available on http://maexplorer.sourceforge.net/.

Version:
$Date: 2005/10/21 16:33:51 $ $Revision: 1.15 $
Author:
P. Lemkin (NCI), G. Thornwall (SAIC), NCI-Frederick, Frederick, MD
See Also:
MAExplorer Home, Table

Field Summary
static boolean dbugFlag
          Debugging flag
 java.lang.String fullFileName
          Optional full file name associated with table
 java.lang.String name
          Optional table name associated with table
 int tCols
          # of columns/row in the table
 java.lang.String[][] tData
          row vectors of data [0:tRows-1][0:tCols-1]
 java.lang.String[] tFields
          names of table fields
 java.lang.String[] tHelp
          names of table help data if any for defining the fields
 java.lang.String title
          Optional title of table
 int tRows
          # of rows/Table
 java.lang.String tType
          Type of table
 
Constructor Summary
SimpleTable()
          SimpleTable() - constructor for creating empty table.
SimpleTable(java.lang.String title, java.lang.String name, int tRows, int tCols)
          SimpleTable() - constructor for creating empty table.
SimpleTable(java.lang.String title, java.lang.String name, int tRows, int tCols, java.lang.String[] tFields, java.lang.String[][] tData)
          SimpleTable() - constructor for creating a table popupated with fields and data.
SimpleTable(java.lang.String title, java.lang.String name, java.lang.String type)
           
SimpleTable(java.lang.String title, java.lang.String name, java.lang.String[] tFields, int tRows, int tCols)
          SimpleTable() - constructor for creating empty table.
 
Method Summary
 boolean addColumnsToTable(java.lang.String[] newFieldNames)
          addColumnsToTable() - add new columns to the table with the specified field names list.
 boolean addColumnToTable(java.lang.String newFieldName, java.lang.String[] colData)
          addColumnToTable() - add a new column to the table with the specified field name.
 boolean appendRowToTable(java.lang.String[] accRowData)
          appendRowToTable() - append row of data to table.
 java.lang.String cvtTblToDelimData(java.lang.String delim)
          cvtTblToDelimData() - convert the table to a delimited string table where delim could be tab or comma or some other character or substring.
 java.lang.String cvtTblToHTML(java.lang.String beginTitle, int fontSize, java.lang.String[] dataPreface, java.lang.String[] dataEpilogue, java.lang.String endTitle)
          cvtTblToHTML() - convert the table to a HTML string
 java.lang.String cvtTblToXML(java.lang.String topObj, java.lang.String dateStr, java.lang.String dbName, java.lang.String entryObj)
          cvtTblToXML() - convert the table to a XML string.
 java.lang.String cvtTblToXML(java.lang.String topObj, java.lang.String dateStr, java.lang.String dbName, java.lang.String entryObj, java.lang.String dtdFile)
          cvtTblToXML() - convert the table to a XML string.
 boolean deleteRowsFromTable(int[] rowsToDelete)
          deleteRowsFromTable() - delete rows from the table and resize the table.
 void freeTable()
          freeTable() - free Table tData[][] and tFields so can garbage collect it.
 java.lang.String getFieldIdxRowDatum(int row, int fieldIdx)
          getFieldIdxRowDatum() - get datum by field idx with row index
 java.lang.String getFieldRowDatum(int row, java.lang.String fieldName)
          getFieldRowDatum() - get datum by field in row by Table row index
 java.lang.String[][] getListOfRowsDataByFieldNameValuePairs(java.lang.String fieldName1, java.lang.String fieldValue1, java.lang.String fieldName2, java.lang.String fieldValue2)
          getListOfRowsDataByFieldNameValuePairs() - lookup a list of rows data by (fieldName1,fieldValue1) and (fieldName2,fieldValue2) matches.
 int[] getListOfRowsIdxByFieldNameValuePairs(java.lang.String fieldName1, java.lang.String fieldValue1, java.lang.String fieldName2, java.lang.String fieldValue2)
          getListOfRowsIdxByFieldNameValuePairs() - lookup a list of rows of data by (fieldName1,fieldValue1) and (fieldName2,fieldValue2) matches.
 java.lang.String[] getRowData(int rowNbr)
          getRowData() - get row of data[] for specific row in the table
 java.lang.String[] getRowDataByFieldNameValue(java.lang.String fieldName, java.lang.String fieldValue)
          getRowDataByFieldNameValue() - get row of data by (fieldName,fieldValue).
 int lookupFieldIdx(java.lang.String fieldName)
          lookupFieldIdx() - lookup column index of field if exists.
 int lookupFieldPrefixIdx(java.lang.String fieldNamePrefix)
          lookupFieldPrefixIdx() - lookup column index of fieldNamePrefix if exists in the table.
 int lookupRowByFieldNameValue(java.lang.String fieldName, java.lang.String fieldValue)
          lookupRowByFieldNameValue() - lookup the row of data by (fieldName,fieldValue).
 int lookupRowByFieldNameValuePairs(java.lang.String fieldName1, java.lang.String fieldValue1, java.lang.String fieldName2, java.lang.String fieldValue2)
          lookupRowByFieldNameValuePairs() - lookup the row of data by (fieldName1,fieldValue1) and (fieldName2,fieldValue2) matches.
 boolean removeRowFromTable(int rowToDelete)
          removeRowFromTable() - remove row of data from table.
 boolean replaceColumnData(int colIdx, java.lang.String colValue)
          replaceColumnData() - replace all data in a column in the table with the same value.
 boolean replaceColumnData(int colIdx, java.lang.String[] colData)
          replaceColumnData() - replace data in a column in the table.
 boolean replaceColumnData(java.lang.String fieldName, java.lang.String colValue)
          replaceColumnData() - replace all data in a column in the table with the same value.
 boolean replaceColumnData(java.lang.String fieldName, java.lang.String[] colData)
          replaceColumnData() - replace data in a column in the table.
 boolean setFieldData(java.lang.String[] newFields, int nNewFields)
          setFieldData() - set new field[] list for table
 boolean setFieldIdxRowDatum(int row, int fieldIdx, java.lang.String datum)
          setFieldIdxRowDatum() - set datum by field idx with row index
 boolean setFieldRowDatum(int row, java.lang.String fieldName, java.lang.String datum)
          setFieldRowDatum() - set datum by field name with row index
 boolean setHelpData(java.lang.String[] newHelpData)
          setHelpData() - set tHelp[] list for table
 boolean setRowData(java.lang.String[] newRowData, int rowNbr)
          setRowData() - set new row data[] for specific row in the table where newRowData[] must be the size of the table rows.
 void trimWhitespace()
          trimWhitespace() - trim Table tData[][] and tFields[] data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dbugFlag

public static boolean dbugFlag
Debugging flag


tCols

public int tCols
# of columns/row in the table


tRows

public int tRows
# of rows/Table


tFields

public java.lang.String[] tFields
names of table fields


tData

public java.lang.String[][] tData
row vectors of data [0:tRows-1][0:tCols-1]


title

public java.lang.String title
Optional title of table


name

public java.lang.String name
Optional table name associated with table


fullFileName

public java.lang.String fullFileName
Optional full file name associated with table


tHelp

public java.lang.String[] tHelp
names of table help data if any for defining the fields


tType

public java.lang.String tType
Type of table

Constructor Detail

SimpleTable

public SimpleTable()
SimpleTable() - constructor for creating empty table.

Parameters:
tRows - is # of rows of table
tCols - is # of columns of table

SimpleTable

public SimpleTable(java.lang.String title,
                   java.lang.String name,
                   java.lang.String type)

SimpleTable

public SimpleTable(java.lang.String title,
                   java.lang.String name,
                   int tRows,
                   int tCols)
SimpleTable() - constructor for creating empty table. The table is similar to a spreadsheet of size (tRows,tCols). If tCols > 0, then tFields[] is allocated. If tCols > 0 AND tRows> 0, then tFields[] is allocated.

Parameters:
title - opt. title of the table
name - opt. name assoc. w/table
tRows - # of rows in table
tCols - # of Field columns in table.

SimpleTable

public SimpleTable(java.lang.String title,
                   java.lang.String name,
                   java.lang.String[] tFields,
                   int tRows,
                   int tCols)
SimpleTable() - constructor for creating empty table. The table is similar to a spreadsheet of size (tRows,tCols). If tCols > 0, then tFields[] is allocated. If tCols > 0 AND tRows> 0, then tData[] is allocated.

Parameters:
title - opt. title of the table
tFields - is list of field names [0:tCols-1]
tRows - # of rows in table
tCols - # of Field columns in table.

SimpleTable

public SimpleTable(java.lang.String title,
                   java.lang.String name,
                   int tRows,
                   int tCols,
                   java.lang.String[] tFields,
                   java.lang.String[][] tData)
SimpleTable() - constructor for creating a table popupated with fields and data. The table is similar to a spreadsheet of size (tRows,tCols). If tCols > 0, then tFields[] is allocated. If tCols > 0 AND tRows> 0, then tFields[] is allocated.

Parameters:
title - opt. title of the table
name - opt. name assoc. w/table
tRows - # of rows in table
tCols - # of Field columns in table.
tFields - field names of the table
tData - rows of data
Method Detail

freeTable

public void freeTable()
freeTable() - free Table tData[][] and tFields so can garbage collect it. Also zero tRows, tCols


trimWhitespace

public void trimWhitespace()
trimWhitespace() - trim Table tData[][] and tFields[] data. This removes leading and trailing white space.


setFieldData

public boolean setFieldData(java.lang.String[] newFields,
                            int nNewFields)
setFieldData() - set new field[] list for table

Parameters:
newFields - is list of new fields
Returns:
nNewFields is # of new fields to use

setHelpData

public boolean setHelpData(java.lang.String[] newHelpData)
setHelpData() - set tHelp[] list for table

Parameters:
newHelpData - is list of new help data
Returns:
true if succeed

getRowData

public java.lang.String[] getRowData(int rowNbr)
getRowData() - get row of data[] for specific row in the table

Parameters:
rowNbr - is the row to get
Returns:
row of data if succeed, null otherwise

setRowData

public boolean setRowData(java.lang.String[] newRowData,
                          int rowNbr)
setRowData() - set new row data[] for specific row in the table where newRowData[] must be the size of the table rows.

Parameters:
newRowData - is list of new data for row rowNbr
rowNbr - is the row to change
Returns:
true if succeed

lookupFieldIdx

public int lookupFieldIdx(java.lang.String fieldName)
lookupFieldIdx() - lookup column index of field if exists.

Parameters:
fieldName - to lookup
Returns:
index if found, else if it does not exist return -1

lookupFieldPrefixIdx

public int lookupFieldPrefixIdx(java.lang.String fieldNamePrefix)
lookupFieldPrefixIdx() - lookup column index of fieldNamePrefix if exists in the table. The first part of the field name is tested.

Parameters:
fieldNamePrefix - to lookup is unique start of the field
Returns:
index if found, else if it does not exist return -1

lookupRowByFieldNameValue

public int lookupRowByFieldNameValue(java.lang.String fieldName,
                                     java.lang.String fieldValue)
lookupRowByFieldNameValue() - lookup the row of data by (fieldName,fieldValue).

Parameters:
fieldName - is the name of the field to check
fieldValue - is value to look for in fieldName field
Returns:
row index of data if found, else -1
See Also:
lookupFieldIdx(java.lang.String)

lookupRowByFieldNameValuePairs

public int lookupRowByFieldNameValuePairs(java.lang.String fieldName1,
                                          java.lang.String fieldValue1,
                                          java.lang.String fieldName2,
                                          java.lang.String fieldValue2)
lookupRowByFieldNameValuePairs() - lookup the row of data by (fieldName1,fieldValue1) and (fieldName2,fieldValue2) matches.

Parameters:
fieldName1 - is the name of the first field to check
fieldValue1 - is value to look for in first fieldName field
fieldName2 - is the name of the 2nd field to check
fieldValue2 - is value to look for in 2nd fieldName field
Returns:
row index of data if found, else -1
See Also:
lookupFieldIdx(java.lang.String)

getListOfRowsIdxByFieldNameValuePairs

public int[] getListOfRowsIdxByFieldNameValuePairs(java.lang.String fieldName1,
                                                   java.lang.String fieldValue1,
                                                   java.lang.String fieldName2,
                                                   java.lang.String fieldValue2)
getListOfRowsIdxByFieldNameValuePairs() - lookup a list of rows of data by (fieldName1,fieldValue1) and (fieldName2,fieldValue2) matches.

Parameters:
fieldName1 - is the name of the first field to check
fieldValue1 - is value to look for in first fieldName field
fieldName2 - is the name of the 2nd field to check
fieldValue2 - is value to look for in 2nd fieldName field
Returns:
list of row indexes of data if found, else null
See Also:
lookupFieldIdx(java.lang.String)

getListOfRowsDataByFieldNameValuePairs

public java.lang.String[][] getListOfRowsDataByFieldNameValuePairs(java.lang.String fieldName1,
                                                                   java.lang.String fieldValue1,
                                                                   java.lang.String fieldName2,
                                                                   java.lang.String fieldValue2)
getListOfRowsDataByFieldNameValuePairs() - lookup a list of rows data by (fieldName1,fieldValue1) and (fieldName2,fieldValue2) matches.
Note: data is copy by value in the table - not by reference! This means you can edit either this data or the table later without changing this data.

Parameters:
fieldName1 - is the name of the first field to check
fieldValue1 - is value to look for in first fieldName field
fieldName2 - is the name of the 2nd field to check
fieldValue2 - is value to look for in 2nd fieldName field
Returns:
list of row data if found, else null
See Also:
lookupFieldIdx(java.lang.String)

getFieldRowDatum

public java.lang.String getFieldRowDatum(int row,
                                         java.lang.String fieldName)
getFieldRowDatum() - get datum by field in row by Table row index

Parameters:
row - is to use
fieldName - to lookup
Returns:
cell data if found, else null
See Also:
lookupFieldIdx(java.lang.String)

getFieldIdxRowDatum

public java.lang.String getFieldIdxRowDatum(int row,
                                            int fieldIdx)
getFieldIdxRowDatum() - get datum by field idx with row index

Parameters:
row - is row to use
fieldIdx - of field to to lookup
Returns:
data if found, else null

setFieldRowDatum

public boolean setFieldRowDatum(int row,
                                java.lang.String fieldName,
                                java.lang.String datum)
setFieldRowDatum() - set datum by field name with row index

Parameters:
row - is row to use
fieldName - of field to to lookup
datum - to store. If the datum is null, save a ""
Returns:
true if suceed

setFieldIdxRowDatum

public boolean setFieldIdxRowDatum(int row,
                                   int fieldIdx,
                                   java.lang.String datum)
setFieldIdxRowDatum() - set datum by field idx with row index

Parameters:
row - is row to use
fieldIdx - of field to to lookup
datum - to store. If the datum is null, save a ""
Returns:
true if suceed

getRowDataByFieldNameValue

public java.lang.String[] getRowDataByFieldNameValue(java.lang.String fieldName,
                                                     java.lang.String fieldValue)
getRowDataByFieldNameValue() - get row of data by (fieldName,fieldValue).

Parameters:
fieldName - is the name of the field to check
fieldValue - is value to look for in fieldName field
Returns:
row of data if found, else null
See Also:
lookupFieldIdx(java.lang.String)

appendRowToTable

public boolean appendRowToTable(java.lang.String[] accRowData)
appendRowToTable() - append row of data to table. Note: this works with an empty table where tRows==0 and tData is null.

Parameters:
accRowData - is rlow of data to append
Returns:
true if added row

removeRowFromTable

public boolean removeRowFromTable(int rowToDelete)
removeRowFromTable() - remove row of data from table. Note: this works with an empty table where tRows==0 and tData is null.

Parameters:
rowToDelete - (count from row 0)
Returns:
true if row was deleted

deleteRowsFromTable

public boolean deleteRowsFromTable(int[] rowsToDelete)
deleteRowsFromTable() - delete rows from the table and resize the table. This creates a new tData with all missing rows AND NULL rows removed.

Parameters:
rowsToDelete - if not null
Returns:
true if succeed

addColumnToTable

public boolean addColumnToTable(java.lang.String newFieldName,
                                java.lang.String[] colData)
addColumnToTable() - add a new column to the table with the specified field name. If there is column data specified, then add it to the table otherwise set null entries the entries to "".

Parameters:
newFieldName - the new field name. If the field name already exists, then do not add the field.
colData - is optional data for the new field. The size of the array must be [0:tRows-1]. If adding the data to an existing column, just replace the data.
Returns:
true if added column to table or column already existed, false if any problems

addColumnsToTable

public boolean addColumnsToTable(java.lang.String[] newFieldNames)
addColumnsToTable() - add new columns to the table with the specified field names list. If any field names already exist, then do not add those particular fields.

Parameters:
newFieldNames - is a list of new field names to add.
Returns:
true if added columns to table or columns already existed, false if any problems

replaceColumnData

public boolean replaceColumnData(java.lang.String fieldName,
                                 java.lang.String[] colData)
replaceColumnData() - replace data in a column in the table. Set null entries to "".

Parameters:
fieldName - is the field name for the column.
colData - is column data for the new field. The size of the array must be [0:tRows-1]. If adding the data to an existing column, just replace the data.
Returns:
true if replaced data in column

replaceColumnData

public boolean replaceColumnData(int colIdx,
                                 java.lang.String[] colData)
replaceColumnData() - replace data in a column in the table. Set null entries to "".

Parameters:
colIdx - is the field index for the column.
colData - is column data for the new field. The size of the array must be [0:tRows-1]. If adding the data to an existing column, just replace the data.
Returns:
true if replaced data in column

replaceColumnData

public boolean replaceColumnData(java.lang.String fieldName,
                                 java.lang.String colValue)
replaceColumnData() - replace all data in a column in the table with the same value. Set null entries to "".

Parameters:
fieldName - is the field name for the column.
colValue - is column data for the new field.
Returns:
true if replaced data in column

replaceColumnData

public boolean replaceColumnData(int colIdx,
                                 java.lang.String colValue)
replaceColumnData() - replace all data in a column in the table with the same value. Set null entries to "".

Parameters:
colIdx - is the field index for the column.
colValue - is column data for the new field.
Returns:
true if replaced data in column

cvtTblToDelimData

public java.lang.String cvtTblToDelimData(java.lang.String delim)
cvtTblToDelimData() - convert the table to a delimited string table where delim could be tab or comma or some other character or substring.

Parameters:
delim - is the delimiter string (or character)
Returns:
delimited string of the table, null if problem

cvtTblToHTML

public java.lang.String cvtTblToHTML(java.lang.String beginTitle,
                                     int fontSize,
                                     java.lang.String[] dataPreface,
                                     java.lang.String[] dataEpilogue,
                                     java.lang.String endTitle)
cvtTblToHTML() - convert the table to a HTML string

Parameters:
beginTitle - to use for the table
fontSize - is 0 (no change), +n to increase, -n to decrease
dataPreface - is either null, or is an array [0:tCols-1] of preface strings added before the corresponding data value.
dataEpilogue - is either null, or is an array [0:tCols-1] of preface strings added before the corresponding data value.
endTitle - to add at the end if it is not null.
Returns:
string of HTML data

cvtTblToXML

public java.lang.String cvtTblToXML(java.lang.String topObj,
                                    java.lang.String dateStr,
                                    java.lang.String dbName,
                                    java.lang.String entryObj,
                                    java.lang.String dtdFile)
cvtTblToXML() - convert the table to a XML string.

Parameters:
topObj - is the name of the top level object
dateStr - is the date created if not null
dbName - is the name of the database if not null
entryObj - is the name of a row entry object
dtdFile - is the DTD file (if not null, insert the DTD if it exists) to insert in the front of the generated XML file. Note that the DTD file must include the "< ?xml ...? >" prefix.
Returns:
string of XML data

cvtTblToXML

public java.lang.String cvtTblToXML(java.lang.String topObj,
                                    java.lang.String dateStr,
                                    java.lang.String dbName,
                                    java.lang.String entryObj)
cvtTblToXML() - convert the table to a XML string. Do NOT insert a DTD.

Parameters:
topObj - is the name of the top level object
dateStr - is the date created if not null
dbName - is the name of the database if not null
entryObj - is the name of a row entry object
Returns:
string of XML data