Class StatefulBeanToCsv<T>

  • Type Parameters:
    T - Type of the bean to be written

    public class StatefulBeanToCsv<T>
    extends java.lang.Object
    This class writes beans out in CSV format to a Writer, keeping state information and making an intelligent guess at the mapping strategy to be applied.

    This class implements multi-threading on writing more than one bean, so there should be no need to use it across threads in an application. As such, it is not thread-safe.

    Since:
    3.9
    Author:
    Andrew Rucker Jones
    See Also:
    OpencsvUtils.determineMappingStrategy(java.lang.Class, java.util.Locale)
    • Constructor Summary

      Constructors 
      Constructor Description
      StatefulBeanToCsv​(MappingStrategy<T> mappingStrategy, boolean throwExceptions, boolean applyQuotesToAll, ICSVWriter csvWriter, org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,​java.lang.reflect.Field> ignoredFields)
      Constructor used to allow building of a StatefulBeanToCsv with a user-supplied ICSVWriter class.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.List<CsvException> getCapturedExceptions()
      Any exceptions captured during writing of beans to a CSV destination can be retrieved through this method.
      boolean isThrowExceptions()  
      void setErrorLocale​(java.util.Locale errorLocale)
      Sets the locale for all error messages.
      void setOrderedResults​(boolean orderedResults)
      Sets whether or not results must be written in the same order in which they appear in the list of beans provided as input.
      void write​(java.util.Iterator<T> iBeans)
      Writes an iterator of beans out to the Writer provided to the constructor.
      void write​(java.util.List<T> beans)
      Writes a list of beans out to the Writer provided to the constructor.
      void write​(java.util.stream.Stream<T> beans)
      Writes a stream of beans out to the Writer provided to the constructor.
      void write​(T bean)
      Writes a bean out to the Writer provided to the constructor.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StatefulBeanToCsv

        public StatefulBeanToCsv​(MappingStrategy<T> mappingStrategy,
                                 boolean throwExceptions,
                                 boolean applyQuotesToAll,
                                 ICSVWriter csvWriter,
                                 org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,​java.lang.reflect.Field> ignoredFields)
        Constructor used to allow building of a StatefulBeanToCsv with a user-supplied ICSVWriter class.
        Parameters:
        mappingStrategy - The mapping strategy to use when writing a CSV file
        throwExceptions - Whether or not exceptions should be thrown while writing the CSV file. If not, they are collected and can be retrieved via getCapturedExceptions().
        applyQuotesToAll - Whether all output fields should be quoted
        csvWriter - An user-supplied ICSVWriter for writing beans to a CSV output
        ignoredFields - The fields to ignore during processing. May be null.
    • Method Detail

      • setOrderedResults

        public void setOrderedResults​(boolean orderedResults)
        Sets whether or not results must be written in the same order in which they appear in the list of beans provided as input. The default is that order is preserved. If your data do not need to be ordered, you can get a slight performance boost by setting orderedResults to false. The lack of ordering then also applies to any captured exceptions, if you have chosen not to have exceptions thrown.
        Parameters:
        orderedResults - Whether or not the lines written are in the same order they appeared in the input
        Since:
        4.0
      • isThrowExceptions

        public boolean isThrowExceptions()
        Returns:
        Whether or not exceptions are thrown. If they are not thrown, they are captured and returned later via getCapturedExceptions().
      • getCapturedExceptions

        public java.util.List<CsvException> getCapturedExceptions()
        Any exceptions captured during writing of beans to a CSV destination can be retrieved through this method.

        Reads from the list are destructive! Calling this method will clear the list of captured exceptions. However, calling write(java.util.List) or write(java.lang.Object) multiple times with no intervening call to this method will not clear the list of captured exceptions, but rather add to it if further exceptions are thrown.

        Returns:
        A list of exceptions that would have been thrown during any and all read operations since the last call to this method
      • setErrorLocale

        public void setErrorLocale​(java.util.Locale errorLocale)
        Sets the locale for all error messages.
        Parameters:
        errorLocale - Locale for error messages. If null, the default locale is used.
        Since:
        4.0