Interface FileReader<C extends FileReaderContext>

All Known Implementing Classes:
CSVFileReader, XLSFileReader

public interface FileReader<C extends FileReaderContext>
A reader for a file of a particular type. Responsible for the physical parsing of a file into conceptual data records for the import flow
Author:
Phillip Verheyden (phillipuniverse)
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    canHandle(String fileType)
    Whether or not this reader is applicable for the current context
    long
    Counts all of the data records within the file.
    createParsingContext(InputStream fileStream, long dataLinesToSkip)
    Creates a parsing context that is passed back throughout the rest of the parsing flows.
    long
    The current line that the context is pointing to, 1-indexed
    getHeaders(C context)
    Obtains the headers for the given context
    boolean
    hasNext(C context)
    Whether or not there are more data records to read as determined by context.
    readNextLine(C context)
    Returns the next row of data.
  • Method Details

    • canHandle

      boolean canHandle(String fileType)
      Whether or not this reader is applicable for the current context
      Parameters:
      fileType - type of the file that should be read
      Returns:
      whether or not this can handle the given file type
    • createParsingContext

      C createParsingContext(InputStream fileStream, long dataLinesToSkip) throws IOException
      Creates a parsing context that is passed back throughout the rest of the parsing flows. This must be invoked before attempting to invoke the parsing logic, but is not required for couting records in the file. Any data necessary to read off items, open additional resources, etc should be initialized here and stored within the returned context
      Parameters:
      fileStream - the underlying file that should be read from
      dataLinesToSkip - how many lines should be skipped from the read in file. Assuming there is a header line, given a current line number the lines to skip should subtract 2
      Returns:
      a context with reader-specific variables passed back through the lifecycle of the processing flow
      Throws:
      IOException
    • countTotalItems

      long countTotalItems(InputStream file)
      Counts all of the data records within the file. This should not include all of the physical lines within the file. For instance this should not include the header line or any lines that are denoted as comments
      Parameters:
      file - file to count records for
      Returns:
      the total number of data records in the file
    • getHeaders

      String[] getHeaders(C context)
      Obtains the headers for the given context
      Parameters:
      context - the context initialized by createParsingContext(InputStream, long)
      Returns:
      the headers from the file
    • currentLineNumber

      long currentLineNumber(C context)
      The current line that the context is pointing to, 1-indexed
      Parameters:
      context - the context initialized by createParsingContext(InputStream, long)
      Returns:
      the current line number that this reader is pointing to based on the context
    • hasNext

      boolean hasNext(C context)
      Whether or not there are more data records to read as determined by context. Should be used as a guard against readNextLine(FileReaderContext) to protect against an attempt at reading too far
      Parameters:
      context - the context initialized by createParsingContext(InputStream, long)
      Returns:
      whether or not there are more data records to read
    • readNextLine

      Map<String,String> readNextLine(C context)
      Returns the next row of data. Keys correspond to the headers and values are the specific values for those headers
      Parameters:
      context - the context initialized by createParsingContext(InputStream, long)
      Returns:
      a row of data from the file specified by context