Top Description Fields Constructors Methods
com.opencsv.bean

public Class FieldMapByName<T>

extends AbstractFieldMap<String, String, RegexToBeanField<T>, T>
Class Inheritance
Type Parameters
<T>
Type of the bean being converted
Imports
com.opencsv.ICSVParser, com.opencsv.exceptions.CsvRequiredFieldEmptyException, org.apache.commons.collections4.MultiValuedMap, org.apache.commons.lang3.ArrayUtils, java.lang.reflect.Field, java.util.*, java.util.stream.Collectors

This class maintains a mapping from header names out of a CSV file to bean fields. Simple entries are matched using string equality. Complex entries are matched using regular expressions.
Author
Andrew Rucker Jones
Since
4.2

Field Summary

Modifier and TypeField and Description
private Comparator<String>
writeOrder

Holds a java.util.Comparator to sort columns on writing.

Inherited from com.opencsv.bean.AbstractFieldMap:
complexMapListerrorLocalesimpleMap

Constructor Summary

AccessConstructor and Description
public
FieldMapByName(final Locale
The locale to be used for error messages
errorLocale
)

Initializes this FieldMap.

Method Summary

Modifier and TypeMethod and Description
public List<FieldMapByNameEntry<T>>

Returns:

A list of name + field for all the required headers that were not found
determineMissingRequiredHeaders
(final String[]
An array of all headers present from the input
headersPresent
)

Returns a list of required headers that were not present in the input.

public String[]
generateHeader(final T bean)

Implements com.opencsv.bean.FieldMap.generateHeader.

This method generates a header that can be used for writing beans of the type provided back to a file.

public void
putComplex(final String
A regular expression matching header names
key
,
final BeanField<T, String> value)

Implements com.opencsv.bean.FieldMap.putComplex.

Adds a BeanField to this map indexed by the data in initializer.

public void
setColumnOrderOnWrite(Comparator<String>
The java.util.Comparator to use. May be null, in which case the natural ordering is used.
writeOrder
)

Sets the java.util.Comparator to be used to sort columns when writing beans to a CSV file.

Inherited from com.opencsv.bean.AbstractFieldMap:
getputsetErrorLocalevalues

Field Detail

writeOrderback to summary
private Comparator<String> writeOrder

Holds a java.util.Comparator to sort columns on writing.

Constructor Detail

FieldMapByNameback to summary
public FieldMapByName(final Locale errorLocale)

Initializes this FieldMap.

Parameters
errorLocale:Locale

The locale to be used for error messages

Method Detail

determineMissingRequiredHeadersback to summary
public List<FieldMapByNameEntry<T>> determineMissingRequiredHeaders(final String[] headersPresent)

Returns a list of required headers that were not present in the input.

Parameters
headersPresent:String[]

An array of all headers present from the input

Returns:List<FieldMapByNameEntry<T>>

A list of name + field for all the required headers that were not found

generateHeaderback to summary
public String[] generateHeader(final T bean) throws CsvRequiredFieldEmptyException

Implements com.opencsv.bean.FieldMap.generateHeader.

This method generates a header that can be used for writing beans of the type provided back to a file.

The ordering of the headers is determined by the java.util.Comparator passed in to setColumnOrderOnWrite(Comparator), should that method be called, otherwise the natural ordering is used (alphabetically ascending).

This implementation will not write headers discovered in multi-valued bean fields if the headers would not be matched by the bean field on reading. There are two reasons for this:

  1. opencsv always tries to create data that are round-trip equivalent, and that would not be the case if it generated data on writing that it would discard on reading.
  2. As the code is currently written, the header name is used on writing each bean field to determine the appropriate BeanField for information concerning conversions, locales, necessity (whether or not the field is required). Without this information, conversion is impossible, and every value written under the unmatched header is blank, regardless of the contents of the bean.
Annotations
@Override
Exceptions
CsvRequiredFieldEmptyException:

Doc from com.opencsv.bean.FieldMap.generateHeader.

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.

putComplexback to summary
public void putComplex(final String key, final BeanField<T, String> value)

Implements com.opencsv.bean.FieldMap.putComplex.

Doc from com.opencsv.bean.FieldMap.putComplex.

Adds a BeanField to this map indexed by the data in initializer. This is what makes this map special: It allows one to define characteristics of a method to match multiple keys.

Parameters
key:String

A regular expression matching header names

Annotations
@Override
setColumnOrderOnWriteback to summary
public void setColumnOrderOnWrite(Comparator<String> writeOrder)

Sets the java.util.Comparator to be used to sort columns when writing beans to a CSV file.

Parameters
writeOrder:Comparator<String>

The java.util.Comparator to use. May be null, in which case the natural ordering is used.

Since
4.3