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>

public class BeanReader<T> extends Object implements 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 Details

    • BeanReader

      public BeanReader(Class<T> beanClass, TableReader reader)
      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

      public BeanReader(TableReader reader)
      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

      public BeanReader(TableReader reader, String[] attributes)
      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 from
      attributes - list of attribute names that will be used to create the beans
    • BeanReader

      public BeanReader(Class<T> beanClass, TableReader reader, String[] attributes)
      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 from
      attributes - 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 from
      attributes - list of attribute names that will be used to create the beans
      evaluateHeaderRow - 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 interface Iterator<T>
      See Also:
    • next

      public T next()
      Returns the next bean from the table reader.
      Specified by:
      next in interface Iterator<T>
      See Also:
    • reset

      public void reset()
      Resets the reader.
    • close

      public void close()
      Closes the reader.
    • convertToBean

      public T convertToBean(Object[] columns)
      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 interface Iterator<T>
      See Also:
    • 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 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(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 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 Method getMethod(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(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 String getMethodName(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