Package csv.util
Class BeanReader<T>
java.lang.Object
csv.util.BeanReader<T>
- Type Parameters:
T
- Type of bean to be read
- All Implemented Interfaces:
java.util.Iterator<T>
public class BeanReader<T>
extends java.lang.Object
implements java.util.Iterator<T>
Reads beans from the underlying table stream.
Please notice that you need to explicitely pass the bean class to the constructor when you create a parameterized BeanReader inline:
// Error: Invalid usage BeanReader<TestBean> beanReader = new BeanReader<TestBean>(tableReader); // Correct usage BeanReader<TestBean> beanReader = new BeanReader<TestBean>(TestBean.class, tableReader);
You can omit the class argument in constructors when your BeanReader was explicitely defined as a class:
public class MyBeanReader extends BeanReader<TestBean> { ... } // Ok here BeanReader<TestBean> = new MyBeanReader(tableReader);
The reason for this is the lack of class parameter inspection for in-line parameters at runtime.
- Author:
- ralph
-
Constructor Summary
Constructors Modifier Constructor Description BeanReader(TableReader reader)
Constructor.BeanReader(TableReader reader, java.lang.String[] attributes)
Constructor.BeanReader(java.lang.Class<T> beanClass, TableReader reader)
Constructor.protected
BeanReader(java.lang.Class<T> beanClass, TableReader reader, boolean evaluateHeaderRow, java.lang.String[] attributes)
Internal Constructor.BeanReader(java.lang.Class<T> beanClass, TableReader reader, java.lang.String[] attributes)
Constructor. -
Method Summary
Modifier and Type Method Description void
close()
Closes the reader.T
convertToBean(java.lang.Object[] columns)
Constructs new bean from values in array.protected java.lang.String
getAttributeName(int columnIndex)
Returns the attribute name of specified columnjava.lang.String[]
getAttributes()
Returns the attributes that will be used for each column index.protected java.lang.reflect.Method
getMethod(java.lang.String attribute)
Returns the correct setter method object for the given attribute.protected java.lang.String
getMethodName(java.lang.String attribute)
This implementation returns the name of setter method for the given attributeboolean
hasNext()
Returns true when there are more beans to be returned.boolean
isEvaluateHeaderRow()
Returns true if attribute names will be evaluated from header row.protected boolean
isValidSetterMethod(java.lang.reflect.Method method)
Returns true if method conforms to JavaBean style of a Setter.T
next()
Returns the next bean from the table reader.void
readHeaderRow()
Reads the next row from stream and sets the attribute names.void
remove()
Method not supported.void
reset()
Resets the reader.protected void
setAttributes(java.lang.String[] attributes)
Sets the attribute names to be set for each column.
-
Constructor Details
-
BeanReader
Constructor. Use this constructor when underlying reader will deliver the attribute names in first record.- Parameters:
beanClass
- class of bean - reflection does not guarantee to find out the correct class.reader
- the underlying reader to read bean properties from
-
BeanReader
Constructor. Use this constructor when underlying reader will deliver the attribute names in first record and you created a specific parameterized class for the reader.- Parameters:
reader
- the underlying reader to read bean properties from
-
BeanReader
Constructor. Use this constructor if underlying reader does NOT deliver attribute names and you created a specific parameterized class for the reader.- Parameters:
reader
- the underlying reader to read bean properties fromattributes
- list of attribute names that will be used to create the beans
-
BeanReader
Constructor. Use this constructor if underlying reader does NOT deliver attribute names.- Parameters:
beanClass
- class of bean - reflection does not guarantee to find out the correct class.reader
- the underlying reader to read bean properties fromattributes
- list of attribute names that will be used to create the beans
-
BeanReader
protected BeanReader(java.lang.Class<T> beanClass, TableReader reader, boolean evaluateHeaderRow, java.lang.String[] attributes)Internal Constructor.- Parameters:
beanClass
- class of bean - reflection does not guarantee to find out the correct class.reader
- the underlying reader to read bean properties fromattributes
- list of attribute names that will be used to create the beansevaluateHeaderRow
- whether header row will be delivered by reader
-
-
Method Details
-
hasNext
public boolean hasNext()Returns true when there are more beans to be returned.- Specified by:
hasNext
in interfacejava.util.Iterator<T>
- See Also:
Iterator.hasNext()
,Iterator.hasNext()
-
next
Returns the next bean from the table reader.- Specified by:
next
in interfacejava.util.Iterator<T>
- See Also:
Iterator.next()
,Iterator.next()
,convertToBean(Object[])
-
reset
public void reset()Resets the reader. -
close
public void close()Closes the reader. -
convertToBean
Constructs new bean from values in array.- Parameters:
columns
- attribute values- Returns:
- new bean
-
remove
public void remove()Method not supported.- Specified by:
remove
in interfacejava.util.Iterator<T>
- See Also:
Iterator.remove()
-
isEvaluateHeaderRow
public boolean isEvaluateHeaderRow()Returns true if attribute names will be evaluated from header row.- Returns:
- true if attribute names will be evaluated
-
getAttributes
public java.lang.String[] getAttributes()Returns the attributes that will be used for each column index.- Returns:
- the attributes array where attribute name stands at respective index.
-
setAttributes
protected void setAttributes(java.lang.String[] attributes)Sets the attribute names to be set for each column.- Parameters:
attributes
- attribute names to set
-
readHeaderRow
public void readHeaderRow()Reads the next row from stream and sets the attribute names. -
getAttributeName
protected java.lang.String getAttributeName(int columnIndex)Returns the attribute name of specified column- Parameters:
columnIndex
- index of column- Returns:
- name or null if it was not set
-
getMethod
protected java.lang.reflect.Method getMethod(java.lang.String attribute)Returns the correct setter method object for the given attribute. The method will be found by inspection of the JavaBean class.- Parameters:
attribute
- attribute to be set- Returns:
- setter method
-
isValidSetterMethod
protected boolean isValidSetterMethod(java.lang.reflect.Method method)Returns true if method conforms to JavaBean style of a Setter.- Parameters:
method
- method- Returns:
- true if method is a setter.
-
getMethodName
protected java.lang.String getMethodName(java.lang.String attribute)This implementation returns the name of setter method for the given attribute- Parameters:
attribute
- attribute- Returns:
- the name of the setter method for this attribute
-