Package edu.uky.ai.util
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 ofTable.Row
andTable.Column
. -
Field Summary
Fields Modifier and Type Field Description ImmutableArray<Table.Cell>
cells
The table's cellsImmutableArray<Table.Column>
columns
The table's columnsImmutableArray<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 labelTable.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.
-
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 rowscolumnLabels
- 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 rowscolumnLabels
- 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 classjava.lang.Object
-
clone
-
getRow
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
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
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 objectcolumnLabel
- 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
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
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
Returns a new table with an additional row whose cells contain the totals of each column. SeeTable.Sequence.sum(Function)
.- Parameters:
function
- a function for converting the values of the cells in each column toNumber
s that will be applied to each cell value before adding it to the sum- Returns:
- the new table
-
addTotalRow
Returns a new table with an additional row whose cells contain the totals of each column. SeeTable.Sequence.sum(Function)
.- Returns:
- the new table
-
addTotalColumn
Returns a new table with an additional column whose cells contain the totals of each row. SeeTable.Sequence.sum(Function)
.- Parameters:
function
- a function for converting the values of the cells in each row toNumber
s that will be applied to each cell value before adding it to the sum- Returns:
- the new table
-
addTotalColumn
Returns a new table with an additional column whose cells contain the totals of each row. SeeTable.Sequence.sum(Function)
.- Returns:
- the new table
-
addAverageRow
Returns a new table with an additional row whose cells contain the averages of each column. SeeTable.Sequence.average(Function)
.- Parameters:
function
- a function for converting the values of the cells in each column toNumber
s that will be applied to each cell value before including it in the average- Returns:
- the new table
-
addAverageRow
Returns a new table with an additional row whose cells contain the averages of each column. SeeTable.Sequence.average(Function)
.- Returns:
- the new table
-
addAverageColumn
Returns a new table with an additional column whose cells contain the averages of each row. SeeTable.Sequence.average(Function)
.- Parameters:
function
- a function for converting the values of the cells in each row toNumber
s that will be applied to each cell value before including it in the average- Returns:
- the new table
-
addAverageColumn
Returns a new table with an additional column whose cells contain the averages of each row. SeeTable.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 copycolumns
- a predicate describing which columns to copytransform
- 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 copycolumns
- a predicate describing which columns to copy- Returns:
- the new table
-
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 sortedcolumnComparator
- defined how the columns should be sorted- Returns:
- the new table
-
sortByRow
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
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
-