Package io.cucumber.datatable
Class DataTable
- java.lang.Object
-
- io.cucumber.datatable.DataTable
-
@API(status=STABLE) public final class DataTable extends Object
A m-by-n table of string values. For example:| | 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 aDataTable.NoConverterDefinedexception when doing so.A DataTable is immutable and thread safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDataTable.TableConverterConverts aDataTableto another type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description List<String>asList()Returns a list view on the table.<T> List<T>asList(Type itemType)Converts the table to a list ofitemType.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 ofitemType.<K,V>
Map<K,V>asMap(Type keyType, Type valueType)Converts the table to a single map ofkeyTypetovalueType.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 ofkeyTypetovalueType.Stringcell(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.DataTablecolumns(int fromColumn)Returns a table that is a view on a portion of this table.DataTablecolumns(int fromColumn, int toColumn)Returns a table that is a view on a portion of this table.<T> Tconvert(Type type, boolean transposed)Converts a table totype.static DataTablecreate(List<List<String>> raw)Creates a new DataTable.static DataTablecreate(List<List<String>> raw, DataTable.TableConverter tableConverter)Creates a new DataTable with a table converter.voiddiff(DataTable actual)Performs a diff against an other instance.static DataTableemptyDataTable()Creates an empty DataTable.booleanequals(Object o)DataTable.TableConvertergetTableConverter()Returns the table converter of this data table.inthashCode()intheight()Returns the number of rows in the table.booleanisEmpty()Returns true iff this table has no cells.voidprint(Appendable appendable)Prints a string representation of this table to theappendable.voidprint(StringBuilder appendable)Prints a string representation of this table to theappendable.List<String>row(int row)Returns a single row.DataTablerows(int fromRow)Returns a table that is a view on a portion of this table.DataTablerows(int fromRow, int toRow)Returns a table that is a view on a portion of this table.DataTablesubTable(int fromRow, int fromColumn)Returns a table that is a view on a portion of this table.DataTablesubTable(int fromRow, int fromColumn, int toRow, int toColumn)Returns a table that is a view on a portion of this table.StringtoString()Returns a string representation of the this table.DataTabletranspose()Returns a transposed view on this table.voidunorderedDiff(DataTable actual)Performs an unordered diff against an other instance.intwidth()Returns the number of columns in the table.
-
-
-
Method Detail
-
create
public static DataTable create(List<List<String>> raw)
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
public static DataTable create(List<List<String>> raw, DataTable.TableConverter tableConverter)
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
public static DataTable emptyDataTable()
Creates an empty DataTable.- Returns:
- an empty DataTable
-
getTableConverter
public DataTable.TableConverter getTableConverter()
Returns the table converter of this data table.- Returns:
- the tables table converter
-
diff
public void diff(DataTable actual) throws TableDiffException
Performs a diff against an other instance.- Parameters:
actual- the other table to diff with- Throws:
TableDiffException- if the tables are different
-
unorderedDiff
public void unorderedDiff(DataTable actual) throws TableDiffException
Performs an unordered diff against an other instance.- Parameters:
actual- the other table to diff with- Throws:
TableDiffException- if the tables are different
-
asList
public List<String> asList()
Returns a list view on the table. Contains the cells ordered from left to right, top to bottom starting at the top left.- Returns:
- the cells of the table
-
asList
public <T> List<T> asList(Type itemType)
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
-
asLists
public List<List<String>> asLists()
Returns the cells of the table.- Returns:
- the cells of the table
-
cells
public List<List<String>> cells()
Returns the cells of the table.- Returns:
- the cells of the table
-
asLists
public <T> List<List<T>> asLists(Type itemType)
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
-
asMap
public <K,V> Map<K,V> asMap(Type keyType, Type valueType)
Converts the table to a single map ofkeyTypetovalueType.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
-
asMaps
public List<Map<String,String>> 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
-
asMaps
public <K,V> List<Map<K,V>> asMaps(Type keyType, Type valueType)
Converts the table to a list of maps ofkeyTypetovalueType. 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
-
cell
public String cell(int row, int column)
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 eitherroworcolumnis outside the table.
-
column
public List<String> column(int column)
Returns a single column.- Parameters:
column- column index the column- Returns:
- a single column
- Throws:
IndexOutOfBoundsException- whencolumnis outside the table.
-
columns
public DataTable columns(int fromColumn)
Returns a table that is a view on a portion of this table. The sub table begins atfromColumninclusive 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
public DataTable columns(int fromColumn, int toColumn)
Returns a table that is a view on a portion of this table. The sub table begins atfromColumninclusive and extends totoColumnexclusive.- 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
public <T> T convert(Type type, boolean transposed)
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
-
isEmpty
public boolean isEmpty()
Returns true iff this table has no cells.- Returns:
- true iff this table has no cells
-
row
public List<String> row(int row)
Returns a single row.- Parameters:
row- row index the column- Returns:
- a single row
- Throws:
IndexOutOfBoundsException- whenrowis outside the table.
-
rows
public DataTable rows(int fromRow)
Returns a table that is a view on a portion of this table. The sub table begins atfromRowinclusive 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
public DataTable rows(int fromRow, int toRow)
Returns a table that is a view on a portion of this table. The sub table begins atfromRowinclusive and extends totoRowexclusive.- 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
public DataTable subTable(int fromRow, int fromColumn)
Returns a table that is a view on a portion of this table. The sub table begins atfromRowinclusive andfromColumninclusive 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
public DataTable subTable(int fromRow, int fromColumn, int toRow, int toColumn)
Returns a table that is a view on a portion of this table. The sub table begins atfromRowinclusive andfromColumninclusive and extends totoRowexclusive andtoColumnexclusive.- 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
public String toString()
Returns a string representation of the this table.
-
print
public void print(Appendable appendable) throws IOException
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
public void print(StringBuilder appendable)
Prints a string representation of this table to theappendable.- Parameters:
appendable- to append the string representation of this table to.
-
transpose
public DataTable 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
-
-