Class Table

java.lang.Object
edu.uky.ai.util.Table
All Implemented Interfaces:
Cloneable

public class Table extends Object implements Cloneable
Logic for displaying simple 2-dimensional tables of data.
Author:
Stephen G. Ware
  • Field Details

  • Constructor Details

    • Table

      public Table(Object[] rowLabels, Object[] columnLabels)
      Constructs a new, empty table with rows and columns that have the given labels.
      Parameters:
      rowLabels - the labels of the rows
      columnLabels - the labels of the columns
      Throws:
      IllegalArgumentException - if any row labels are duplicates or one another or any column labels are duplicates of one another
    • Table

      public Table(Iterable<?> rowLabels, Iterable<?> columnLabels)
      Constructs a new, empty table with rows and columns that have the given labels.
      Parameters:
      rowLabels - the labels of the rows
      columnLabels - the labels of the columns
      Throws:
      IllegalArgumentException - if any row labels are duplicates or one another or any column labels are duplicates of one another
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      public Table clone()
      Overrides:
      clone in class Object
    • getRow

      public Table.Row getRow(Object label)
      Returns the row with the given label.
      Parameters:
      label - the label object
      Returns:
      the row with this label
      Throws:
      IllegalArgumentException - if no row has this label
    • getColumn

      public Table.Column getColumn(Object label)
      Returns the column with the given label.
      Parameters:
      label - the label object
      Returns:
      the column with this label
      Throws:
      IllegalArgumentException - if no column has this label
    • getCell

      public Table.Cell getCell(Object rowLabel, Object columnLabel)
      Returns the cell at the interaction of the row with the given label and and the column with the given label
      Parameters:
      rowLabel - the row label object
      columnLabel - the column label object
      Returns:
      the cell in the given row and column
      Throws:
      IllegalArgumentException - if no such row or column exists
    • addRow

      public Table addRow(Object label)
      Returns a new table with an addition row with the given label.
      Parameters:
      label - the label for the new row
      Returns:
      the new table
    • addColumn

      public Table addColumn(Object label)
      Returns a new table with an addition column with the given label.
      Parameters:
      label - the label for the new column
      Returns:
      the new table
    • addTotalRow

      public Table addTotalRow(Function<Object,?> function)
      Returns a new table with an additional row whose cells contain the totals of each column. See Table.Sequence.sum(Function).
      Parameters:
      function - a function for converting the values of the cells in each column to Numbers that will be applied to each cell value before adding it to the sum
      Returns:
      the new table
    • addTotalRow

      public Table addTotalRow()
      Returns a new table with an additional row whose cells contain the totals of each column. See Table.Sequence.sum(Function).
      Returns:
      the new table
    • addTotalColumn

      public Table addTotalColumn(Function<Object,?> function)
      Returns a new table with an additional column whose cells contain the totals of each row. See Table.Sequence.sum(Function).
      Parameters:
      function - a function for converting the values of the cells in each row to Numbers that will be applied to each cell value before adding it to the sum
      Returns:
      the new table
    • addTotalColumn

      public Table addTotalColumn()
      Returns a new table with an additional column whose cells contain the totals of each row. See Table.Sequence.sum(Function).
      Returns:
      the new table
    • addAverageRow

      public Table addAverageRow(Function<Object,?> function)
      Returns a new table with an additional row whose cells contain the averages of each column. See Table.Sequence.average(Function).
      Parameters:
      function - a function for converting the values of the cells in each column to Numbers that will be applied to each cell value before including it in the average
      Returns:
      the new table
    • addAverageRow

      public Table addAverageRow()
      Returns a new table with an additional row whose cells contain the averages of each column. See Table.Sequence.average(Function).
      Returns:
      the new table
    • addAverageColumn

      public Table addAverageColumn(Function<Object,?> function)
      Returns a new table with an additional column whose cells contain the averages of each row. See Table.Sequence.average(Function).
      Parameters:
      function - a function for converting the values of the cells in each row to Numbers that will be applied to each cell value before including it in the average
      Returns:
      the new table
    • addAverageColumn

      public Table addAverageColumn()
      Returns a new table with an additional column whose cells contain the averages of each row. See Table.Sequence.average(Function).
      Returns:
      the new table
    • transform

      public Table transform(Predicate<? super Table.Row> rows, Predicate<? super Table.Column> columns, Function<Object,?> transform)
      Returns a new table which only keeps the row and columns of this table that are identified by two predicates and whose cell values are the results of applying the given function to the values in this table.
      Parameters:
      rows - a predicate describing which rows to copy
      columns - a predicate describing which columns to copy
      transform - a function to transform the values of the cells
      Returns:
      the new table
    • transform

      public Table transform(Predicate<? super Table.Row> rows, Predicate<? super Table.Column> columns)
      Returns a new table which only keeps the row and columns of this table that are identified by two predicates.
      Parameters:
      rows - a predicate describing which rows to copy
      columns - a predicate describing which columns to copy
      Returns:
      the new table
    • transform

      public Table transform(Function<Object,?> transform)
      Returns a new table whose cell values are the results of applying the given function to the values in this table.
      Parameters:
      transform - a function to transform the values of the cells
      Returns:
      the new table
    • sort

      public Table sort(Comparator<? super Table.Row> rowComparator, Comparator<? super Table.Column> columnComparator)
      Returns a new table whose rows and columns have been sorted according to the given comparators.
      Parameters:
      rowComparator - defines how the rows should be sorted
      columnComparator - defined how the columns should be sorted
      Returns:
      the new table
    • sortByRow

      public Table sortByRow(Comparator<? super Table.Row> comparator)
      Returns a new table whose rows have been sorted according to the given comparator.
      Parameters:
      comparator - defines how the rows should be sorted
      Returns:
      the new table
    • sortByColumn

      public Table sortByColumn(Comparator<? super Table.Column> comparator)
      Returns a new table whose columns have been sorted according to the given comparator.
      Parameters:
      comparator - defines how the columns should be sorted
      Returns:
      the new table
    • toHTML

      public String toHTML()
      Returns an HTML representation of this table.
      Returns:
      an HTML string