Package com.opencsv.bean
Class StatefulBeanToCsv<T>
- java.lang.Object
-
- com.opencsv.bean.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 aWriter
, 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.
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
applyQuotesToAll
private java.util.List<CsvException>
capturedExceptions
private ICSVWriter
csvwriter
private java.util.Locale
errorLocale
private char
escapechar
private CsvExceptionHandler
exceptionHandler
private BeanExecutor<T>
executor
private boolean
headerWritten
private org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,java.lang.reflect.Field>
ignoredFields
private java.lang.String
lineEnd
private int
lineNumber
The beans being written are counted in the order they are written.private MappingStrategy<T>
mappingStrategy
private static char
NO_CHARACTER
private boolean
orderedResults
private java.lang.String
profile
private char
quotechar
private char
separator
private java.io.Writer
writer
-
Constructor Summary
Constructors Constructor Description StatefulBeanToCsv(char escapechar, java.lang.String lineEnd, MappingStrategy<T> mappingStrategy, char quotechar, char separator, CsvExceptionHandler exceptionHandler, java.io.Writer writer, boolean applyQuotesToAll, org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields, java.lang.String profile)
Constructor used when supplying a Writer instead of a CsvWriter class.StatefulBeanToCsv(MappingStrategy<T> mappingStrategy, CsvExceptionHandler exceptionHandler, boolean applyQuotesToAll, ICSVWriter csvWriter, org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields, java.lang.String profile)
Constructor used to allow building of aStatefulBeanToCsv
with a user-suppliedICSVWriter
class.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private void
beforeFirstWrite(T bean)
Custodial tasks that must be performed before beans are written to a CSV destination for the first time.java.util.List<CsvException>
getCapturedExceptions()
Any exceptions captured during writing of beans to a CSV destination can be retrieved through this method.boolean
isThrowExceptions()
Deprecated.There is simply no need for this method.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.private void
submitAllLines(java.util.Iterator<T> beans)
void
write(java.util.Iterator<T> iBeans)
Writes an iterator of beans out to theWriter
provided to the constructor.void
write(java.util.List<T> beans)
Writes a list of beans out to theWriter
provided to the constructor.void
write(java.util.stream.Stream<T> beans)
Writes a stream of beans out to theWriter
provided to the constructor.void
write(T bean)
Writes a bean out to theWriter
provided to the constructor.
-
-
-
Field Detail
-
NO_CHARACTER
private static final char NO_CHARACTER
- See Also:
- Constant Field Values
-
lineNumber
private int lineNumber
The beans being written are counted in the order they are written.
-
separator
private final char separator
-
quotechar
private final char quotechar
-
escapechar
private final char escapechar
-
lineEnd
private final java.lang.String lineEnd
-
headerWritten
private boolean headerWritten
-
mappingStrategy
private MappingStrategy<T> mappingStrategy
-
writer
private final java.io.Writer writer
-
csvwriter
private ICSVWriter csvwriter
-
exceptionHandler
private final CsvExceptionHandler exceptionHandler
-
capturedExceptions
private java.util.List<CsvException> capturedExceptions
-
orderedResults
private boolean orderedResults
-
executor
private BeanExecutor<T> executor
-
errorLocale
private java.util.Locale errorLocale
-
applyQuotesToAll
private final boolean applyQuotesToAll
-
ignoredFields
private final org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields
-
profile
private final java.lang.String profile
-
-
Constructor Detail
-
StatefulBeanToCsv
StatefulBeanToCsv(char escapechar, java.lang.String lineEnd, MappingStrategy<T> mappingStrategy, char quotechar, char separator, CsvExceptionHandler exceptionHandler, java.io.Writer writer, boolean applyQuotesToAll, org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields, java.lang.String profile)
Constructor used when supplying a Writer instead of a CsvWriter class. It is defined as package protected to ensure thatStatefulBeanToCsvBuilder
is always used.- Parameters:
escapechar
- The escape character to use when writing a CSV filelineEnd
- The line ending to use when writing a CSV filemappingStrategy
- The mapping strategy to use when writing a CSV filequotechar
- The quote character to use when writing a CSV fileseparator
- The field separator to use when writing a CSV fileexceptionHandler
- Determines the exception handling behaviorwriter
- AWriter
for writing the beans as a CSV toapplyQuotesToAll
- Whether all output fields should be quotedignoredFields
- The fields to ignore during processing. May benull
.profile
- The profile to use when configuring how to interpret bean fields
-
StatefulBeanToCsv
public StatefulBeanToCsv(MappingStrategy<T> mappingStrategy, CsvExceptionHandler exceptionHandler, boolean applyQuotesToAll, ICSVWriter csvWriter, org.apache.commons.collections4.MultiValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields, java.lang.String profile)
Constructor used to allow building of aStatefulBeanToCsv
with a user-suppliedICSVWriter
class.- Parameters:
mappingStrategy
- The mapping strategy to use when writing a CSV fileexceptionHandler
- Determines the exception handling behaviorapplyQuotesToAll
- Whether all output fields should be quotedcsvWriter
- An user-suppliedICSVWriter
for writing beans to a CSV outputignoredFields
- The fields to ignore during processing. May benull
.profile
- The profile to use when configuring how to interpret bean fields
-
-
Method Detail
-
beforeFirstWrite
private void beforeFirstWrite(T bean) throws CsvRequiredFieldEmptyException
Custodial tasks that must be performed before beans are written to a CSV destination for the first time.- Parameters:
bean
- Any bean to be written. Used to determine the mapping strategy automatically. The bean itself is not written to the output by this method.- Throws:
CsvRequiredFieldEmptyException
- If a required header is missing while attempting to write. Since every other header is hard-wired through the bean fields and their associated annotations, this can only happen with multi-valued fields.
-
write
public void write(T bean) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
Writes a bean out to theWriter
provided to the constructor.- Parameters:
bean
- A bean to be written to a CSV destination- Throws:
CsvDataTypeMismatchException
- If a field of the bean is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException
- If a field is marked as required, but the source is null
-
submitAllLines
private void submitAllLines(java.util.Iterator<T> beans) throws java.lang.InterruptedException
- Throws:
java.lang.InterruptedException
-
write
public void write(java.util.List<T> beans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
Writes a list of beans out to theWriter
provided to the constructor.- Parameters:
beans
- A list of beans to be written to a CSV destination- Throws:
CsvDataTypeMismatchException
- If a field of the beans is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException
- If a field is marked as required, but the source is null
-
write
public void write(java.util.Iterator<T> iBeans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
Writes an iterator of beans out to theWriter
provided to the constructor.- Parameters:
iBeans
- An iterator of beans to be written to a CSV destination- Throws:
CsvDataTypeMismatchException
- If a field of the beans is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException
- If a field is marked as required, but the source is null
-
write
public void write(java.util.stream.Stream<T> beans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException
Writes a stream of beans out to theWriter
provided to the constructor.- Parameters:
beans
- A stream of beans to be written to a CSV destination- Throws:
CsvDataTypeMismatchException
- If a field of the beans is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException
- If a field is marked as required, but the source is null
-
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 settingorderedResults
tofalse
. 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
@Deprecated public boolean isThrowExceptions()
Deprecated.There is simply no need for this method.- 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)
orwrite(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
-
-