Top Description Fields Constructors Methods
com.opencsv.bean

public Class StatefulBeanToCsvBuilder<T>

extends Object
Class Inheritance
Type Parameters
<T>
The type of the beans being written
Imports
com.opencsv.CSVWriter, .ICSVParser, .ICSVWriter, com.opencsv.bean.exceptionhandler.CsvExceptionHandler, .ExceptionHandlerQueue, .ExceptionHandlerThrow, org.apache.commons.collections4.ListValuedMap, .MultiValuedMap, org.apache.commons.collections4.multimap.ArrayListValuedHashMap, org.apache.commons.lang3.ObjectUtils, .StringUtils, java.io.Writer, java.lang.reflect.Field, java.util.Locale, .ResourceBundle

This is a builder for StatefulBeanToCsv, allowing one to set all parameters necessary for writing a CSV file.
Author
Andrew Rucker Jones
Since
3.9

Field Summary

Modifier and TypeField and Description
private boolean
private final ICSVWriter
private Locale
private char
private CsvExceptionHandler
private final ListValuedMap<Class<?>, Field>
private String
private MappingStrategy<T>
private boolean
private String
private char
private char
private final Writer

Constructor Summary

AccessConstructor and Description
public
StatefulBeanToCsvBuilder(Writer
- the writer that will be used to output the csv version of the bean.
writer
)

Default constructor - Being stateful the writer is required by the builder at the start and not added in later.

public
StatefulBeanToCsvBuilder(ICSVWriter
- the ICSVWriter that will be used to output the csv version of the bean.
icsvWriter
)

Being stateful the writer is required by the builder at the start and not added in later.

Method Summary

Modifier and TypeMethod and Description
public StatefulBeanToCsv<T>

Returns:

A new StatefulBeanToCsv
build
()

Builds a StatefulBeanToCsv from the information provided, filling in default values where none have been specified.

public StatefulBeanToCsvBuilder<T>

Returns:

this
withApplyQuotesToAll
(boolean
Whether all outputs should be quoted
applyQuotesToAll
)

Sets whether all outputs should be put in quotes.

public StatefulBeanToCsvBuilder<T>

Returns:

this
withErrorLocale
(Locale
Locale for error messages. If null, the default locale is used.
errorLocale
)

Sets the locale to be used for all error messages.

public StatefulBeanToCsvBuilder<T>
withEscapechar(char
The escape character to be used when writing a CSV file
escapechar
)

Returns: this

public StatefulBeanToCsvBuilder<T>

Returns:

this
withExceptionHandler
(CsvExceptionHandler
The exception handler to be used. If null, this method does nothing.
exceptionHandler
)

Sets the handler for recoverable exceptions raised during processing of records.

public StatefulBeanToCsvBuilder<T>

Returns:

this
withIgnoreField
(Class<?>
The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.
type
,
Field
The field opencsv is to ignore
field
)

Adds a Field to the list of fields opencsv should ignore completely.

public StatefulBeanToCsvBuilder<T>
withLineEnd(String
The line ending to be used when writing a CSV file
lineEnd
)

Returns: this

public StatefulBeanToCsvBuilder<T>

Returns:

this
withMappingStrategy
(MappingStrategy<T>
The mapping strategy to be used for write operations
mappingStrategy
)

Sets the mapping strategy for writing beans to a CSV destination.

public StatefulBeanToCsvBuilder<T>

Returns:

this
withOrderedResults
(boolean
whether the lines written are in the same order they appeared in the input
orderedResults
)

Sets whether results must be written in the same order in which they appear in the list of beans provided as input.

public StatefulBeanToCsvBuilder<T>

Returns:

this
withProfile
(String
The name of the profile to be used
profile
)

Selects a profile for deciding which configurations to use for the bean fields.

public StatefulBeanToCsvBuilder<T>
withQuotechar(char
The quote character to be used when writing a CSV file
quotechar
)

Returns: this

public StatefulBeanToCsvBuilder<T>
withSeparator(char
The field separator to be used when writing a CSV file
separator
)

Returns: this

public StatefulBeanToCsvBuilder<T>

Returns:

this
withThrowExceptions
(boolean
Whether or not exceptions should be thrown while writing a CSV file. If not, they may be retrieved later by calling com.opencsv.bean.StatefulBeanToCsv#getCapturedExceptions().
throwExceptions
)

Sets the handler for recoverable exceptions that arise during the processing of records.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

applyQuotesToAllback to summary
private boolean applyQuotesToAll
csvWriterback to summary
private final ICSVWriter csvWriter
errorLocaleback to summary
private Locale errorLocale
escapecharback to summary
private char escapechar
exceptionHandlerback to summary
private CsvExceptionHandler exceptionHandler
ignoredFieldsback to summary
private final ListValuedMap<Class<?>, Field> ignoredFields
lineEndback to summary
private String lineEnd
mappingStrategyback to summary
private MappingStrategy<T> mappingStrategy
orderedResultsback to summary
private boolean orderedResults
profileback to summary
private String profile
quotecharback to summary
private char quotechar
separatorback to summary
private char separator
writerback to summary
private final Writer writer

Constructor Detail

