Package csv.impl

Class AbstractTableReader

java.lang.Object
csv.impl.AbstractTableReader
All Implemented Interfaces:
TableReader, java.lang.Iterable<java.lang.Object[]>, java.util.Iterator<java.lang.Object[]>
Direct Known Subclasses:
AbstractStreamTableReader, JdbcReader, JTableReader

public abstract class AbstractTableReader
extends java.lang.Object
implements TableReader
Abstract implementation that shall be suitable for most implementations.
Author:
ralph
  • Constructor Details

  • Method Details

    • open

      public void open()
      Opens the CSV reader.
      Specified by:
      open in interface TableReader
    • reset

      public void reset()
      Resets the CSV reader and its underlying stream.
      Specified by:
      reset in interface TableReader
    • getHeaderRow

      public java.lang.Object[] getHeaderRow()
      Returns the header row.
      Specified by:
      getHeaderRow in interface TableReader
      Returns:
      header row if such was defined.
    • readHeaderRow

      protected void readHeaderRow()
      Reads the header row if required. This is an empty method. Subclasses must override to correctly read the header row.
    • setHeaderRow

      protected void setHeaderRow​(java.lang.String[] names)
      Sets the header rows.
      Parameters:
      names - names to be set
    • setHeaderRowRead

      protected void setHeaderRowRead​(boolean headerRowRead)
      Parameters:
      headerRowRead - the headerRowRead to set
    • get

      public java.lang.Object get​(java.lang.String name, java.lang.Object[] row)
      Returns the value in column with specified name. Returns null if row has no such column.
      Parameters:
      name - name of column (from header row)
      row - row of values
      Returns:
      value in row for specified column.
    • getColumnIndex

      public int getColumnIndex​(java.lang.String name)
      Returns the column index of given column name. The first column with given name will be returned.
      Specified by:
      getColumnIndex in interface TableReader
      Parameters:
      name - name of column
      Returns:
      index of column or -1 if it does not exist.
    • setColumnType

      public void setColumnType​(int columnIndex, java.lang.Class<?> type)
      Explicitely set the type of a column. This information will be used to convert the value of this column.
      Parameters:
      columnIndex - index of column
      type - type of column
      See Also:
      getTypeConversionHandler(String), convert(int, String)
    • getColumnType

      public java.lang.String getColumnType​(int columnIndex)
      Returns the type of a column. This information will be used to convert the value of this column.
      Parameters:
      columnIndex - index of column.
      Returns:
      type of values in column
      See Also:
      getTypeConversionHandler(String), convert(int, String)
    • hasHeaderRow

      public boolean hasHeaderRow()
      Tells whether the underlying stream has a header row or not
      Specified by:
      hasHeaderRow in interface TableReader
      Returns:
      true if there is a header row.
    • setHasHeaderRow

      public void setHasHeaderRow​(boolean hasHeaderRow)
      Tells the reader whether the underlying stream will treat first row as header row.
      Specified by:
      setHasHeaderRow in interface TableReader
      Parameters:
      hasHeaderRow - true if there is a header row.
    • addCommentCallBack

      @Deprecated public void addCommentCallBack​(CommentCallback callback)
      Adds a comment callback.
      Parameters:
      callback - the callback
    • registerCommentCallBack

      public void registerCommentCallBack​(CommentCallback callback)
      Adds a comment callback.
      Specified by:
      registerCommentCallBack in interface TableReader
      Parameters:
      callback - the callback
    • removeCommentCallBack

      @Deprecated public void removeCommentCallBack​(CommentCallback callback)
      Removes a comment callback.
      Parameters:
      callback - the callback
    • unregisterCommentCallBack

      public void unregisterCommentCallBack​(CommentCallback callback)
      Removes a comment callback.
      Specified by:
      unregisterCommentCallBack in interface TableReader
      Parameters:
      callback - the callback
    • iterator

      public java.util.Iterator<java.lang.Object[]> iterator()
      Specified by:
      iterator in interface java.lang.Iterable<java.lang.Object[]>
    • notifyComment

      protected void notifyComment​(java.lang.String s, int row, int cell)
      Notifies all comment callbacks about a comment.
      Parameters:
      s - the comment to notify about
      row - row number
      cell - cell number in row
    • registerTypeConversionHandler

      public void registerTypeConversionHandler​(TypeConversionHandler handler)
      Registers a type conversion handler.
      Parameters:
      handler - handler to register
    • unregisterTypeConversionHandler

      public void unregisterTypeConversionHandler​(TypeConversionHandler handler)
      Unregisters a type conversion handler.
      Parameters:
      handler - handler to unregister
    • convert

      protected java.lang.Object convert​(int columnIndex, java.lang.String value)
      Converts the string back to correct object. This method will retrieve the column type from getColumnType(int) and then then forward the transformation to convert(String, String).
      Parameters:
      columnIndex - index of column of this value
      value - string representation of object
      Returns:
      object the converted object
      See Also:
      convert(String, String), registerTypeConversionHandler(TypeConversionHandler), getColumnType(int)
    • convert

      protected java.lang.Object convert​(java.lang.String type, java.lang.String value)
      Converts the string back to correct object.
      Parameters:
      type - type of object being returned
      value - string representation of object
      Returns:
      object
    • getTypeConversionHandler

      protected TypeConversionHandler getTypeConversionHandler​(java.lang.String type)
      Returns a type conversion handler for the given type.
      Parameters:
      type - type to get a handler for
      Returns:
      conversion handler
    • incrementLineCount

      protected int incrementLineCount()
      Increases the line count. Line count reflects the lines in an input file.
      Returns:
      lines read so far
    • getLineCount

      public int getLineCount()
      Line count reflects the lines in an input file.
      Returns:
      lines read so far
    • incrementRowCount

      protected int incrementRowCount()
      Increments the row Count. Row count is the number of netto rows (<= line count) meaning rows delivered by Iterator.next().
      Returns:
      rows delivered so far
    • getRowCount

      public int getRowCount()
      Returns the row count. Row count is the number of netto rows (<= line count) meaning rows delivered by Iterator.next().
      Returns:
      rows delivered so far
    • close

      public void close()
      Does nothing
      Specified by:
      close in interface TableReader
      See Also:
      TableReader.close()
    • setMinimumColumnCount

      public void setMinimumColumnCount​(int length)
      Description copied from interface: TableReader
      Sets the minimum number of columns to be returned by Iterator.next().
      Specified by:
      setMinimumColumnCount in interface TableReader
      Parameters:
      length - number of columns
      See Also:
      TableReader.setMinimumColumnCount(int)
    • getMinimumColumnCount

      public int getMinimumColumnCount()
      Description copied from interface: TableReader
      Returns the minimum number of columns to be returned by Iterator.next().
      Specified by:
      getMinimumColumnCount in interface TableReader
      Returns:
      the minimumLineCount
    • convertArray

      protected java.lang.Object[] convertArray​(java.util.List<java.lang.String> columns)
      Returns an array from the columns. This function exists for convinience to take care of minimum column count.
      Parameters:
      columns - columns to return
      Returns:
      arrray with column values
    • convertArray

      protected java.lang.Object[] convertArray​(java.lang.String[] columns)
      Returns an array from the columns. This function exists for convinience to take care of minimum column count.
      Parameters:
      columns - columns to return
      Returns:
      arrray with column values
    • isHeaderRowRead

      public boolean isHeaderRowRead()
      Returns:
      the headerRowRead