Top Description Fields Constructors Methods
com.opencsv.bean

public Class StatefulBeanToCsv<T>

extends Object
Class Inheritance
Type Parameters
<T>
Type of the bean to be written
Imports
com.opencsv.CSVWriter, .ICSVParser, .ICSVWriter, com.opencsv.bean.concurrent.BeanExecutor, .ProcessCsvBean, com.opencsv.bean.exceptionhandler.CsvExceptionHandler, .ExceptionHandlerThrow, com.opencsv.bean.util.OpencsvUtils, .OrderedObject, com.opencsv.exceptions.CsvDataTypeMismatchException, .CsvException, .CsvRequiredFieldEmptyException, .CsvRuntimeException, org.apache.commons.collections4.CollectionUtils, .MultiValuedMap, org.apache.commons.collections4.iterators.PeekingIterator, org.apache.commons.lang3.ObjectUtils, .StringUtils, java.io.Writer, java.lang.reflect.Field, java.util.*, java.util.concurrent.ArrayBlockingQueue, .BlockingQueue, .LinkedBlockingQueue, .RejectedExecutionException, java.util.stream.Stream, .StreamSupport

This class writes beans out in CSV format to a java.io.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.

Author
Andrew Rucker Jones
Since
3.9
See Also
OpencsvUtils#determineMappingStrategy(java.lang.Class, java.util.Locale, java.lang.String)

Field Summary

Modifier and TypeField and Description
private final boolean
private List<CsvException>
private ICSVWriter
private Locale
private final char
private final CsvExceptionHandler
private BeanExecutor<T>
private boolean
private final MultiValuedMap<Class<?>, Field>
private final String
private int
lineNumber

The beans being written are counted in the order they are written.

private MappingStrategy<T>
private static final char
private boolean
private final String
private final char
private final char
private final Writer

Constructor Summary

AccessConstructor and Description
pack-priv
StatefulBeanToCsv(char
The escape character to use when writing a CSV file
escapechar
,
String
The line ending to use when writing a CSV file
lineEnd
,
MappingStrategy<T>
The mapping strategy to use when writing a CSV file
mappingStrategy
,
char
The quote character to use when writing a CSV file
quotechar
,
char
The field separator to use when writing a CSV file
separator
,
CsvExceptionHandler
Determines the exception handling behavior
exceptionHandler
,
Writer
A java.io.Writer for writing the beans as a CSV to
writer
,
boolean
Whether all output fields should be quoted
applyQuotesToAll
,
MultiValuedMap<Class<?>, Field>
The fields to ignore during processing. May be null.
ignoredFields
,
String
The profile to use when configuring how to interpret bean fields
profile
)

Constructor used when supplying a Writer instead of a CsvWriter class.

public
StatefulBeanToCsv(MappingStrategy<T>
The mapping strategy to use when writing a CSV file
mappingStrategy
,
CsvExceptionHandler
Determines the exception handling behavior
exceptionHandler
,
boolean
Whether all output fields should be quoted
applyQuotesToAll
,
ICSVWriter
An user-supplied com.opencsv.ICSVWriter for writing beans to a CSV output
csvWriter
,
MultiValuedMap<Class<?>, Field>
The fields to ignore during processing. May be null.
ignoredFields
,
String
The profile to use when configuring how to interpret bean fields
profile
)

Constructor used to allow building of a com.opencsv.bean.StatefulBeanToCsv with a user-supplied com.opencsv.ICSVWriter class.

Method Summary

Modifier and TypeMethod and Description
private void
beforeFirstWrite(T
Any bean to be written. Used to determine the mapping strategy automatically. The bean itself is not written to the output by this method.
bean
)

Custodial tasks that must be performed before beans are written to a CSV destination for the first time.

public List<CsvException>

Returns:

A list of exceptions that would have been thrown during any and all read operations since the last call to this method
getCapturedExceptions
()

Any exceptions captured during writing of beans to a CSV destination can be retrieved through this method.

public boolean
isThrowExceptions()
Deprecated There is simply no need for this method.

Returns: Whether or not exceptions are thrown.

public void
setErrorLocale(Locale
Locale for error messages. If null, the default locale is used.
errorLocale
)

Sets the locale for all error messages.