StatefulBeanToCsvBuilderback to summary
public StatefulBeanToCsvBuilder(Writer writer)

Default constructor - Being stateful the writer is required by the builder at the start and not added in later.

Parameters
writer:Writer

- the writer that will be used to output the csv version of the bean.

StatefulBeanToCsvBuilderback to summary
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:ICSVWriter

- the ICSVWriter that will be used to output the csv version of the bean.

Since
4.2

Method Detail

buildback to summary
public StatefulBeanToCsv<T> build()

Builds a StatefulBeanToCsv from the information provided, filling in default values where none have been specified.

Returns:StatefulBeanToCsv<T>

A new StatefulBeanToCsv

withApplyQuotesToAllback to summary
public StatefulBeanToCsvBuilder<T> withApplyQuotesToAll(boolean applyQuotesToAll)

Sets whether all outputs should be put in quotes. Defaults to true.

Parameters
applyQuotesToAll:boolean

Whether all outputs should be quoted

Returns:StatefulBeanToCsvBuilder<T>

this

Since
4.2
See Also
com.opencsv.CSVWriter#writeNext(String[], boolean)
withErrorLocaleback to summary
public StatefulBeanToCsvBuilder<T> withErrorLocale(Locale errorLocale)

Sets the locale to be used for all error messages.

Parameters
errorLocale:Locale

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

Returns:StatefulBeanToCsvBuilder<T>

this

Since
4.0
See Also
StatefulBeanToCsv#setErrorLocale(java.util.Locale)
withEscapecharback to summary
public StatefulBeanToCsvBuilder<T> withEscapechar(char escapechar)
Parameters
escapechar:char

The escape character to be used when writing a CSV file

Returns:StatefulBeanToCsvBuilder<T>

this

See Also
com.opencsv.CSVWriter#escapechar
withExceptionHandlerback to summary
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 is ExceptionHandlerThrow.

Please note that if both this method and withThrowExceptions(boolean) are called, the last call wins.

Parameters
exceptionHandler:CsvExceptionHandler

The exception handler to be used. If null, this method does nothing.

Returns:StatefulBeanToCsvBuilder<T>

this

Since
5.2
withIgnoreFieldback to summary
public StatefulBeanToCsvBuilder<T> withIgnoreField(Class<?> type, Field field) throws IllegalArgumentException

Adds a Field to the list of fields opencsv should ignore completely.

May be called as many times as necessary.

Parameters
type:Class<?>

The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.

field:Field

The field opencsv is to ignore

Returns:StatefulBeanToCsvBuilder<T>

this

Exceptions
IllegalArgumentException:
If one of the parameters is null or field cannot be found in type.
Since
5.0
See Also
MappingStrategy#ignoreFields(MultiValuedMap)
withLineEndback to summary
public StatefulBeanToCsvBuilder<T> withLineEnd(String lineEnd)
Parameters
lineEnd:String

The line ending to be used when writing a CSV file

Returns:StatefulBeanToCsvBuilder<T>

this

See Also
com.opencsv.CSVWriter#lineEnd
withMappingStrategyback to summary
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:MappingStrategy<T>

The mapping strategy to be used for write operations

Returns:StatefulBeanToCsvBuilder<T>

this

withOrderedResultsback to summary
public StatefulBeanToCsvBuilder<T> withOrderedResults(boolean orderedResults)

Sets whether results must be written in the same order in which they appear in the list of beans provided as input.

Parameters
orderedResults:boolean

whether the lines written are in the same order they appeared in the input

Returns:StatefulBeanToCsvBuilder<T>

this

Since
4.0
See Also
StatefulBeanToCsv#setOrderedResults(boolean)
withProfileback to summary
public StatefulBeanToCsvBuilder<T> withProfile(String profile)

Selects a profile for deciding which configurations to use for the bean fields.

Parameters
profile:String

The name of the profile to be used

Returns:StatefulBeanToCsvBuilder<T>

this

Since
5.4
withQuotecharback to summary
public StatefulBeanToCsvBuilder<T> withQuotechar(char quotechar)
Parameters
quotechar:char

The quote character to be used when writing a CSV file

Returns:StatefulBeanToCsvBuilder<T>

this

See Also
com.opencsv.CSVWriter#quotechar
withSeparatorback to summary
public StatefulBeanToCsvBuilder<T> withSeparator(char separator)
Parameters
separator:char

The field separator to be used when writing a CSV file

Returns:StatefulBeanToCsvBuilder<T>

this

See Also
com.opencsv.CSVWriter#separator
withThrowExceptionsback to summary
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 to withExceptionHandler(new ExceptionHandlerThrow()) and false is equivalent to withExceptionHandler(new ExceptionHandlerQueue())

Please note that if both this method and withExceptionHandler(CsvExceptionHandler) are called, the last call wins.

Parameters
throwExceptions:boolean

Whether or not exceptions should be thrown while writing a CSV file. If not, they may be retrieved later by calling com.opencsv.bean.StatefulBeanToCsv#getCapturedExceptions().

Returns:StatefulBeanToCsvBuilder<T>

this

See Also
withExceptionHandler(CsvExceptionHandler)