Class CSVTable
java.lang.Object
net.simplace.sim.io.CSVTable
Content of a CSV file (or CSV content string), split into lines and columns. The table is cached in
FileData and shared by all resources that read the same file with the same divider, lines to read and
skipped header lines - also by resources of other sessions.
Line 0 is the header line and stays a String[]. The data cells are stored as strings first and are
converted to the data type of their column when a resource needs the column (typeColumns(String[], CellConverter[])). The converted value replaces the string, so every cell is converted only once, however many
resources and sessions read the file, and the strings are not kept in memory. Columns no resource asks for stay
strings.
Rules that keep the results identical to reading the strings:
- A column gets exactly one conversion, identified by a key (e.g.
V:DOUBLE:nullfor a scalarDOUBLEvariable,Dfor an element of a double array). If a resource asks for another conversion of an already converted column,typeColumns(String[], CellConverter[])returnsfalseand the resource has to read the file again as strings (reloadRaw()). - A cell whose conversion fails stays the original string, and
nullcells staynull. The reader converts strings andnullagain with the original code, so errors are reported (or thrown) exactly as before.
- Author:
- Andreas Enders, Claude (Anthropic)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceConversion of one cell from its string to the value of the column's data type. -
Method Summary
Modifier and TypeMethodDescriptionstatic CSVTableSplits the lines into columns likeStringHelper.convertLinesToStringarray(Vector, String): the number of columns is taken from the first line, double quotes are removed and the values are trimmed; surplus columns in later lines are logged and dropped, missing columns arenull.intString[]Object[]getLine(int aIndex) intReads the file or content again as a new table that is not shared (used after a conflict of column conversions).booleantypeColumns(String[] aKeys, CSVTable.CellConverter[] aConverters) Converts the requested columns of all data lines, unless they are already converted with the same key.
-
Method Details
-
fromLines
public static CSVTable fromLines(List<String> aLines, String aDivider, Callable<CSVTable> aRawLoader) Splits the lines into columns likeStringHelper.convertLinesToStringarray(Vector, String): the number of columns is taken from the first line, double quotes are removed and the values are trimmed; surplus columns in later lines are logged and dropped, missing columns arenull.- Parameters:
aLines- the lines, the first one is the headeraDivider- column divider (regular expression as inString.split(String))aRawLoader- reads the file or content again as a new table (forreloadRaw())- Returns:
- the table, or
nullif there are no lines
-
getHeader
- Returns:
- the header line (column names)
-
getLineCount
public int getLineCount()- Returns:
- the number of lines including the header line
-
getLine
- Parameters:
aIndex- line index, 0 is the header- Returns:
- the cells of the line: strings,
nullor values converted bytypeColumns(String[], CellConverter[])
-
getColumnCount
public int getColumnCount()- Returns:
- the number of columns (of the first line)
-
typeColumns
Converts the requested columns of all data lines, unless they are already converted with the same key. Columns with a key but without converter (values used as strings) are only registered, so no other resource can convert them later. Nothing is changed if one of the requested columns already has another key.- Parameters:
aKeys- conversion key per column (index = column),nullfor columns the resource does not read; may be shorter or longer than the number of columns (surplus entries are ignored)aConverters- converter per column,nullto keep the strings- Returns:
trueif all requested columns now have the requested conversion,falseon a conflict with another resource (the table is unchanged)
-
reloadRaw
-