Top Description Methods
com.opencsv.bean

public Interface MappingStrategy<T>

Known Direct Implementers
com.opencsv.bean.AbstractMappingStrategy
Type Parameters
<T>
Type of object you are converting the data to.
Imports
com.opencsv.CSVReader, com.opencsv.exceptions.*, org.apache.commons.collections4.MultiValuedMap, java.io.IOException, java.lang.reflect.Field, java.util.Locale

The interface for the classes that handle translating between the columns in the CSV file to an actual object.

Any implementing class must be thread-safe. Specifically, the following methods must be thread-safe:

Method Summary

Modifier and TypeMethod and Description
public void
captureHeader(CSVReader
The CSVReader to use for header parsing
reader
)

Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.

public String[]

Returns:

An array of column names for a header. This may be an empty array if no header should be written, but it must not be null.
generateHeader
(T
One fully populated bean from which the header can be derived. This is important in the face of joining and splitting. If we have a MultiValuedMap as a field that is the target for a join on reading, that same field must be split into multiple columns on writing. Since the joining is done via regular expressions, it is impossible for opencsv to know what the column names are supposed to be on writing unless this bean includes a fully populated map.
bean
)

Implementations of this method must return an array of column headers based on the contents of the mapping strategy.

public default void
ignoreFields(MultiValuedMap<Class<?>, Field>
All fields to be ignored, mapped from the classes of which they are members. These are the classes as opencsv encounters them, not necessarily the declaring classes if any fields are inherited. May be null.
fields
)

When processing a bean for reading or writing, ignore the given fields from the given classes completely, including all annotations and requirements.

public default boolean

Returns:

Whether the mapping strategy is driven by annotations
isAnnotationDriven
()
Deprecated This is simply no longer necessary.

Determines whether the mapping strategy is driven by annotations.

public T

Returns:

A bean containing the converted information from the input
populateNewBean
(String[]
A line of input returned from com.opencsv.CSVReader
line
)

Takes a line of input from a CSV file and creates a bean out of it.

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

Sets the locale for all error messages.

public default void
setProfile(String
The profile to use
profile
)

Sets the profile this mapping strategy will use when configuring bean fields.

public void
setType(Class<? extends T>
Class type.
type
)

Sets the class type that is being mapped.

public String[]

Returns:

The converted values of the bean fields in the correct order, ready to be passed to a com.opencsv.CSVWriter
transmuteBean
(T
The bean to be transmuted
bean
)

Transmutes a bean instance into an array of Strings to be written to a CSV file.

Method Detail

captureHeaderback to summary
public void captureHeader(CSVReader reader) throws IOException, CsvRequiredFieldEmptyException

Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.

Parameters
reader:CSVReader

The CSVReader to use for header parsing

Exceptions
IOException:
If parsing fails
CsvRequiredFieldEmptyException:
If a field is required, but the header or column position for the field is not present in the input
generateHeaderback to summary
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException

Implementations of this method must return an array of column headers based on the contents of the mapping strategy. If no header can or should be generated, an array of zero length must be returned, and not null.

Parameters
bean:T

One fully populated bean from which the header can be derived. This is important in the face of joining and splitting. If we have a MultiValuedMap as a field that is the target for a join on reading, that same field must be split into multiple columns on writing. Since the joining is done via regular expressions, it is impossible for opencsv to know what the column names are supposed to be on writing unless this bean includes a fully populated map.

Returns:String[]

An array of column names for a header. This may be an empty array if no header should be written, but it must not be null.

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.
Since
3.9
ignoreFieldsback to summary
public default void ignoreFields(MultiValuedMap<Class<?>, Field> fields) throws IllegalArgumentException

When processing a bean for reading or writing, ignore the given fields from the given classes completely, including all annotations and requirements. This method has two compelling applications:

  1. If you are not able to modify the source code of the beans you use, or
  2. If you use a mapping strategy without annotations and want to exclude a small number of fields from a bean with a large number of fields.

Calling this method overwrites the fields passed in from any previous calls. It is legal to call this method before calling setType(Class), and it may be more efficient to do so.

Caution must be exercised with this method when letting opencsv automatically determine the mapping strategy. When a field is ignored, opencsv pretends it does not exist at all. If, for instance, all fields annotated with opencsv binding annotations are ignored, opencsv will automatically switch to HeaderColumnNameMappingStrategy and assume header names exactly match field names.

An implementation of this interface is not required to implement this method. The default implementation throws UnsupportedOperationException.

Parameters
fields:MultiValuedMap<Class<?>, Field>

All fields to be ignored, mapped from the classes of which they are members. These are the classes as opencsv encounters them, not necessarily the declaring classes if any fields are inherited. May be null.

Exceptions
IllegalArgumentException:
If any entry in the map has a null key, a null value, or if the value is not a field in the class represented by the key
Since
5.0
See Also
CsvIgnore
isAnnotationDrivenback to summary
public default boolean isAnnotationDriven()

Deprecated

This is simply no longer necessary.

Determines whether the mapping strategy is driven by annotations.

Returns:boolean

Whether the mapping strategy is driven by annotations

Annotations
@Deprecated
populateNewBeanback to summary
public T populateNewBean(String[] line) throws CsvBeanIntrospectionException, CsvFieldAssignmentException, CsvChainedException

Takes a line of input from a CSV file and creates a bean out of it.

Parameters
line:String[]

A line of input returned from com.opencsv.CSVReader

Returns:T

A bean containing the converted information from the input

Exceptions
CsvBeanIntrospectionException:
Generally, if some part of the bean cannot be accessed and used as needed
CsvFieldAssignmentException:
A more specific subclass of this exception is thrown for any problem decoding and assigning a field of the input to a bean field
CsvChainedException:
If multiple exceptions are thrown for the same input line
Since
4.2
setErrorLocaleback to summary
public default void setErrorLocale(Locale errorLocale)

Sets the locale for all error messages. The default implementation does nothing, as it is expected that most implementations of this interface will not support multiple languages.

Parameters
errorLocale:Locale

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

Since
4.0
setProfileback to summary
public default void setProfile(String profile)

Sets the profile this mapping strategy will use when configuring bean fields.

The default implementation throws UnsupportedOperationException.

Parameters
profile:String

The profile to use

Since
5.4
setTypeback to summary
public void setType(Class<? extends T> type) throws CsvBadConverterException

Sets the class type that is being mapped. May perform additional initialization tasks. If instantiating a mapping strategy, this method should be called after any other initialization, for example a call to setErrorLocale(Locale) or setProfile(String).

Parameters
type:Class<? extends T>

Class type.

Exceptions
CsvBadConverterException:
If a field in the bean is annotated with a custom converter that cannot be initialized. If you are not using custom converters that you have written yourself, it should be safe to catch this exception and ignore it.
transmuteBeanback to summary
public String[] transmuteBean(T bean) throws CsvFieldAssignmentException, CsvChainedException

Transmutes a bean instance into an array of Strings to be written to a CSV file.

Parameters
bean:T

The bean to be transmuted

Returns:String[]

The converted values of the bean fields in the correct order, ready to be passed to a com.opencsv.CSVWriter

Exceptions
CsvFieldAssignmentException:
A more specific subclass of this exception is thrown for any problem decoding and assigning a field of the input to a bean field
CsvChainedException:
If multiple exceptions are thrown for the same input line