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 Summary
Constructors Constructor Description AbstractTableReader()Default Constructor. -
Method Summary
Modifier and Type Method Description voidaddCommentCallBack(CommentCallback callback)Deprecated.UseTableReader.registerCommentCallBack(CommentCallback)instead.voidclose()Does nothingprotected java.lang.Objectconvert(int columnIndex, java.lang.String value)Converts the string back to correct object.protected java.lang.Objectconvert(java.lang.String type, java.lang.String value)Converts the string back to correct object.protected java.lang.Object[]convertArray(java.lang.String[] columns)Returns an array from the columns.protected java.lang.Object[]convertArray(java.util.List<java.lang.String> columns)Returns an array from the columns.java.lang.Objectget(java.lang.String name, java.lang.Object[] row)Returns the value in column with specified name.intgetColumnIndex(java.lang.String name)Returns the column index of given column name.java.lang.StringgetColumnType(int columnIndex)Returns the type of a column.java.lang.Object[]getHeaderRow()Returns the header row.intgetLineCount()Line count reflects the lines in an input file.intgetMinimumColumnCount()Returns the minimum number of columns to be returned byIterator.next().intgetRowCount()Returns the row count.protected TypeConversionHandlergetTypeConversionHandler(java.lang.String type)Returns a type conversion handler for the given type.booleanhasHeaderRow()Tells whether the underlying stream has a header row or notprotected intincrementLineCount()Increases the line count.protected intincrementRowCount()Increments the row Count.booleanisHeaderRowRead()java.util.Iterator<java.lang.Object[]>iterator()protected voidnotifyComment(java.lang.String s, int row, int cell)Notifies all comment callbacks about a comment.voidopen()Opens the CSV reader.protected voidreadHeaderRow()Reads the header row if required.voidregisterCommentCallBack(CommentCallback callback)Adds a comment callback.voidregisterTypeConversionHandler(TypeConversionHandler handler)Registers a type conversion handler.voidremoveCommentCallBack(CommentCallback callback)Deprecated.voidreset()Resets the CSV reader and its underlying stream.voidsetColumnType(int columnIndex, java.lang.Class<?> type)Explicitely set the type of a column.voidsetHasHeaderRow(boolean hasHeaderRow)Tells the reader whether the underlying stream will treat first row as header row.protected voidsetHeaderRow(java.lang.String[] names)Sets the header rows.protected voidsetHeaderRowRead(boolean headerRowRead)voidsetMinimumColumnCount(int length)Sets the minimum number of columns to be returned byIterator.next().voidunregisterCommentCallBack(CommentCallback callback)Removes a comment callback.voidunregisterTypeConversionHandler(TypeConversionHandler handler)Unregisters a type conversion handler.
-
Constructor Details
-
AbstractTableReader
public AbstractTableReader()Default Constructor.
-
-
Method Details
-
open
public void open()Opens the CSV reader.- Specified by:
openin interfaceTableReader
-
reset
public void reset()Resets the CSV reader and its underlying stream.- Specified by:
resetin interfaceTableReader
-
getHeaderRow
public java.lang.Object[] getHeaderRow()Returns the header row.- Specified by:
getHeaderRowin interfaceTableReader- 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:
getColumnIndexin interfaceTableReader- 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 columntype- 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:
hasHeaderRowin interfaceTableReader- 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:
setHasHeaderRowin interfaceTableReader- Parameters:
hasHeaderRow- true if there is a header row.
-
addCommentCallBack
Deprecated.UseTableReader.registerCommentCallBack(CommentCallback)instead.Adds a comment callback.- Parameters:
callback- the callback
-
registerCommentCallBack
Adds a comment callback.- Specified by:
registerCommentCallBackin interfaceTableReader- Parameters:
callback- the callback
-
removeCommentCallBack
Deprecated.Removes a comment callback.- Parameters:
callback- the callback
-
unregisterCommentCallBack
Removes a comment callback.- Specified by:
unregisterCommentCallBackin interfaceTableReader- Parameters:
callback- the callback
-
iterator
public java.util.Iterator<java.lang.Object[]> iterator()- Specified by:
iteratorin interfacejava.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 aboutrow- row numbercell- cell number in row
-
registerTypeConversionHandler
Registers a type conversion handler.- Parameters:
handler- handler to register
-
unregisterTypeConversionHandler
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 fromgetColumnType(int)and then then forward the transformation toconvert(String, String).- Parameters:
columnIndex- index of column of this valuevalue- 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 returnedvalue- string representation of object- Returns:
- object
-
getTypeConversionHandler
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 byIterator.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 byIterator.next().- Returns:
- rows delivered so far
-
close
public void close()Does nothing- Specified by:
closein interfaceTableReader- See Also:
TableReader.close()
-
setMinimumColumnCount
public void setMinimumColumnCount(int length)Description copied from interface:TableReaderSets the minimum number of columns to be returned byIterator.next().- Specified by:
setMinimumColumnCountin interfaceTableReader- Parameters:
length- number of columns- See Also:
TableReader.setMinimumColumnCount(int)
-
getMinimumColumnCount
public int getMinimumColumnCount()Description copied from interface:TableReaderReturns the minimum number of columns to be returned byIterator.next().- Specified by:
getMinimumColumnCountin interfaceTableReader- 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
-