Class DataTable
| | firstName | lastName | birthDate | | 4a1 | Annie M. G. | Schmidt | 1911-03-20 | | c92 | Roald | Dahl | 1916-09-13 |
A table is either empty or contains one or more values. As such if a table has zero height it must have zero width and vice versa.
The first row of the the table may be referred to as the table header. The remaining cells as the table body.
A table can be converted into an object of an arbitrary type by a
DataTable.TableConverter
. A table created without a table converter will throw
a DataTable.NoConverterDefined
exception when doing so.
Several methods are provided to convert tables to common data structures such
as lists, maps, ect. These methods have the form asX
and will use the
provided data table converter. A DataTable is immutable and thread safe.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionasList()
Converts the table to a list ofString
s.<T> List<T>
Converts the table to a list ofitemType
.<T> List<T>
Converts the table to a list ofitemType
.asLists()
Converts the table to a list of lists ofString
s.Converts the table to a list of lists ofitemType
.Converts the table to a list of lists ofitemType
.asMap()
Converts the table to a single map ofString
toString
.<K,
V> Map<K, V> Converts the table to a single map ofkeyType
tovalueType
.<K,
V> Map<K, V> Converts the table to a single map ofkeyType
tovalueType
.asMaps()
Converts the table to a list of maps of strings.Converts the table to a list of maps ofkeyType
tovalueType
.Converts the table to a list of maps ofkeyType
tovalueType
.cell
(int row, int column) Returns a single table cell.cells()
Returns the cells of the table.column
(int column) Returns a single column.columns
(int fromColumn) Returns a table that is a view on a portion of this table.columns
(int fromColumn, int toColumn) Returns a table that is a view on a portion of this table.<T> T
Converts a table totype
.<T> T
Converts a table totype
.static DataTable
Creates a new DataTable.static DataTable
create
(List<List<String>> raw, DataTable.TableConverter tableConverter) Creates a new DataTable with a table converter.void
Performs a diff against an other instance.static DataTable
Creates an empty DataTable.entries()
Returns a view of the entries in a table.boolean
Returns the table converter of this data table.int
hashCode()
int
height()
Returns the number of rows in the table.boolean
isEmpty()
Returns true iff this table has no cells.void
print
(Appendable appendable) Deprecated.void
print
(StringBuilder appendable) Deprecated.superseded byDataTableFormatter.formatTo(DataTable, StringBuilder)
row
(int row) Returns a single row.rows
(int fromRow) Returns a table that is a view on a portion of this table.rows
(int fromRow, int toRow) Returns a table that is a view on a portion of this table.subTable
(int fromRow, int fromColumn) Returns a table that is a view on a portion of this table.subTable
(int fromRow, int fromColumn, int toRow, int toColumn) Returns a table that is a view on a portion of this table.toString()
Returns a string representation of the this table.Returns a transposed view on this table.void
unorderedDiff
(DataTable actual) Performs an unordered diff against an other instance.values()
Returns the values in the table as a single list.int
width()
Returns the number of columns in the table.
-
Method Details
-
create
Creates a new DataTable.- Parameters:
raw
- the underlying table- Returns:
- a new data table containing the raw values
- Throws:
NullPointerException
- if raw is nullIllegalArgumentException
- when the table is not rectangular or contains null values.
-
create
Creates a new DataTable with a table converter.- Parameters:
raw
- the underlying tabletableConverter
- to transform the table- Returns:
- a new data table containing the raw values
- Throws:
NullPointerException
- if either raw or tableConverter is nullIllegalArgumentException
- when the table is not rectangular or contains null values
-
emptyDataTable
Creates an empty DataTable.- Returns:
- an empty DataTable
-
getTableConverter
Returns the table converter of this data table.- Returns:
- the tables table converter
-
diff
Performs a diff against an other instance.- Parameters:
actual
- the other table to diff with- Throws:
TableDiffException
- if the tables are different
-
unorderedDiff
Performs an unordered diff against an other instance.- Parameters:
actual
- the other table to diff with- Throws:
TableDiffException
- if the tables are different
-
values
Returns the values in the table as a single list. Contains the cells ordered from left to right, top to bottom, starting at the top left.- Returns:
- the values of the table
-
asList
Converts the table to a list ofString
s.- Returns:
- a list of strings
- See Also:
-
asList
Converts the table to a list ofitemType
.- Type Parameters:
T
- the type of the list items- Parameters:
itemType
- the type of the list items- Returns:
- a list of objects
- See Also:
-
asList
Converts the table to a list ofitemType
.- Type Parameters:
T
- the type of the list items- Parameters:
itemType
- the type of the list items- Returns:
- a list of objects
- See Also:
-
asLists
Converts the table to a list of lists ofString
s.- Returns:
- a list of list of strings
- See Also:
-
asLists
Converts the table to a list of lists ofitemType
.- Type Parameters:
T
- the type of the list items- Parameters:
itemType
- the type of the list items- Returns:
- a list of list of objects
- See Also:
-
asLists
Converts the table to a list of lists ofitemType
.- Type Parameters:
T
- the type of the list items- Parameters:
itemType
- the type of the list items- Returns:
- a list of list of objects
- See Also:
-
asMap
Converts the table to a single map ofString
toString
.For each row the first cell is used to create the key value. The remaining cells are used to create the value. If the table only has a single column that value is null.
- Returns:
- a map
- See Also:
-
asMap
Converts the table to a single map ofkeyType
tovalueType
.For each row the first cell is used to create the key value. The remaining cells are used to create the value. If the table only has a single column that value is null.
- Type Parameters:
K
- key typeV
- value type- Parameters:
keyType
- key typevalueType
- value type- Returns:
- a map
- See Also:
-
asMap
Converts the table to a single map ofkeyType
tovalueType
.For each row the first cell is used to create the key value. The remaining cells are used to create the value. If the table only has a single column that value is null.
- Type Parameters:
K
- key typeV
- value type- Parameters:
keyType
- key typevalueType
- value type- Returns:
- a map
- See Also:
-
entries
Returns a view of the entries in a table. An entry is a map of the header values to the corresponding values in a row in the body of the table.- Returns:
- a view of the entries in a table.
-
asMaps
Converts the table to a list of maps of strings. For each row in the body of the table a map is created containing a mapping of column headers to the column cell of that row.- Returns:
- a list of maps
- See Also:
-
asMaps
Converts the table to a list of maps ofkeyType
tovalueType
. For each row in the body of the table a map is created containing a mapping of column headers to the column cell of that row.- Type Parameters:
K
- key typeV
- value type- Parameters:
keyType
- key typevalueType
- value type- Returns:
- a list of maps
- See Also:
-
asMaps
Converts the table to a list of maps ofkeyType
tovalueType
. For each row in the body of the table a map is created containing a mapping of column headers to the column cell of that row.- Type Parameters:
K
- key typeV
- value type- Parameters:
keyType
- key typevalueType
- value type- Returns:
- a list of maps
- See Also:
-
cells
Returns the cells of the table.- Returns:
- the cells of the table
-
cell
Returns a single table cell.- Parameters:
row
- row index of the cellcolumn
- column index of the cell- Returns:
- a single table cell
- Throws:
IndexOutOfBoundsException
- when eitherrow
orcolumn
is outside the table.
-
column
Returns a single column.- Parameters:
column
- column index the column- Returns:
- a single column
- Throws:
IndexOutOfBoundsException
- whencolumn
is outside the table.
-
columns
Returns a table that is a view on a portion of this table. The sub table begins atfromColumn
inclusive and extends to the end of that table.- Parameters:
fromColumn
- the beginning column index, inclusive- Returns:
- the specified sub table
- Throws:
IndexOutOfBoundsException
- when any endpoint is outside the table.IllegalArgumentException
- when a from endpoint comes after an to endpoint
-
columns
Returns a table that is a view on a portion of this table. The sub table begins atfromColumn
inclusive and extends totoColumn
exclusive.- Parameters:
fromColumn
- the beginning column index, inclusivetoColumn
- the end column index, exclusive- Returns:
- the specified sub table
- Throws:
IndexOutOfBoundsException
- when any endpoint is outside the table.IllegalArgumentException
- when a from endpoint comes after an to endpoint
-
convert
Converts a table totype
.- Type Parameters:
T
- the desired type- Parameters:
type
- the desired typetransposed
- transpose the table before transformation- Returns:
- an instance of
type
-
convert
Converts a table totype
.- Type Parameters:
T
- the desired type- Parameters:
type
- the desired typetransposed
- transpose the table before transformation- Returns:
- an instance of
type
-
equals
-
hashCode
public int hashCode() -
isEmpty
public boolean isEmpty()Returns true iff this table has no cells.- Returns:
- true iff this table has no cells
-
row
Returns a single row.- Parameters:
row
- row index the column- Returns:
- a single row
- Throws:
IndexOutOfBoundsException
- whenrow
is outside the table.
-
rows
Returns a table that is a view on a portion of this table. The sub table begins atfromRow
inclusive and extends to the end of that table.- Parameters:
fromRow
- the beginning row index, inclusive- Returns:
- the specified sub table
- Throws:
IndexOutOfBoundsException
- when any endpoint is outside the table.IllegalArgumentException
- when a from endpoint comes after an to endpoint
-
rows
Returns a table that is a view on a portion of this table. The sub table begins atfromRow
inclusive and extends totoRow
exclusive.- Parameters:
fromRow
- the beginning row index, inclusivetoRow
- the end row index, exclusive- Returns:
- the specified sub table
- Throws:
IndexOutOfBoundsException
- when any endpoint is outside the table.IllegalArgumentException
- when a from endpoint comes after an to endpoint
-
subTable
Returns a table that is a view on a portion of this table. The sub table begins atfromRow
inclusive andfromColumn
inclusive and extends to the last column and row.- Parameters:
fromRow
- the beginning row index, inclusivefromColumn
- the beginning column index, inclusive- Returns:
- the specified sub table
- Throws:
IndexOutOfBoundsException
- when any endpoint is outside the table.
-
subTable
Returns a table that is a view on a portion of this table. The sub table begins atfromRow
inclusive andfromColumn
inclusive and extends totoRow
exclusive andtoColumn
exclusive.- Parameters:
fromRow
- the beginning row index, inclusivefromColumn
- the beginning column index, inclusivetoRow
- the end row index, exclusivetoColumn
- the end column index, exclusive- Returns:
- the specified sub table
- Throws:
IndexOutOfBoundsException
- when any endpoint is outside the table.IllegalArgumentException
- when a from endpoint comes after an to endpoint
-
height
public int height()Returns the number of rows in the table.- Returns:
- the number of rows in the table
-
width
public int width()Returns the number of columns in the table.- Returns:
- the number of columns in the table
-
toString
Returns a string representation of the this table. -
print
Deprecated.superseded byDataTableFormatter.formatTo(DataTable, Appendable)
Prints a string representation of this table to theappendable
.- Parameters:
appendable
- to append the string representation of this table to.- Throws:
IOException
- If an I/O error occurs
-
print
Deprecated.superseded byDataTableFormatter.formatTo(DataTable, StringBuilder)
Prints a string representation of this table to theappendable
.- Parameters:
appendable
- to append the string representation of this table to.
-
transpose
Returns a transposed view on this table. Example:| a | 7 | 4 | | b | 9 | 2 |
becomes:
| a | b | | 7 | 9 | | 4 | 2 |
- Returns:
- a transposed view of the table
-
DataTableFormatter.formatTo(DataTable, Appendable)