Class Table

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

public class Table
extends java.lang.Object
implements java.lang.Cloneable
Logic for displaying simple 2-dimensional tables of data.
Author:
Stephen G. Ware
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    static class  Table.Cell
    An individual unit of data in the table.
    static class  Table.Column
    A vertical sequence of cells.
    static class  Table.Row
    A horizontal sequence of cells.
    static class  Table.Sequence
    The abstract parent of Table.Row and Table.Column.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    ImmutableArray<Table.Cell> cells
    The table's cells
    ImmutableArray<Table.Column> columns
    The table's columns
    ImmutableArray<Table.Row> rows
    The table's rows
  • Constructor Summary

    Constructors 
    Constructor Description
    Table​(java.lang.Iterable<?> rowLabels, java.lang.Iterable<?> columnLabels)
    Constructs a new, empty table with rows and columns that have the given labels.
    Table​(java.lang.Object[] rowLabels, java.lang.Object[] columnLabels)
    Constructs a new, empty table with rows and columns that have the given labels.
  • Method Summary

    Modifier and Type Method Description
    Table addAverageColumn()
    Returns a new table with an additional column whose cells contain the averages of each row.
    Table addAverageColumn​(java.util.function.Function<java.lang.Object,​?> function)
    Returns a new table with an additional column whose cells contain the averages of each row.
    Table addAverageRow()
    Returns a new table with an additional row whose cells contain the averages of each column.
    Table addAverageRow​(java.util.function.Function<java.lang.Object,​?> function)
    Returns a new table with an additional row whose cells contain the averages of each column.
    Table addColumn​(java.lang.Object label)
    Returns a new table with an addition column with the given label.
    Table addRow​(java.lang.Object label)
    Returns a new table with an addition row with the given label.
    Table addTotalColumn()
    Returns a new table with an additional column whose cells contain the totals of each row.
    Table addTotalColumn​(java.util.function.Function<java.lang.Object,​?> function)
    Returns a new table with an additional column whose cells contain the totals of each row.
    Table addTotalRow()
    Returns a new table with an additional row whose cells contain the totals of each column.
    Table addTotalRow​(java.util.function.Function<java.lang.Object,​?> function)
    Returns a new table with an additional row whose cells contain the totals of each column.
    Table clone()  
    Table.Cell getCell​(java.lang.Object rowLabel, java.lang.Object columnLabel)
    Returns the cell at the interaction of the row with the given label and and the column with the given label
    Table.Column getColumn​(java.lang.Object label)
    Returns the column with the given label.
    Table.Row getRow​(java.lang.Object label)
    Returns the row with the given label.
    Table sort​(java.util.Comparator<? super Table.Row> rowComparator, java.util.Comparator<? super Table.Column> columnComparator)
    Returns a new table whose rows and columns have been sorted according to the given comparators.
    Table sortByColumn​(java.util.Comparator<? super Table.Column> comparator)
    Returns a new table whose columns have been sorted according to the given comparator.
    Table sortByRow​(java.util.Comparator<? super Table.Row> comparator)
    Returns a new table whose rows have been sorted according to the given comparator.
    java.lang.String toHTML()
    Returns an HTML representation of this table.
    java.lang.String toString()  
    Table transform​(java.util.function.Function<java.lang.Object,​?> transform)
    Returns a new table whose cell values are the results of applying the given function to the values in this table.
    Table transform​(java.util.function.Predicate<? super Table.Row> rows, java.util.function.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.
    Table transform​(java.util.function.Predicate<? super Table.Row> rows, java.util.function.Predicate<? super Table.Column> columns, java.util.function.Function<java.lang.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.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Table

      public Table​(java.lang.Object[] rowLabels, java.lang.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:
      java.lang.IllegalArgumentException - if any row labels are duplicates or one another or any column labels are duplicates of one another
    • Table

      public Table​(java.lang.Iterable<?> rowLabels, java.lang.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:
      java.lang.IllegalArgumentException - if any row labels are duplicates or one another or any column labels are duplicates of one another
  • Method Details

    • toString

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

      public Table clone()
    • getRow

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

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

      public Table.Cell getCell​(java.lang.Object rowLabel, java.lang.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:
      java.lang.IllegalArgumentException - if no such row or column exists
    • addRow

      public Table addRow​(java.lang.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​(java.lang.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​(java.util.function.Function<java.lang.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​(java.util.function.Function<java.lang.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​(java.util.function.Function<java.lang.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​(java.util.function.Function<java.lang.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​(java.util.function.Predicate<? super Table.Row> rows, java.util.function.Predicate<? super Table.Column> columns, java.util.function.Function<java.lang.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​(java.util.function.Predicate<? super Table.Row> rows, java.util.function.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​(java.util.function.Function<java.lang.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​(java.util.Comparator<? super Table.Row> rowComparator, java.util.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​(java.util.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​(java.util.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 java.lang.String toHTML()
      Returns an HTML representation of this table.
      Returns:
      an HTML string