- java.lang.Object
-
- de.siegmar.fastcsv.reader.CsvCallbackHandler<T>
-
- de.siegmar.fastcsv.reader.AbstractBaseCsvCallbackHandler<T>
-
- Type Parameters:
T
- the type of the record
public abstract class AbstractBaseCsvCallbackHandler<T> extends CsvCallbackHandler<T>
Base class forCsvCallbackHandler
implementations that handles their own field storage and record building.This implementation is stateful and must not be reused.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
comment
private boolean
emptyLine
private int
fieldCount
private long
startingLineNumber
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractBaseCsvCallbackHandler()
Constructs a new instance.
-
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 theCsvReader
in order to determine how to process the record.-
Methods inherited from class de.siegmar.fastcsv.reader.CsvCallbackHandler
buildRecord, terminate
-
-
-
-
Method Detail
-
getStartingLineNumber
protected long getStartingLineNumber()
The starting line number of the current record.See
CsvCallbackHandler.beginRecord(long)
andCsvRecord.getStartingLineNumber()
.- Returns:
- the starting line number of the current record
-
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). SeeCsvRecord.getStartingLineNumber()
. Resets the internal state of this handler and delegates tohandleBegin(long)
.- Specified by:
beginRecord
in classCsvCallbackHandler<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 theoffset
points to the first character after the opening quote and thelen
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 betweennull
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 theemptyLine
flag and before incrementing thefieldCount
.- Specified by:
addField
in classCsvCallbackHandler<T>
- Parameters:
buf
- the internal buffer that contains the field value (among other data)offset
- the offset of the field value in the bufferlen
- the length of the field valuequoted
-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 bufferlen
- the length of the field valuequoted
-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 toCommentStrategy.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 thecomment
andemptyLine
flag and before incrementing thefieldCount
.- Specified by:
setComment
in classCsvCallbackHandler<T>
- Parameters:
buf
- the internal buffer that contains the field value (among other data)offset
- the offset of the field value in the bufferlen
- 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 bufferlen
- the length of the field value
-
wrapRecord
protected RecordWrapper<T> wrapRecord(T record)
Builds a wrapper for the record that contains information necessary for theCsvReader
in order to determine how to process the record.- Parameters:
record
- the actual record to be returned by theCsvReader
, must not benull
- Returns:
- the wrapper for the actual record
- Throws:
java.lang.NullPointerException
- ifnull
is passed forrecord
-
-