de.rwth.utils
Class Table

java.lang.Object
  |
  +--de.rwth.utils.Table

public class Table
extends java.lang.Object

A class for creating tables consisting of rows and columns. Each column has a field name associated with it and each row has a key name associated with it. The resulting tables can be converted to a String which is suitable for CSV (comma separated values) files.

Version:
$Id: Table.java,v 1.3 2002/09/17 06:53:53 mohnen Exp $
Author:
Markus Mohnen

Field Summary
protected  char columnsep
          The column separator char for the conversion to String.
protected  java.util.LinkedList fieldnames
          The list of field names.
protected  java.lang.String keyname
          The field name of the key column.
protected  java.util.HashMap keys
          Stores the rows of the table.
protected  char quotechar
          The quote char for the conversion to String.
protected  char recordsep
          The record separator char for the conversion to String.
 
Constructor Summary
Table(java.lang.String keyname)
          Creates a new Table instance with default values for record separator (\n), column separator (,), and quote character (").
 
Method Summary
 void addNewFieldName(java.lang.String fieldname)
          Creates a new column in this table.
 void createNewKey(java.lang.String key)
          Creates a new row in this table.
 java.lang.Object getFieldForKey(java.lang.String fieldname, java.lang.String key)
          Gets the value of a cell for a specific row/column.
 boolean hasFieldName(java.lang.String fieldname)
          Checks if a String is a valid field name in this table.
static void main(java.lang.String[] args)
          Mini test environment.
protected  java.lang.String maybequote(java.lang.Object o)
          Returns the maybe quoted String representation of its argument.
 void setFieldForKey(java.lang.String fieldname, java.lang.String key, java.lang.Object value)
          Sets the value of a cell for a specific row/column.
 java.lang.String toString()
          Creates a String representation of this table.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

keyname

protected java.lang.String keyname
The field name of the key column.

fieldnames

protected java.util.LinkedList fieldnames
The list of field names.

keys

protected java.util.HashMap keys
Stores the rows of the table. Each row hashes the key name of the row to a HashMap of column entries, which in turn hash a field name to the value in the cell.

recordsep

protected char recordsep
The record separator char for the conversion to String.

columnsep

protected char columnsep
The column separator char for the conversion to String.

quotechar

protected char quotechar
The quote char for the conversion to String.
Constructor Detail

Table

public Table(java.lang.String keyname)
Creates a new Table instance with default values for record separator (\n), column separator (,), and quote character (").
Parameters:
keyname - a String value with the name of the key column.
Method Detail

hasFieldName

public boolean hasFieldName(java.lang.String fieldname)
Checks if a String is a valid field name in this table.
Parameters:
fieldname - a String value: The name to check.
Returns:
a boolean value: true if and only if the name was the argument to the constructor of this table, or the argument of one of the subsequent calls to addNewFieldName.

addNewFieldName

public void addNewFieldName(java.lang.String fieldname)
                     throws java.lang.IllegalArgumentException
Creates a new column in this table.
Parameters:
fieldname - a String value
Throws:
java.lang.IllegalArgumentException - if the fieldname is already one of the field names or the name of the key column.

createNewKey

public void createNewKey(java.lang.String key)
                  throws java.lang.IllegalArgumentException
Creates a new row in this table.
Parameters:
key - a String value
Throws:
java.lang.IllegalArgumentException - if there is already a row with key.

setFieldForKey

public void setFieldForKey(java.lang.String fieldname,
                           java.lang.String key,
                           java.lang.Object value)
                    throws java.lang.IllegalArgumentException
Sets the value of a cell for a specific row/column.
Parameters:
fieldname - a String value: The column of the cell.
key - a String value: The row of the cell.
value - an Object value
Throws:
java.lang.IllegalArgumentException - if either row or column do not exist or if there is already a value for this cell.

getFieldForKey

public java.lang.Object getFieldForKey(java.lang.String fieldname,
                                       java.lang.String key)
Gets the value of a cell for a specific row/column.
Parameters:
fieldname - a String value: The column of the cell.
key - a String value: The row of the cell.
Returns:
an Object value: The value
Throws:
java.lang.IllegalArgumentException - if either row or column do not exist or if there is no value associated with this cell.

maybequote

protected java.lang.String maybequote(java.lang.Object o)
Returns the maybe quoted String representation of its argument.
Parameters:
o - an Object value
Returns:
a String value

toString

public java.lang.String toString()
Creates a String representation of this table. Rows are separated with the record separator and columns with a column separator. The first row contains the field names. In each row, the first entry is the key of the row, followed by string representations of the values obtained by toString(). The order of rows and keys is undetermined. If the string representation of a value contains the column separator or blanks at the beginning or end, it is enclosed in quotes and quotes inside are doubled.
Overrides:
toString in class java.lang.Object
Returns:
a String value

main

public static void main(java.lang.String[] args)
Mini test environment.
Parameters:
args - a String[] value