public final class DataTable extends Object
| | 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 cells. 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 NoConverterDefined
exception when doing so.
A DataTable is immutable and thread safe.
| Modifier and Type | Class and Description |
|---|---|
static interface |
DataTable.TableConverter
Converts a
DataTable to another type. |
| Modifier and Type | Method and Description |
|---|---|
List<String> |
asList()
Returns a list view on the table.
|
<T> List<T> |
asList(Type itemType)
Converts the table to a list of
itemType. |
List<List<String>> |
asLists()
Returns the cells of the table.
|
<T> List<List<T>> |
asLists(Type itemType)
Converts the table to a list of lists of
itemType. |
<K,V> Map<K,V> |
asMap(Type keyType,
Type valueType)
Converts the table to a single map of
keyType to valueType. |
List<Map<String,String>> |
asMaps()
Converts the table to a list of maps of strings.
|
<K,V> List<Map<K,V>> |
asMaps(Type keyType,
Type valueType)
Converts the table to a list of maps of
keyType to valueType. |
String |
cell(int row,
int column)
Returns a single table cell.
|
List<List<String>> |
cells()
Returns the cells of the table.
|
List<String> |
column(int column)
Returns a single column.
|
DataTable |
columns(int fromColumn)
Returns a table that is a view on a portion of this
table.
|
DataTable |
columns(int fromColumn,
int toColumn)
Returns a table that is a view on a portion of this
table.
|
<T> T |
convert(Type type,
boolean transposed)
Converts a table to
type. |
static DataTable |
create(List<List<String>> raw)
Creates a new DataTable.
|
static DataTable |
create(List<List<String>> raw,
DataTable.TableConverter tableConverter)
Creates a new DataTable with a table converter.
|
void |
diff(DataTable actual)
Performs a diff against an other instance.
|
static DataTable |
emptyDataTable()
Creates an empty DataTable.
|
boolean |
equals(Object o) |
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)
Prints a string representation of this
table to the
appendable. |
void |
print(StringBuilder appendable)
Prints a string representation of this
table to the
appendable. |
List<String> |
row(int row)
Returns a single row.
|
DataTable |
rows(int fromRow)
Returns a table that is a view on a portion of this
table.
|
DataTable |
rows(int fromRow,
int toRow)
Returns a table that is a view on a portion of this
table.
|
DataTable |
subTable(int fromRow,
int fromColumn)
Returns a table that is a view on a portion of this
table.
|
DataTable |
subTable(int fromRow,
int fromColumn,
int toRow,
int toColumn)
Returns a table that is a view on a portion of this
table.
|
String |
toString()
Returns a string representation of the this
table.
|
DataTable |
transpose()
Returns a transposed view on this table.
|
void |
unorderedDiff(DataTable actual)
Performs an unordered diff against an other instance.
|
int |
width()
Returns the number of columns in the table.
|
public static DataTable create(List<List<String>> raw)
raw - the underlying tableNullPointerException - if raw is nullIllegalArgumentException - when the table is not rectangular or contains null values.public static DataTable create(List<List<String>> raw, DataTable.TableConverter tableConverter)
raw - the underlying tabletableConverter - to transform the tableNullPointerException - if either raw or tableConverter is nullIllegalArgumentException - when the table is not rectangular or contains null valuespublic static DataTable emptyDataTable()
public void diff(DataTable actual) throws TableDiffException
actual - the other table to diff withTableDiffException - if the tables are differentpublic void unorderedDiff(DataTable actual) throws TableDiffException
actual - the other table to diff withTableDiffException - if the tables are differentpublic List<String> asList()
public <T> List<T> asList(Type itemType)
itemType.T - the type of the list itemsitemType - the type of the list itemspublic List<List<String>> asLists()
public List<List<String>> cells()
public <T> List<List<T>> asLists(Type itemType)
itemType.T - the type of the list itemsitemType - the type of the list itemspublic <K,V> Map<K,V> asMap(Type keyType, Type valueType)
keyType to valueType.
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.
K - key typeV - value typekeyType - key typevalueType - value typepublic List<Map<String,String>> asMaps()
public <K,V> List<Map<K,V>> asMaps(Type keyType, Type valueType)
keyType to valueType.
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.K - key typeV - value typekeyType - key typevalueType - value typepublic String cell(int row, int column)
row - row index of the cellcolumn - column index of the cellIndexOutOfBoundsException - when either row or column
is outside the table.public List<String> column(int column)
column - column index the columnIndexOutOfBoundsException - when column
is outside the table.public DataTable columns(int fromColumn)
fromColumn inclusive
and extends to the end of that table.fromColumn - the beginning column index, inclusiveIndexOutOfBoundsException - when any endpoint is
outside the table.IllegalArgumentException - when a from endpoint
comes after an to endpointpublic DataTable columns(int fromColumn, int toColumn)
fromColumn inclusive
and extends to toColumn exclusive.fromColumn - the beginning column index, inclusivetoColumn - the end column index, exclusiveIndexOutOfBoundsException - when any endpoint is outside
the table.IllegalArgumentException - when a from endpoint comes
after an to endpointpublic <T> T convert(Type type, boolean transposed)
type.T - the desired typetype - the desired typetransposed - transpose the table before transformationtypepublic boolean isEmpty()
public List<String> row(int row)
row - row index the columnIndexOutOfBoundsException - when row
is outside the table.public DataTable rows(int fromRow)
fromRow inclusive
and extends to the end of that table.fromRow - the beginning row index, inclusiveIndexOutOfBoundsException - when any endpoint is
outside the table.IllegalArgumentException - when a from endpoint
comes after an to endpointpublic DataTable rows(int fromRow, int toRow)
fromRow inclusive
and extends to toRow exclusive.fromRow - the beginning row index, inclusivetoRow - the end row index, exclusiveIndexOutOfBoundsException - when any endpoint is outside
the table.IllegalArgumentException - when a from endpoint comes
after an to endpointpublic DataTable subTable(int fromRow, int fromColumn)
fromRow inclusive and
fromColumn inclusive and extends to the last column
and row.fromRow - the beginning row index, inclusivefromColumn - the beginning column index, inclusiveIndexOutOfBoundsException - when any endpoint is outside
the table.public DataTable subTable(int fromRow, int fromColumn, int toRow, int toColumn)
fromRow inclusive and
fromColumn inclusive and extends to toRow
exclusive and toColumn exclusive.fromRow - the beginning row index, inclusivefromColumn - the beginning column index, inclusivetoRow - the end row index, exclusivetoColumn - the end column index, exclusiveIndexOutOfBoundsException - when any endpoint is outside
the table.IllegalArgumentException - when a from endpoint comes
after an to endpointpublic int height()
public int width()
public String toString()
public void print(Appendable appendable) throws IOException
appendable.appendable - to append the string representation
of this table to.IOException - If an I/O error occurspublic void print(StringBuilder appendable)
appendable.appendable - to append the string representation
of this table to.public DataTable transpose()
| a | 7 | 4 |
| b | 9 | 2 |
becomes:
| a | b | | 7 | 9 | | 4 | 2 |
Copyright © 2018. All rights reserved.