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 java.lang.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 Summary

    Constructors 
    Constructor Description
    BeanWriter​(TableWriter writer, boolean writeHeaderRow)
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    protected void addGetter​(java.lang.Class<T> clazz, java.lang.String attribute)
    Adds a getter method to the methods being used for retrieveing column values.
    protected void addGetter​(java.lang.String attributeName, java.lang.reflect.Method method)
    Adds a getter method to the methods being used for retrieveing column values.
    void close()
    Closes the underlying writer.
    java.lang.Object[] convertToColumns​(T bean)
    Converts the given bean to an object array.
    protected void createAttributeList​(java.lang.Class<T> clazz)
    Creates the attribute list by introspection.
    protected void createAttributeList​(java.lang.Class<T> clazz, java.lang.String[] attributes)
    Creates the attribute list from given list.
    protected void createAttributeList​(java.lang.Class<T> clazz, java.util.List<java.lang.String> attributes)
    Creates the attribute list from given list.
    protected java.lang.Object getAttribute​(int index, T bean)
    Returns the value for given bean at given column index.
    protected java.lang.Object getAttribute​(java.lang.reflect.Method method, T bean)
    Returns the attribute value for given bean.
    protected java.lang.Object getAttribute​(java.lang.String attribute, T bean)
    Returns the attribute value for given bean.
    protected java.lang.String getAttributeName​(java.lang.reflect.Method m)
    Returns the attribute name derived from method name
    TableWriter getWriter()
    Returns the current writer.
    protected boolean isValidGetterMethod​(java.lang.reflect.Method method)
    Returns true if method conforms to JavaBean style of a Getter.
    boolean isWriteHeaderRow()
    Returns true when a header row with attribute names shall be written.
    protected void setWriteHeaderRow​(boolean writeHeaderRow)
    Sets whether a header row with attribute names shall be written.
    protected void setWriter​(TableWriter writer)
    Sets the writer.
    void writeBean​(T bean)
    Writes the bean to the underlying table writer.
    int writeBeans​(java.lang.Object[] arr)
    Copies the beans from the collection to this bean writer.
    int writeBeans​(java.util.Collection<? extends T> collection)
    Copies the beans from the collection to this bean writer.
    int writeBeans​(java.util.Iterator<? extends T> i)
    Copies the beans from the collection to this bean writer.
    protected void writeHeaderRow()
    Writes the header row to the underlying table writer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 java.io.IOException
      Writes the bean to the underlying table writer.
      Parameters:
      bean - the bean to be written
      Throws:
      java.io.IOException - if bean cannot be written
      See Also:
      TableWriter.printRow(Object[]), convertToColumns(Object)
    • 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 java.lang.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 java.lang.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 java.lang.Object getAttribute​(java.lang.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 java.lang.Object getAttribute​(java.lang.reflect.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​(java.lang.Class<T> clazz, java.util.List<java.lang.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​(java.lang.Class<T> clazz, java.lang.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​(java.lang.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​(java.lang.Class<T> clazz, java.lang.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​(java.lang.reflect.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​(java.lang.String attributeName, java.lang.reflect.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 java.lang.String getAttributeName​(java.lang.reflect.Method m)
      Returns the attribute name derived from method name
      Parameters:
      m - method
      Returns:
      attribute name
    • writeBeans

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

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

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