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:
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
ModifierConstructorDescriptionBeanReader
(TableReader reader) Constructor.BeanReader
(TableReader reader, String[] attributes) Constructor.BeanReader
(Class<T> beanClass, TableReader reader) Constructor.protected
BeanReader
(Class<T> beanClass, TableReader reader, boolean evaluateHeaderRow, String[] attributes) Internal Constructor.BeanReader
(Class<T> beanClass, TableReader reader, String[] attributes) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the reader.convertToBean
(Object[] columns) Constructs new bean from values in array.protected String
getAttributeName
(int columnIndex) Returns the attribute name of specified columnString[]
Returns the attributes that will be used for each column index.protected Method
Returns the correct setter method object for the given attribute.protected String
getMethodName
(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
Returns true if attribute names will be evaluated from header row.protected boolean
isValidSetterMethod
(Method method) Returns true if method conforms to JavaBean style of a Setter.next()
Returns the next bean from the table reader.void
Reads the next row from stream and sets the attribute names.void
remove()
Method not supported.void
reset()
Resets the reader.protected void
setAttributes
(String[] attributes) Sets the attribute names to be set for each column.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining
-
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(Class<T> beanClass, TableReader reader, boolean evaluateHeaderRow, 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. -
next
Returns the next bean from the table reader. -
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. -
isEvaluateHeaderRow
public boolean isEvaluateHeaderRow()Returns true if attribute names will be evaluated from header row.- Returns:
- true if attribute names will be evaluated
-
getAttributes
Returns the attributes that will be used for each column index.- Returns:
- the attributes array where attribute name stands at respective index.
-
setAttributes
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
Returns the attribute name of specified column- Parameters:
columnIndex
- index of column- Returns:
- name or null if it was not set
-
getMethod
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
Returns true if method conforms to JavaBean style of a Setter.- Parameters:
method
- method- Returns:
- true if method is a setter.
-
getMethodName
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
-