Class CSVTable

java.lang.Object
net.simplace.sim.io.CSVTable

public class CSVTable extends Object
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:null for a scalar DOUBLE variable, D for an element of a double array). If a resource asks for another conversion of an already converted column, typeColumns(String[], CellConverter[]) returns false and the resource has to read the file again as strings (reloadRaw()).
  • A cell whose conversion fails stays the original string, and null cells stay null. The reader converts strings and null again with the original code, so errors are reported (or thrown) exactly as before.
Thread safety: conversion runs synchronized on the table. A reader only reads the columns it converted (or registered) itself before, so it sees completely converted columns.
Author:
Andreas Enders, Claude (Anthropic)
  • Method Details

    • fromLines

      public static CSVTable fromLines(List<String> aLines, String aDivider, Callable<CSVTable> aRawLoader)
      Splits the lines into columns like StringHelper.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 are null.
      Parameters:
      aLines - the lines, the first one is the header
      aDivider - column divider (regular expression as in String.split(String))
      aRawLoader - reads the file or content again as a new table (for reloadRaw())
      Returns:
      the table, or null if there are no lines
    • getHeader

      public String[] getHeader()
      Returns:
      the header line (column names)
    • getLineCount

      public int getLineCount()
      Returns:
      the number of lines including the header line
    • getLine

      public Object[] getLine(int aIndex)
      Parameters:
      aIndex - line index, 0 is the header
      Returns:
      the cells of the line: strings, null or values converted by typeColumns(String[], CellConverter[])
    • getColumnCount

      public int getColumnCount()
      Returns:
      the number of columns (of the first line)
    • typeColumns

      public boolean typeColumns(String[] aKeys, CSVTable.CellConverter[] aConverters)
      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), null for columns the resource does not read; may be shorter or longer than the number of columns (surplus entries are ignored)
      aConverters - converter per column, null to keep the strings
      Returns:
      true if all requested columns now have the requested conversion, false on a conflict with another resource (the table is unchanged)
    • reloadRaw

      public CSVTable reloadRaw() throws Exception
      Reads the file or content again as a new table that is not shared (used after a conflict of column conversions).
      Returns:
      the new table of strings
      Throws:
      Exception - if reading fails