Class FileData

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

public class FileData extends Object
One file (or a string with file content) used by a file interface. For reading, the content is read once and cached, either as table (CSV, getDataCache(Integer[], Integer), see CSVTable) or as XML root element (getRootElement()); the cache can be shared with an equal file of another session. For writing, lines are appended to the file (writeToFile(String)). Paths starting with // are converted to UNC paths.
Author:
Andreas Enders
  • Constructor Details

    • FileData

      public FileData(String aPathname, String aContent, FWSimSession aActiveInSession, String aDivider, String aProjectID)
      Creates the file object.
      Parameters:
      aPathname - path of the file, or null if the content is given
      aContent - the data as string (used only if the path is null)
      aActiveInSession - the session
      aDivider - column divider for CSV files, may be null
      aProjectID - id of the current project (not used)
    • FileData

      public FileData(String aPathname, String aContent, FWSimSession aActiveInSession)
      Creates the file object without divider (e.g. for XML files).
      Parameters:
      aPathname - path of the file, or null if the content is given
      aContent - the data as string (used only if the path is null)
      aActiveInSession - the session
  • Method Details

    • setCache

      public void setCache(FileData aFileData, boolean aWasUpdated)
      Shares the cache of an equal file (e.g. of another session) instead of reading the file again.
      Parameters:
      aFileData - the file whose cache is used
      aWasUpdated - if the file counts as updated
    • getDataCache

      public CSVTable getDataCache(Integer[] aLinesToRead, Integer aSkipHeaderLines) throws IOException
      Returns the content as table, split by the divider. The file is read on the first call and again if the lines to read or header lines change; the table of the shared file is used only if it was read with the same divider, lines to read and header lines. Content given as string is cached the same way. The table is shared: the resources convert its columns to their data types in place (see CSVTable).
      Parameters:
      aLinesToRead - lines to read, or null for all
      aSkipHeaderLines - number of lines to skip at the start, may be null
      Returns:
      the table; line 0 is the header
      Throws:
      IOException - if the file cannot be read or is empty
    • getRootElement

      public org.jdom2.Element getRootElement() throws IOException
      Returns the XML root element; the file (or content) is parsed on the first call.
      Returns:
      the root element, or null without file name and content
      Throws:
      IOException - if the file cannot be read or parsed
    • writeStringsToFile

      public void writeStringsToFile(Collection<String> aCollection)
      Appends the strings as one line, separated by the divider; null is written as NULL.
      Parameters:
      aCollection - the values of the line (e.g. the header)
    • writeObjectsToFile

      public void writeObjectsToFile(Collection<Object> aCollection, LinkedHashMap<String, FWSimVariable<?>> aHeaderVariables, CSVInterface.DATA_MODE aDataMode)
      Appends an output line, separated by the divider. Values are formatted by the format of their header variable, e.g. <out id="CURRENT_DAY" datatype="DATE" format="yyyy-MM-dd" .../> or <out id="decimal_num" datatype="DOUBLE" format="%.2f" .../> (12.2363452 → 12.24); booleans with a format are written as 1/0, null as NULL.

      Arrays: in COL mode all elements are written in one line (separated by the divider, or by | if the variable has no count); in ROW mode one line per element of the longest array is written, shorter arrays are filled with NULL.

      Parameters:
      aCollection - the values of the line, in the order of the header variables
      aHeaderVariables - the header variables (formats and data types)
      aDataMode - how arrays are written
    • writeToFile

      public void writeToFile(String aLine)
      Appends a line (with CR LF, platform default charset - the same charset the resources are read with, see IOHelper.readLinesOfStream(java.io.InputStream, Integer[], String)); the output stream is opened on the first call. Errors are logged.
      Parameters:
      aLine - the line without line break
    • getSession

      public FWSimSession getSession()
      Returns:
      the session the file is used in
    • closeFile

      public void closeFile()
      Closes the file after writing or reading: closes the output stream and discards the caches. Note: the file name is discarded as well, so the object is no longer equal to other objects for the same file.
    • setUpdated

      public void setUpdated(boolean aIsUpdated)
      Parameters:
      aIsUpdated - if the file is new or changed and has to be read again
    • isUpdated

      public boolean isUpdated()
      Returns:
      true if the file is new or changed and has to be read again (initially true)
    • getFile

      public File getFile()
      Returns:
      the file, or null for content
    • getUniqueID

      public String getUniqueID()
      Returns:
      an id of the source data: the path for files, the hash code of the content otherwise
    • equals

      public boolean equals(Object obj)
      Two file objects are equal if they have the same path (content objects are never equal).
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Consistent with equals(Object): based on the file name.
      Overrides:
      hashCode in class Object
      See Also:
    • toString

      public String toString()
      Returns the absolute path, or the beginning of the content.
      Overrides:
      toString in class Object
      See Also: