Package com.opencsv.bean
Class FieldMapByName<T>
java.lang.Object
com.opencsv.bean.AbstractFieldMap<String,String,RegexToBeanField<T>,T>
com.opencsv.bean.FieldMapByName<T>
- Type Parameters:
T
- Type of the bean being converted
- All Implemented Interfaces:
FieldMap<String,
String, RegexToBeanField<T>, T>
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.
- Since:
- 4.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Comparator
<String> Holds aComparator
to sort columns on writing.Fields inherited from class com.opencsv.bean.AbstractFieldMap
complexMapList, errorLocale, simpleMap
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondetermineMissingRequiredHeaders
(String[] headersPresent) Returns a list of required headers that were not present in the input.String[]
generateHeader
(T bean) This method generates a header that can be used for writing beans of the type provided back to a file.void
putComplex
(String key, BeanField<T, String> value) Adds aBeanField
to this map indexed by the data ininitializer
.void
setColumnOrderOnWrite
(Comparator<String> writeOrder) Sets theComparator
to be used to sort columns when writing beans to a CSV file.Methods inherited from class com.opencsv.bean.AbstractFieldMap
get, put, setErrorLocale, values
-
Field Details
-
writeOrder
Holds aComparator
to sort columns on writing.
-
-
Constructor Details
-
FieldMapByName
Initializes thisFieldMap
.- Parameters:
errorLocale
- The locale to be used for error messages
-
-
Method Details
-
putComplex
Description copied from interface:FieldMap
Adds aBeanField
to this map indexed by the data ininitializer
. This is what makes this map special: It allows one to define characteristics of a method to match multiple keys.- Parameters:
key
- A regular expression matching header namesvalue
- TheBeanField
that is to be returned on a later match
-
determineMissingRequiredHeaders
Returns a list of required headers that were not present in the input.- Parameters:
headersPresent
- An array of all headers present from the input- Returns:
- A list of name + field for all the required headers that were not found
-
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
Comparator
passed in tosetColumnOrderOnWrite(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:
- 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.
- 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.
- Parameters:
bean
- One perfect, shining example of how the bean to be written should look. The most crucial thing is, for fields that result from joining multiple fields on reading and thus need to be split on writing, theMultiValuedMap
in question must have the complete structure of the header to be generated, even if some values are empty.- Returns:
- An array of header names for the output file
- Throws:
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.
-
setColumnOrderOnWrite
Sets theComparator
to be used to sort columns when writing beans to a CSV file.- Parameters:
writeOrder
- TheComparator
to use. May benull
, in which case the natural ordering is used.- Since:
- 4.3
-