Class StatefulBeanToCsvBuilder<T>
- java.lang.Object
-
- com.opencsv.bean.StatefulBeanToCsvBuilder<T>
-
- Type Parameters:
T
- The type of the beans being written
public class StatefulBeanToCsvBuilder<T> extends java.lang.Object
This is a builder for StatefulBeanToCsv, allowing one to set all parameters necessary for writing a CSV file.- Since:
- 3.9
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
applyQuotesToAll
private ICSVWriter
csvWriter
private java.util.Locale
errorLocale
private char
escapechar
private CsvExceptionHandler
exceptionHandler
private org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,java.lang.reflect.Field>
ignoredFields
private java.lang.String
lineEnd
private MappingStrategy<T>
mappingStrategy
private boolean
orderedResults
private java.lang.String
profile
private char
quotechar
private char
separator
private java.io.Writer
writer
-
Constructor Summary
Constructors Constructor Description StatefulBeanToCsvBuilder(ICSVWriter icsvWriter)
Being stateful the writer is required by the builder at the start and not added in later.StatefulBeanToCsvBuilder(java.io.Writer writer)
Default constructor - Being stateful the writer is required by the builder at the start and not added in later.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StatefulBeanToCsv<T>
build()
Builds a StatefulBeanToCsv from the information provided, filling in default values where none have been specified.StatefulBeanToCsvBuilder<T>
withApplyQuotesToAll(boolean applyQuotesToAll)
Sets whether all outputs should be put in quotes.StatefulBeanToCsvBuilder<T>
withErrorLocale(java.util.Locale errorLocale)
Sets the locale to be used for all error messages.StatefulBeanToCsvBuilder<T>
withEscapechar(char escapechar)
StatefulBeanToCsvBuilder<T>
withExceptionHandler(CsvExceptionHandler exceptionHandler)
Sets the handler for recoverable exceptions raised during processing of records.StatefulBeanToCsvBuilder<T>
withIgnoreField(java.lang.Class<?> type, java.lang.reflect.Field field)
Adds aField
to the list of fields opencsv should ignore completely.StatefulBeanToCsvBuilder<T>
withLineEnd(java.lang.String lineEnd)
StatefulBeanToCsvBuilder<T>
withMappingStrategy(MappingStrategy<T> mappingStrategy)
Sets the mapping strategy for writing beans to a CSV destination.StatefulBeanToCsvBuilder<T>
withOrderedResults(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.StatefulBeanToCsvBuilder<T>
withProfile(java.lang.String profile)
Selects a profile for deciding which configurations to use for the bean fields.StatefulBeanToCsvBuilder<T>
withQuotechar(char quotechar)
StatefulBeanToCsvBuilder<T>
withSeparator(char separator)
StatefulBeanToCsvBuilder<T>
withThrowExceptions(boolean throwExceptions)
Sets the handler for recoverable exceptions that arise during the processing of records.
-
-
-
Field Detail
-
separator
private char separator
-
quotechar
private char quotechar
-
escapechar
private char escapechar
-
lineEnd
private java.lang.String lineEnd
-
mappingStrategy
private MappingStrategy<T> mappingStrategy
-
writer
private final java.io.Writer writer
-
csvWriter
private final ICSVWriter csvWriter
-
exceptionHandler
private CsvExceptionHandler exceptionHandler
-
orderedResults
private boolean orderedResults
-
errorLocale
private java.util.Locale errorLocale
-
applyQuotesToAll
private boolean applyQuotesToAll
-
ignoredFields
private final org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields
-
profile
private java.lang.String profile
-
-
Constructor Detail
-
StatefulBeanToCsvBuilder
public StatefulBeanToCsvBuilder(java.io.Writer writer)
Default constructor - Being stateful the writer is required by the builder at the start and not added in later.- Parameters:
writer
- - the writer that will be used to output the csv version of the bean.
-
StatefulBeanToCsvBuilder
public StatefulBeanToCsvBuilder(ICSVWriter icsvWriter)
Being stateful the writer is required by the builder at the start and not added in later. By passing in the ICSVWriter you can create a writer with the desired ICSVParser to allow you to use the exact same parser for reading and writing.- Parameters:
icsvWriter
- - the ICSVWriter that will be used to output the csv version of the bean.- Since:
- 4.2
-
-
Method Detail
-
withMappingStrategy
public StatefulBeanToCsvBuilder<T> withMappingStrategy(MappingStrategy<T> mappingStrategy)
Sets the mapping strategy for writing beans to a CSV destination.If the mapping strategy is set this way, it will always be used instead of automatic determination of an appropriate mapping strategy.
It is perfectly legitimate to read a CSV source, take the mapping strategy from the read operation, and pass it in to this method for a write operation. This conserves some processing time, but, more importantly, preserves header ordering.
- Parameters:
mappingStrategy
- The mapping strategy to be used for write operations- Returns:
- this
-
withSeparator
public StatefulBeanToCsvBuilder<T> withSeparator(char separator)
- Parameters:
separator
- The field separator to be used when writing a CSV file- Returns:
- this
- See Also:
CSVWriter.separator
-
withQuotechar
public StatefulBeanToCsvBuilder<T> withQuotechar(char quotechar)
- Parameters:
quotechar
- The quote character to be used when writing a CSV file- Returns:
- this
- See Also:
CSVWriter.quotechar
-
withEscapechar
public StatefulBeanToCsvBuilder<T> withEscapechar(char escapechar)
- Parameters:
escapechar
- The escape character to be used when writing a CSV file- Returns:
- this
- See Also:
CSVWriter.escapechar
-
withLineEnd
public StatefulBeanToCsvBuilder<T> withLineEnd(java.lang.String lineEnd)
- Parameters:
lineEnd
- The line ending to be used when writing a CSV file- Returns:
- this
- See Also:
AbstractCSVWriter.lineEnd
-
withThrowExceptions
public StatefulBeanToCsvBuilder<T> withThrowExceptions(boolean throwExceptions)
Sets the handler for recoverable exceptions that arise during the processing of records.This is a convenience function and is maintained for backwards compatibility. Passing in
true
is equivalent towithExceptionHandler(new ExceptionHandlerThrow())
andfalse
is equivalent towithExceptionHandler(new ExceptionHandlerQueue())
Please note that if both this method and
withExceptionHandler(CsvExceptionHandler)
are called, the last call wins.- Parameters:
throwExceptions
- Whether or not exceptions should be thrown while writing a CSV file. If not, they may be retrieved later by callingStatefulBeanToCsv.getCapturedExceptions()
.- Returns:
- this
- See Also:
withExceptionHandler(CsvExceptionHandler)
-
withExceptionHandler
public StatefulBeanToCsvBuilder<T> withExceptionHandler(CsvExceptionHandler exceptionHandler)
Sets the handler for recoverable exceptions raised during processing of records.If neither this method nor
withThrowExceptions(boolean)
is called, the default exception handler isExceptionHandlerThrow
.Please note that if both this method and
withThrowExceptions(boolean)
are called, the last call wins.- Parameters:
exceptionHandler
- The exception handler to be used. Ifnull
, this method does nothing.- Returns:
this
- Since:
- 5.2
-
withOrderedResults
public StatefulBeanToCsvBuilder<T> withOrderedResults(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.- Parameters:
orderedResults
- Whether or not the lines written are in the same order they appeared in the input- Returns:
- this
- Since:
- 4.0
- See Also:
StatefulBeanToCsv.setOrderedResults(boolean)
-
withErrorLocale
public StatefulBeanToCsvBuilder<T> withErrorLocale(java.util.Locale errorLocale)
Sets the locale to be used for all error messages.- Parameters:
errorLocale
- Locale for error messages. If null, the default locale is used.- Returns:
- this
- Since:
- 4.0
- See Also:
StatefulBeanToCsv.setErrorLocale(java.util.Locale)
-
withApplyQuotesToAll
public StatefulBeanToCsvBuilder<T> withApplyQuotesToAll(boolean applyQuotesToAll)
Sets whether all outputs should be put in quotes. Defaults totrue
.- Parameters:
applyQuotesToAll
- Whether all outputs should be quoted- Returns:
- this
- Since:
- 4.2
- See Also:
AbstractCSVWriter.writeNext(String[], boolean)
-
withIgnoreField
public StatefulBeanToCsvBuilder<T> withIgnoreField(java.lang.Class<?> type, java.lang.reflect.Field field) throws java.lang.IllegalArgumentException
Adds aField
to the list of fields opencsv should ignore completely.May be called as many times as necessary.
- Parameters:
type
- The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.field
- The field opencsv is to ignore- Returns:
this
- Throws:
java.lang.IllegalArgumentException
- If one of the parameters isnull
orfield
cannot be found intype
.- Since:
- 5.0
- See Also:
MappingStrategy.ignoreFields(MultiValuedMap)
-
withProfile
public StatefulBeanToCsvBuilder<T> withProfile(java.lang.String profile)
Selects a profile for deciding which configurations to use for the bean fields.- Parameters:
profile
- The name of the profile to be used- Returns:
this
- Since:
- 5.4
-
build
public StatefulBeanToCsv<T> build()
Builds a StatefulBeanToCsv from the information provided, filling in default values where none have been specified.- Returns:
- A new
StatefulBeanToCsv
-
-