Package table

Class Table

All Implemented Interfaces:
Serializable, Cloneable, TableModel

public class Table extends AbstractTableModel implements Cloneable
Class for data tables.

This data table class is implemented as a subclass of AbstractTableModel so that it can be displayed directly in a JTable.

Since:
2006.09.16
See Also:
  • Field Details

    • NOHEADER

      public static final int NOHEADER
      read/write mode: no header present/do not write a header
      See Also:
    • NODATA

      public static final int NODATA
      read mode: only adapt types, but do not load data
      See Also:
    • NOEXTEND

      public static final int NOEXTEND
      read mode: do not add new columns (no extension)
      See Also:
    • NONULLS

      public static final int NONULLS
      read mode: do not allow null values
      See Also:
    • MARKED

      public static final int MARKED
      read/write mode: read/write only marked columns (non-negative)
      See Also:
    • WEIGHT

      public static final int WEIGHT
      read/write mode: tuple weight in last field (currently unused)
      See Also:
    • VERBOSE

      public static final int VERBOSE
      read/write mode: verbose message output (print tuple counter)
      See Also:
  • Constructor Details

    • Table

      public Table()
      Create a data table.
      Since:
      2007.04.13 (Christian Borgelt)
    • Table

      public Table(String name)
      Create a data table.
      Parameters:
      name - the name of the table
      Since:
      2006.09.16 (Christian Borgelt)
  • Method Details

    • clone

      public Object clone()
      Clone this table.

      The clone is not a fully deep copy, as it keeps the types and the data arrays of the columns. However, the structure managing the column organization (order, naming etc) is independent of the old table and thus it is possible to rename/reorder columns and to add new columns without affecting the original table.

      If a deeper copy of the table is needed, the table should first be cloned with this function (i.e. clone()) and then the clone deepened with cloneAllColumns(), cloneAllTypes() or cloneAllData().

      Overrides:
      clone in class Object
      Returns:
      a clone of this table
      Since:
      2006.10.04 (Christian Borgelt)
      See Also:
    • cloneDomains

      public Table cloneDomains()
      Clone the domain descriptions of this table.
      Returns:
      a clone of this table without the data
      Since:
      2007.05.15 (Christian Borgelt)
    • cloneColumn

      public void cloneColumn(int index)
      Clone a column's type and value array and replace originals.

      With this method the shallow copy of the table as returned by clone() can be deepened for particular columns, when changes to the type and the contents of a column become necessary and the state of the original column must be preserved.

      Parameters:
      index - the index of the column to clone
      Since:
      2006.10.06 (Christian Borgelt)
    • cloneAllColumns

      public void cloneAllColumns()
      Clone all columns' type and data arrays and replace originals.

      With this method the shallow copy of the table as returned by clone() can be fully deepened.

      Since:
      2006.10.06 (Christian Borgelt)
    • cloneAllTypes

      public void cloneAllTypes()
      Clone all columns' types and replace originals.

      With this method the shallow copy of the table as returned by clone() can be partially deepened.

      Since:
      2005.05.02 (Christian Borgelt)
    • cloneAllData

      public void cloneAllData()
      Clone all columns' types and replace originals.

      With this method the shallow copy of the table as returned by clone() can be partially deepened.

      Since:
      2005.05.02 (Christian Borgelt)
    • clear

      public void clear()
      Clear this table, that is, remove all rows.
      Since:
      2006.10.04 (Christian Borgelt)
    • getName

      public String getName()
      Get the name of the table.
      Returns:
      the name of the table
      Since:
      2006.10.04 (Christian Borgelt)
    • setName

      public void setName(String name)
      Set the name of the table.
      Parameters:
      name - the new name of the table
      Since:
      2006.10.04 (Christian Borgelt)
    • getRowCount

      public int getRowCount()
      Get the number of rows of the table.
      Specified by:
      getRowCount in interface TableModel
      Returns:
      the number of rows of the table
      Since:
      2006.10.04 (Christian Borgelt)
    • getColumnCount

      public int getColumnCount()
      Get the number of columns of the table.
      Specified by:
      getColumnCount in interface TableModel
      Returns:
      the number of columns of the table
      Since:
      2006.09.16 (Christian Borgelt)
    • getColumn

      public Column getColumn(int index)
      Get a column given its index.
      Parameters:
      index - the index of the column to get
      Returns:
      the column with the given index
      Since:
      2006.10.04 (Christian Borgelt)
    • getColumn

      public Column getColumn(String name)
      Get a column given its name.
      Parameters:
      name - the index of the column to get
      Returns:
      the column with the given name
      Since:
      2006.10.04 (Christian Borgelt)
    • getColumnName

      public String getColumnName(int col)
      Get the name of a column given its index.
      Specified by:
      getColumnName in interface TableModel
      Overrides:
      getColumnName in class AbstractTableModel
      Parameters:
      col - the index of the column
      Returns:
      the name of the column with the given index
      Since:
      2006.10.04 (Christian Borgelt)
    • getColumnIndex

      public int getColumnIndex(String name)
      Get the index of a column given its name.

      This function is equivalent to findColumn().

      Parameters:
      name - the name of the column
      Returns:
      the index of the column or -1 if there is no column with the given name
      Since:
      2006.10.04 (Christian Borgelt)
      See Also:
    • findColumn

      public int findColumn(String name)
      Get the index of a column given its name.

      This function is equivalent to getColumnIndex().

      Overrides:
      findColumn in class AbstractTableModel
      Parameters:
      name - the name of the column
      Returns:
      the index of the column or -1 if there is no column with the given name
      Since:
      2006.10.04 (Christian Borgelt)
      See Also:
    • getColumnType

      public ColType getColumnType(int col)
      Get the type of a column given its index.
      Parameters:
      col - the index of the column
      Returns:
      the type of the column with the given index
      Since:
      2007.06.07 (Christian Borgelt)
    • setColumnType

      public void setColumnType(int col, ColType type)
      Set the type of a column given its index.
      Parameters:
      col - the index of the column
      type - the type to set
      Since:
      2007.07.19 (Christian Borgelt)
    • getColumnClass

      public Class<?> getColumnClass(int col)
      Get the value class of a column given its index.
      Specified by:
      getColumnClass in interface TableModel
      Overrides:
      getColumnClass in class AbstractTableModel
      Parameters:
      col - the index of the column
      Returns:
      the value class of the column with the given index
      Since:
      2007.03.18 (Christian Borgelt)
    • getColumnDir

      public int getColumnDir(int col)
      Get the direction of a column given its index.
      Parameters:
      col - the index of the column
      Returns:
      the direction of the column
      Since:
      2007.07.19 (Christian Borgelt)
    • setColumnDir

      public void setColumnDir(int col, int dir)
      Set the direction of a column given its index.
      Parameters:
      col - the index of the column
      dir - the direction to set
      Since:
      2007.07.19 (Christian Borgelt)
    • getColumnWeight

      public double getColumnWeight(int col)
      Get the weight of a column given its index.
      Parameters:
      col - the index of the column
      Returns:
      the weight of the column
      Since:
      2007.07.24 (Christian Borgelt)
    • setColumnWeight

      public void setColumnWeight(int col, double wgt)
      Set the weight of a column given its index.
      Parameters:
      col - the index of the column
      wgt - the weight to set
      Since:
      2007.07.24 (Christian Borgelt)
    • addColumn

      public int addColumn(Column col)
      Add a column to the table.

      It is assumed that the column has the same number of rows as any column already present in the table. The first column added to a table determines the table's number of rows.

      Parameters:
      col - the column to add
      Returns:
      the index of the added column or -1 if a column with the same name already exists
      Since:
      2006.09.16 (Christian Borgelt)
    • addColumn

      public Column addColumn(String name, ColType type)
      Add a column to the table.
      Parameters:
      name - the name of the column to add
      type - the type of the column to add
      Returns:
      the added column or null if a column with the same name already exists
      Since:
      2007.07.23 (Christian Borgelt)
    • renameColumn

      public int renameColumn(int index, String name)
      Rename a column, that is, give it a new name.
      Parameters:
      index - the index of the column
      name - the new name of the column
      Returns:
      the index of the column, that is, index or -1 if replacing would create a duplicate
      Since:
      2006.10.06 (Christian Borgelt)
    • removeColumn

      public void removeColumn(int index)
      Remove a column from the table.
      Parameters:
      index - the index of the column to remove
      Since:
      2007.01.31 (Christian Borgelt)
    • removeColumn

      public void removeColumn(String name)
      Remove a column from the table.
      Parameters:
      name - the name of the column to remove
      Since:
      2007.01.31 (Christian Borgelt)
    • moveColumn

      public void moveColumn(int src, int dst)
      Move a column to a new position.
      Parameters:
      src - the index of the column to move
      dst - the index to which to move the column
      Since:
      2007.01.31 (Christian Borgelt)
    • reorderColumns

      public void reorderColumns(int[] map)
      Reorder the columns of the table.

      The desired reordering has to be stated as a permutation of the integer numbers 0 to size()-1, with each entry stating the new index for the column that is identified by the array index (forward map).

      If the reordering map is not such a permutation, the table will get into an inconsistent state that may lead to serious errors.

      Parameters:
      map - an integer array containing a permutation of the integer numbers 0 to getColumnCount()-1
      Since:
      2007.01.31 (Christian Borgelt)
    • sortColumns

      public int[] sortColumns()
      Sort the columns of the table by name.
      Returns:
      a map from the old indices (array indices) to the new indices (contents of array elements)
      Since:
      2007.07.26 (Christian Borgelt)
    • sortColumns

      public int[] sortColumns(Comparator<Object> cmp)
      Sort the columns of the table.
      Parameters:
      cmp - the comparator for the columns
      Returns:
      a map from the old indices (array indices) to the new indices (contents of array elements)
      Since:
      2007.07.26 (Christian Borgelt)
    • autoType

      public void autoType()
      Automatically determine the type of columns.

      Note that it is only tried to convert nominal columns.

      Since:
      2007.02.02 (Christian Borgelt)
    • autoDir

      public void autoDir()
      Automatically determine the direction of columns.
      Since:
      2007.06.07 (Christian Borgelt)
    • sortTypes

      public void sortTypes()
      Sort the values of the nominal columns.
      Since:
      2007.07.26 (Christian Borgelt)
    • sortTypes

      public void sortTypes(Comparator<Object> cmp)
      Sort the values of the nominal columns.
      Parameters:
      cmp - the comparator for the values
      Since:
      2007.07.26 (Christian Borgelt)
    • isCellEditable

      public boolean isCellEditable(int row, int col)
      Returns whether a table cell is editable.

      Editing is currently not supported, therefore this function always returns false.

      Specified by:
      isCellEditable in interface TableModel
      Overrides:
      isCellEditable in class AbstractTableModel
      Parameters:
      row - the row of the cell to access
      col - the column of the cell to access
      Returns:
      whether the specified cell is editable
      Since:
      2006.10.04 (Christian Borgelt)
    • getValueAt

      public Object getValueAt(int row, int col)
      Get the value of a table cell as an object.
      Specified by:
      getValueAt in interface TableModel
      Parameters:
      row - the row of the cell to access
      col - the column of the cell to access
      Returns:
      an object representing the value in the specified cell
      Since:
      2006.10.04 (Christian Borgelt)
    • setValueAt

      public void setValueAt(Object value, int row, int col)
      Set the value of a table cell from an object.
      Specified by:
      setValueAt in interface TableModel
      Overrides:
      setValueAt in class AbstractTableModel
      Parameters:
      value - the value to set in the specified cell
      row - the row of the cell to set
      col - the column of the cell to set
      Since:
      2006.10.04 (Christian Borgelt)
    • getStringAt

      public String getStringAt(int row, int col)
      Get the value of a table cell as a string.
      Parameters:
      row - the row of the cell to access
      col - the column of the cell to access
      Returns:
      a string description of the value in the specified cell
      Since:
      2006.10.04 (Christian Borgelt)
    • resize

      public void resize(int newcnt)
      Resize the table, that is, change the number of rows.

      Note that new value arrays are allocated regardless of whether the new number of rows coincides with the old number or not. Hence calling this function with the old number of rows effectively clones the value arrays of all columns.

      Parameters:
      newcnt - the new number of rows
      Since:
      2007.01.31 (Christian Borgelt)
    • readHeader

      public void readHeader(util.TableReader reader) throws IOException
      Read table header from an input stream.
      Parameters:
      reader - the table reader to read from
      Throws:
      IOException - if a read error occurs
      Since:
      2007.05.15 (Christian Borgelt)
      See Also:
    • readHeader

      public void readHeader(util.TableReader reader, int mode) throws IOException
      Read table header from an input stream.

      If (mode & NOHEADER) != 0, default field names are generated and the first data record is read into an internal buffer, which is processed on the next call to any of the readRow() functions.

      Parameters:
      reader - the table reader to read from
      mode - the read mode
      Throws:
      IOException - if a read error occurs
      Since:
      2007.05.15 (Christian Borgelt)
    • clearBuffer

      public void clearBuffer()
      Clear the internal record buffer.

      The internal record buffer of a table gets filled if one of the readHeader() functions is called with (mode & NOHEADER) != 0. This internal buffer would be processed on the next call to one of the readRow() functions. If this is not desired, the internal buffer can be cleared by calling this function.

      Since:
      2007.05.21 (Christian Borgelt)
    • readRow

      public boolean readRow(util.TableReader reader) throws IOException
      Read a table row.

      The row is not stored in the table, only the column types are adapted.

      Parameters:
      reader - the table reader to read from
      Returns:
      whether a record was read
      Throws:
      IOException - if a read error occurs
      Since:
      2007.05.15 (Christian Borgelt)
      See Also:
    • readRow

      public boolean readRow(int row, util.TableReader reader) throws IOException
      Read a table row.

      If row < 0, the row is not stored in the table, only the column types are adapted. If row ≥ 0, the corresponding table row must already exist in the table (that is, it must be getRowCount() > row).

      Parameters:
      row - the table row in which to store the read record
      reader - the table reader to read from
      Returns:
      whether a record was read
      Throws:
      IOException - if a read error occurs
      Since:
      2007.05.15 (Christian Borgelt)
      See Also:
    • readRow

      public boolean readRow(int row, util.TableReader reader, int mode) throws IOException
      Read a table row.

      If row < 0, the row is not stored in the table, only the column types are adapted. If row ≥ 0, the corresponding table row must already exist in the table (that is, it must be getRowCount() > row).

      Parameters:
      row - the table row in which to store the record
      reader - the table reader to read from
      mode - the read mode
      Returns:
      whether a record was read
      Throws:
      IOException - if a read error occurs
      Since:
      2007.05.15 (Christian Borgelt)
    • read

      public int read(util.TableReader reader) throws IOException
      Read table from an input stream.
      Parameters:
      reader - the table reader to read from
      Returns:
      the number of rows read
      Throws:
      IOException - if a read error occurs
      Since:
      2007.01.31 (Christian Borgelt)
    • read

      public int read(util.TableReader reader, int mode) throws IOException
      Read a table from an input stream.
      Parameters:
      reader - the table reader to read from
      mode - the read mode
      Returns:
      the number of rows read
      Throws:
      IOException - if a read error occurs
      Since:
      2007.02.08 (Christian Borgelt)
    • writeHeader

      public void writeHeader(util.TableWriter writer) throws IOException
      Write a table header.
      Parameters:
      writer - the table writer
      Throws:
      IOException - if a write error occurs
      Since:
      2007.05.15 (Christian Borgelt)
    • writeHeader

      public void writeHeader(util.TableWriter writer, int mode) throws IOException
      Write a table header.
      Parameters:
      writer - the table writer
      mode - the write mode
      Throws:
      IOException - if a write error occurs
      Since:
      2007.05.15 (Christian Borgelt)
    • writeRow

      public void writeRow(int row, util.TableWriter writer) throws IOException
      Write a table row.
      Parameters:
      row - the table row to write
      writer - the table writer
      Throws:
      IOException - if a write error occurs
      Since:
      2007.05.19 (Christian Borgelt)
    • writeRow

      public void writeRow(int row, util.TableWriter writer, int mode) throws IOException
      Write a table row.
      Parameters:
      row - the table row to write
      writer - the writer to write the table row to
      mode - the write mode
      Throws:
      IOException - if a write error occurs
      Since:
      2007.05.19 (Christian Borgelt)
    • write

      public void write(util.TableWriter writer) throws IOException
      Write the table.
      Parameters:
      writer - the table writer
      Throws:
      IOException - if a write error occurs
      Since:
      2007.01.31 (Christian Borgelt)
    • write

      public void write(util.TableWriter writer, int mode) throws IOException
      Write the table.
      Parameters:
      writer - the writer to write the table to
      mode - the write mode
      Throws:
      IOException - if a write error occurs
      Since:
      2007.01.31 (Christian Borgelt)
    • abort

      public void abort()
      Abort an i/o operation.

      Reading or writing a table with one of the read() or write() functions, respectively, is aborted after the next table row.

      Since:
      2007.05.08 (Christian Borgelt)
    • toString

      public String toString(String header)
      Create a string description of the table type.
      Parameters:
      header - the text to add as a header
      Returns:
      a string description of the table type
      Since:
      2007.05.02 (Christian Borgelt)
    • toString

      public String toString()
      Create a string description of the table type.
      Overrides:
      toString in class Object
      Returns:
      a string description of the table type
      Since:
      2007.02.13 (Christian Borgelt)
    • parse

      public static Table parse(String name, util.Scanner scan) throws IOException
      Parse a table description.
      Parameters:
      name - the name of the table to create
      scan - the scanner to read from
      Returns:
      the described table
      Throws:
      IOException - if an i/o error occurs or a duplicate column name is found
      Since:
      2007.02.16 (Christian Borgelt)
    • main

      public static void main(String[] args)
      Main function for testing basic functionality.
      Parameters:
      args - the command line arguments
      Since:
      2004.06.02 (Christian Borgelt)