public void
setOrderedResults(boolean
Whether or not the lines written are in the same order they appeared in the input
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
public void
write(T
A bean to be written to a CSV destination
bean
)

Writes a bean out to the java.io.Writer provided to the constructor.

public void
write(List<T>
A list of beans to be written to a CSV destination
beans
)

Writes a list of beans out to the java.io.Writer provided to the constructor.

public void
write(Iterator<T>
An iterator of beans to be written to a CSV destination
iBeans
)

Writes an iterator of beans out to the java.io.Writer provided to the constructor.

public void
write(Stream<T>
A stream of beans to be written to a CSV destination
beans
)

Writes a stream of beans out to the java.io.Writer provided to the constructor.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applyQuotesToAllback to summary
private final boolean applyQuotesToAll
capturedExceptionsback to summary
private List<CsvException> capturedExceptions
csvwriterback to summary
private ICSVWriter csvwriter
errorLocaleback to summary
private Locale errorLocale
escapecharback to summary
private final char escapechar
exceptionHandlerback to summary
private final CsvExceptionHandler exceptionHandler
executorback to summary
private BeanExecutor<T> executor
headerWrittenback to summary
private boolean headerWritten
ignoredFieldsback to summary
private final MultiValuedMap<Class<?>, Field> ignoredFields
lineEndback to summary
private final String lineEnd
lineNumberback to summary
private int lineNumber

The beans being written are counted in the order they are written.

mappingStrategyback to summary
private MappingStrategy<T> mappingStrategy
NO_CHARACTERback to summary
private static final char NO_CHARACTER
orderedResultsback to summary
private boolean orderedResults
profileback to summary
private final String profile
quotecharback to summary
private final char quotechar
separatorback to summary
private final char separator
writerback to summary
private final Writer writer

Constructor Detail

StatefulBeanToCsvback to summary
pack-priv StatefulBeanToCsv(char escapechar, String lineEnd, MappingStrategy<T> mappingStrategy, char quotechar, char separator, CsvExceptionHandler exceptionHandler, Writer writer, boolean applyQuotesToAll, MultiValuedMap<Class<?>, Field> ignoredFields, String profile)

Constructor used when supplying a Writer instead of a CsvWriter class. It is defined as package protected to ensure that StatefulBeanToCsvBuilder is always used.

Parameters
escapechar:char

The escape character to use when writing a CSV file

lineEnd:String

The line ending to use when writing a CSV file

mappingStrategy:MappingStrategy<T>

The mapping strategy to use when writing a CSV file

quotechar:char

The quote character to use when writing a CSV file

separator:char

The field separator to use when writing a CSV file

exceptionHandler:CsvExceptionHandler

Determines the exception handling behavior

writer:Writer

A java.io.Writer for writing the beans as a CSV to

applyQuotesToAll:boolean

Whether all output fields should be quoted

ignoredFields:MultiValuedMap<Class<?>, Field>

The fields to ignore during processing. May be null.

profile:String

The profile to use when configuring how to interpret bean fields

StatefulBeanToCsvback to summary
public StatefulBeanToCsv(MappingStrategy<T> mappingStrategy, CsvExceptionHandler exceptionHandler, boolean applyQuotesToAll, ICSVWriter csvWriter, MultiValuedMap<Class<?>, Field> ignoredFields, String profile)

Constructor used to allow building of a com.opencsv.bean.StatefulBeanToCsv with a user-supplied com.opencsv.ICSVWriter class.

Parameters
mappingStrategy:MappingStrategy<T>

The mapping strategy to use when writing a CSV file

exceptionHandler:CsvExceptionHandler

Determines the exception handling behavior

applyQuotesToAll:boolean

Whether all output fields should be quoted

csvWriter:ICSVWriter

An user-supplied com.opencsv.ICSVWriter for writing beans to a CSV output

ignoredFields:MultiValuedMap<Class<?>, Field>

The fields to ignore during processing. May be null.

profile:String

The profile to use when configuring how to interpret bean fields

Method Detail

beforeFirstWriteback to summary
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:T

Any bean to be written. Used to determine the mapping strategy automatically. The bean itself is not written to the output by this method.

Exceptions
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.
getCapturedExceptionsback to summary
public 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:List<CsvException>

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

isThrowExceptionsback to summary
public boolean isThrowExceptions()

Deprecated

There is simply no need for this method.

Returns:boolean

Whether or not exceptions are thrown. If they are not thrown, they are captured and returned later via getCapturedExceptions().

Annotations
@Deprecated
setErrorLocaleback to summary
public void setErrorLocale(Locale errorLocale)

Sets the locale for all error messages.

Parameters
errorLocale:Locale

Locale for error messages. If null, the default locale is used.

Since
4.0
setOrderedResultsback to summary
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:boolean

Whether or not the lines written are in the same order they appeared in the input

Since
4.0
submitAllLinesback to summary
private void submitAllLines(Iterator<T> beans) throws InterruptedException
writeback to summary
public void write(T bean) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

Writes a bean out to the java.io.Writer provided to the constructor.

Parameters
bean:T

A bean to be written to a CSV destination

Exceptions
CsvDataTypeMismatchException:
If a field of the bean is annotated improperly or an unsupported data type is supposed to be written
CsvRequiredFieldEmptyException:
If a field is marked as required, but the source is null
writeback to summary
public void write(List<T> beans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

Writes a list of beans out to the java.io.Writer provided to the constructor.

Parameters
beans:List<T>

A list of beans to be written to a CSV destination

Exceptions
CsvDataTypeMismatchException:
If a field of the beans is annotated improperly or an unsupported data type is supposed to be written
CsvRequiredFieldEmptyException:
If a field is marked as required, but the source is null
writeback to summary
public void write(Iterator<T> iBeans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

Writes an iterator of beans out to the java.io.Writer provided to the constructor.

Parameters
iBeans:Iterator<T>

An iterator of beans to be written to a CSV destination

Exceptions
CsvDataTypeMismatchException:
If a field of the beans is annotated improperly or an unsupported data type is supposed to be written
CsvRequiredFieldEmptyException:
If a field is marked as required, but the source is null
writeback to summary
public void write(Stream<T> beans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException

Writes a stream of beans out to the java.io.Writer provided to the constructor.

Parameters
beans:Stream<T>

A stream of beans to be written to a CSV destination

Exceptions
CsvDataTypeMismatchException:
If a field of the beans is annotated improperly or an unsupported data type is supposed to be written
CsvRequiredFieldEmptyException:
If a field is marked as required, but the source is null