Interface DataTable.TableConverter
- All Known Implementing Classes:
DataTableTypeRegistryTableConverter
- Enclosing class:
- DataTable
DataTable to another type.
There are three ways in which a table might be mapped to a certain type. The table converter considers the possible conversions in this order:
- Using the whole table to create a single instance.
- Using individual rows to create a collection of instances. The first row may be used as header.
- Using individual cells to a create a collection of instances.
-
Method Summary
-
Method Details
-
convert
Converts aDataTableto another type.Delegates to
toList,toLists,toMapandtoMapsforList<T>,List<List<T>>,Map<K,V>andList<Map<K,V>>respectively.- Type Parameters:
T- the type to convert to- Parameters:
dataTable- the table to converttype- the type to convert to- Returns:
- an object of type
-
convert
Converts aDataTableto another type.Delegates to
toList,toLists,toMapandtoMapsforList<T>,List<List<T>>,Map<K,V>andList<Map<K,V>>respectively.- Type Parameters:
T- the type to convert to- Parameters:
dataTable- the table to converttype- the type to convert totransposed- whether the table should be transposed first.- Returns:
- an object of type
-
toList
Converts aDataTableto a list.A table converter may either map each row or each individual cell to a list element.
For example:
| Annie M. G. Schmidt | 1911-03-20 | | Roald Dahl | 1916-09-13 | convert.toList(table, String.class);
can become[ "Annie M. G. Schmidt", "1911-03-20", "Roald Dahl", "1916-09-13" ]
While:
convert.toList(table, Author.class);
can become:
[ Author[ name: Annie M. G. Schmidt, birthDate: 1911-03-20 ], Author[ name: Roald Dahl, birthDate: 1916-09-13 ] ]
Likewise:
| firstName | lastName | birthDate | | Annie M. G. | Schmidt | 1911-03-20 | | Roald | Dahl | 1916-09-13 | convert.toList(table, Authors.class);
can become:[ Author[ firstName: Annie M. G., lastName: Schmidt, birthDate: 1911-03-20 ], Author[ firstName: Roald, lastName: Dahl, birthDate: 1916-09-13 ] ]
- Type Parameters:
T- the type to convert to- Parameters:
dataTable- the table to convertitemType- the list item type to convert to- Returns:
- a list of objects of
itemType
-
toLists
Converts aDataTableto a list of lists.Each row maps to a list, each table cell a list entry.
For example:
| Annie M. G. Schmidt | 1911-03-20 | | Roald Dahl | 1916-09-13 | convert.toLists(table, String.class);
can become[ [ "Annie M. G. Schmidt", "1911-03-20" ], [ "Roald Dahl", "1916-09-13" ] ]- Type Parameters:
T- the type to convert to- Parameters:
dataTable- the table to convertitemType- the list item type to convert to- Returns:
- a list of lists of objects of
itemType
-
toMap
Converts aDataTableto a map.The left column of the table is used to instantiate the key values. The other columns are used to instantiate the values.
For example:
| 4a1 | Annie M. G. Schmidt | 1911-03-20 | | c92 | Roald Dahl | 1916-09-13 | convert.toMap(table, Id.class, Authors.class);
can become:{ Id[ 4a1 ]: Author[ name: Annie M. G. Schmidt, birthDate: 1911-03-20 ], Id[ c92 ]: Author[ name: Roald Dahl, birthDate: 1916-09-13 ] }The header cells may be used to map values into the types. When doing so the first header cell may be left blank.
For example:
| | firstName | lastName | birthDate | | 4a1 | Annie M. G. | Schmidt | 1911-03-20 | | c92 | Roald | Dahl | 1916-09-13 | convert.toMap(table, Id.class, Authors.class);
can becomes:{ Id[ 4a1 ]: Author[ firstName: Annie M. G., lastName: Schmidt, birthDate: 1911-03-20 ], Id[ c92 ]: Author[ firstName: Roald, lastName: Dahl, birthDate: 1916-09-13 ] }- Type Parameters:
K- the key type to convert toV- the value type to convert to- Parameters:
dataTable- the table to convertkeyType- the key type to convert tovalueType- the value to convert to- Returns:
- a map of
keyTypevalueType
-
toMaps
Converts aDataTableto a list of maps.Each map represents a row in the table. The map keys are the column headers.
For example:
| firstName | lastName | birthDate | | Annie M. G. | Schmidt | 1911-03-20 | | Roald | Dahl | 1916-09-13 |
can become:[ {firstName: Annie M. G., lastName: Schmidt, birthDate: 1911-03-20 } {firstName: Roald, lastName: Dahl, birthDate: 1916-09-13 } ]- Type Parameters:
K- the key type to convert toV- the value type to convert to- Parameters:
dataTable- the table to convertkeyType- the key type to convert tovalueType- the value to convert to- Returns:
- a list of maps of
keyTypevalueType
-