Class AbstractBaseCsvCallbackHandler<T>

  • Type Parameters:
    T - the type of the record

    public abstract class AbstractBaseCsvCallbackHandler<T>
    extends CsvCallbackHandler<T>
    Base class for CsvCallbackHandler implementations that handles their own field storage and record building.

    This implementation is stateful and must not be reused.

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void addField​(char[] buf, int offset, int len, boolean quoted)
      Called for each field in the record.
      protected void beginRecord​(long startingLineNumber)
      Called at the beginning of each record.
      protected int getFieldCount()
      Returns the number of fields in the current record.
      protected long getStartingLineNumber()
      The starting line number of the current record.
      protected void handleBegin​(long startingLineNumber)
      Handles the beginning of a record.
      protected void handleComment​(char[] buf, int offset, int len)
      Handles a comment.
      protected void handleField​(int fieldIdx, char[] buf, int offset, int len, boolean quoted)
      Handles a field.
      protected boolean isComment()
      Returns whether the current record is a comment.
      protected boolean isEmptyLine()
      Returns whether the current record is an empty line.
      protected void setComment​(char[] buf, int offset, int len)
      Called for each comment line.
      protected RecordWrapper<T> wrapRecord​(T record)
      Builds a wrapper for the record that contains information necessary for the CsvReader in order to determine how to process the record.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • startingLineNumber

        private long startingLineNumber
      • comment

        private boolean comment
      • emptyLine

        private boolean emptyLine
      • fieldCount

        private int fieldCount
    • Constructor Detail

      • AbstractBaseCsvCallbackHandler

        protected AbstractBaseCsvCallbackHandler()
        Constructs a new instance.
    • Method Detail

      • isComment

        protected boolean isComment()
        Returns whether the current record is a comment.
        Returns:
        true if the current record is a comment
      • isEmptyLine

        protected boolean isEmptyLine()
        Returns whether the current record is an empty line.
        Returns:
        true if the current record is an empty line
      • getFieldCount

        protected int getFieldCount()
        Returns the number of fields in the current record.
        Returns:
        the number of fields in the current record
      • beginRecord

        protected final void beginRecord​(long startingLineNumber)
        Called at the beginning of each record.

        The startingLineNumber is the line number where the record starts (starting with 1). See CsvRecord.getStartingLineNumber(). Resets the internal state of this handler and delegates to handleBegin(long).

        Specified by:
        beginRecord in class CsvCallbackHandler<T>
        Parameters:
        startingLineNumber - the line number where the record starts (starting with 1)
      • handleBegin

        protected void handleBegin​(long startingLineNumber)
        Handles the beginning of a record.

        This method is called at the beginning of each record.

        Parameters:
        startingLineNumber - the line number where the record starts (starting with 1)
      • addField

        protected final void addField​(char[] buf,
                                      int offset,
                                      int len,
                                      boolean quoted)
        Called for each field in the record.

        A record can either be a comment or a regular record. If this method is called, the record is a regular record and cannot be a comment.

        The quoted parameter indicates whether the field was quoted. It is for informational purposes only. Any potential escape characters are already removed and the offset points to the first character after the opening quote and the len does not include the closing quote. Hence, a quoted field can be processed in the same way as an unquoted field. Some implementations need the information whether a field was quoted, e.g., for differentiating between null and empty fields (foo,,bar vs. foo,"",bar).

        The buf parameter is the internal buffer that contains the field value (among other data). Do not attempt to modify the buffer or store a reference to it. The buffer is reused for performance reasons.

        This implementation delegates to handleField(int, char[], int, int, boolean) after updating the emptyLine flag and before incrementing the fieldCount.

        Specified by:
        addField in class CsvCallbackHandler<T>
        Parameters:
        buf - the internal buffer that contains the field value (among other data)
        offset - the offset of the field value in the buffer
        len - the length of the field value
        quoted - true if the field was quoted
      • handleField

        protected void handleField​(int fieldIdx,
                                   char[] buf,
                                   int offset,
                                   int len,
                                   boolean quoted)
        Handles a field.

        This method is called for each field in the record.

        See CsvCallbackHandler.addField(char[], int, int, boolean) for more details on the parameters.

        Parameters:
        fieldIdx - the index of the field in the record (starting with 0)
        buf - the internal buffer that contains the field value (among other data)
        offset - the offset of the field value in the buffer
        len - the length of the field value
        quoted - true if the field was quoted
      • setComment

        protected final void setComment​(char[] buf,
                                        int offset,
                                        int len)
        Called for each comment line.

        Note that the comment character is not included in the value.

        This method is not called if CsvReader.CsvReaderBuilder.commentStrategy(CommentStrategy) is set to CommentStrategy.NONE.

        There can only be one invocation of this method per record. A record can either be a comment or a regular record. If this method is called, the record is a comment and cannot be a regular record.

        The buf parameter is the internal buffer that contains the field value (among other data). Do not attempt to modify the buffer or store a reference to it. The buffer is reused for performance reasons.

        This implementation delegates to handleComment(char[], int, int) after updating the comment and emptyLine flag and before incrementing the fieldCount.

        Specified by:
        setComment in class CsvCallbackHandler<T>
        Parameters:
        buf - the internal buffer that contains the field value (among other data)
        offset - the offset of the field value in the buffer
        len - the length of the field value
      • handleComment

        protected void handleComment​(char[] buf,
                                     int offset,
                                     int len)
        Handles a comment.

        This method is called for each comment line.

        See CsvCallbackHandler.setComment(char[], int, int) for more details on the parameters.

        Parameters:
        buf - the internal buffer that contains the field value (among other data)
        offset - the offset of the field value in the buffer
        len - the length of the field value
      • wrapRecord

        protected RecordWrapper<T> wrapRecord​(T record)
        Builds a wrapper for the record that contains information necessary for the CsvReader in order to determine how to process the record.
        Parameters:
        record - the actual record to be returned by the CsvReader, must not be null
        Returns:
        the wrapper for the actual record
        Throws:
        java.lang.NullPointerException - if null is passed for record