Package csv.util

Class BeanWriter<T>

java.lang.Object
csv.util.BeanWriter<T>
Type Parameters:
T - Type of bean to be written

public class BeanWriter<T> extends Object
Writes beans to an underlying table writer. The attributes written are either determined by inspection of first bean to be written or by explicitely setting them through special methods.
Author:
ralph
  • Constructor Details

    • BeanWriter

      public BeanWriter(TableWriter writer, boolean writeHeaderRow)
      Constructor.
      Parameters:
      writer - the underlying writer
      writeHeaderRow - whether a header row with attribute names shall be written
  • Method Details

    • isWriteHeaderRow

      public boolean isWriteHeaderRow()
      Returns true when a header row with attribute names shall be written.
      Returns:
      the writeHeaderRow
    • setWriteHeaderRow

      protected void setWriteHeaderRow(boolean writeHeaderRow)
      Sets whether a header row with attribute names shall be written.
      Parameters:
      writeHeaderRow - true if header row shall be written.
    • writeBean

      public void writeBean(T bean) throws IOException
      Writes the bean to the underlying table writer.
      Parameters:
      bean - the bean to be written
      Throws:
      IOException - if bean cannot be written
      See Also:
    • close

      public void close()
      Closes the underlying writer.
    • getWriter

      public TableWriter getWriter()
      Returns the current writer.
      Returns:
      the writer
    • setWriter

      protected void setWriter(TableWriter writer)
      Sets the writer.
      Parameters:
      writer - the writer
    • convertToColumns

      public Object[] convertToColumns(T bean)
      Converts the given bean to an object array.
      Parameters:
      bean - bean to be converted
      Returns:
      object array with attribute values
    • getAttribute

      protected Object getAttribute(int index, T bean)
      Returns the value for given bean at given column index.
      Parameters:
      index - column index
      bean - bean object
      Returns:
      value in column
    • getAttribute

      protected Object getAttribute(String attribute, T bean)
      Returns the attribute value for given bean.
      Parameters:
      attribute - name of attribute
      bean - bean object
      Returns:
      value of attribute
    • getAttribute

      protected Object getAttribute(Method method, T bean)
      Returns the attribute value for given bean.
      Parameters:
      method - method that will deliver the value
      bean - bean object
      Returns:
      value of attribute
    • createAttributeList

      protected void createAttributeList(Class<T> clazz, List<String> attributes)
      Creates the attribute list from given list. All JavaBean getter methods will be taken over.
      Parameters:
      clazz - Class to introspect
      attributes - list of attributes
    • createAttributeList

      protected void createAttributeList(Class<T> clazz, String[] attributes)
      Creates the attribute list from given list. All JavaBean getter methods will be taken over.
      Parameters:
      clazz - Class to introspect
      attributes - list of attributes
    • createAttributeList

      protected void createAttributeList(Class<T> clazz)
      Creates the attribute list by introspection. All JavaBean getter methods will be taken over.
      Parameters:
      clazz - Class to introspect
    • addGetter

      protected void addGetter(Class<T> clazz, String attribute)
      Adds a getter method to the methods being used for retrieveing column values. The metthod must have a return type and no arguments.
      Parameters:
      clazz - Class to introspect
      attribute - name of attribute
    • isValidGetterMethod

      protected boolean isValidGetterMethod(Method method)
      Returns true if method conforms to JavaBean style of a Getter.
      Parameters:
      method - method
      Returns:
      true if method is a getter.
    • addGetter

      protected void addGetter(String attributeName, Method method)
      Adds a getter method to the methods being used for retrieveing column values. The metthod must have a return type and no arguments.
      Parameters:
      attributeName - name of attribute
      method - method name.
    • writeHeaderRow

      protected void writeHeaderRow()
      Writes the header row to the underlying table writer.
    • getAttributeName

      protected String getAttributeName(Method m)
      Returns the attribute name derived from method name
      Parameters:
      m - method
      Returns:
      attribute name
    • writeBeans

      public int writeBeans(Collection<? extends T> collection) throws IOException
      Copies the beans from the collection to this bean writer.
      Parameters:
      collection - collection that contains JavaBeans
      Returns:
      number of rows written
      Throws:
      IOException - when there is a problem with the writer.
    • writeBeans

      public int writeBeans(Iterator<? extends T> i) throws IOException
      Copies the beans from the collection to this bean writer.
      Parameters:
      i - iterator that delivers JavaBeans
      Returns:
      number of rows written
      Throws:
      IOException - when there is a problem with the writer.
    • writeBeans

      public int writeBeans(Object[] arr) throws IOException
      Copies the beans from the collection to this bean writer.
      Parameters:
      arr - array with beans
      Returns:
      number of rows written
      Throws:
      IOException - when there is a problem with the writer.