Package com.opencsv.bean
Class FieldMapByName<T>
- java.lang.Object
-
- com.opencsv.bean.AbstractFieldMap<java.lang.String,java.lang.String,RegexToBeanField<T>,T>
-
- com.opencsv.bean.FieldMapByName<T>
-
- Type Parameters:
T
- Type of the bean being converted
- All Implemented Interfaces:
FieldMap<java.lang.String,java.lang.String,RegexToBeanField<T>,T>
public class FieldMapByName<T> extends AbstractFieldMap<java.lang.String,java.lang.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
- Author:
- Andrew Rucker Jones
-
-
Field Summary
-
Fields inherited from class com.opencsv.bean.AbstractFieldMap
complexMapList, errorLocale, simpleMap
-
-
Constructor Summary
Constructors Constructor Description FieldMapByName(java.util.Locale errorLocale)
Initializes thisFieldMap
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.List<FieldMapByNameEntry<T>>
determineMissingRequiredHeaders(java.lang.String[] headersPresent)
Returns a list of required headers that were not present in the input.java.lang.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(java.lang.String key, BeanField<T,java.lang.String> value)
Adds aBeanField
to this map indexed by the data ininitializer
.void
setColumnOrderOnWrite(java.util.Comparator<java.lang.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
-
-
-
-
Constructor Detail
-
FieldMapByName
public FieldMapByName(java.util.Locale errorLocale)
Initializes thisFieldMap
.- Parameters:
errorLocale
- The locale to be used for error messages
-
-
Method Detail
-
putComplex
public void putComplex(java.lang.String key, BeanField<T,java.lang.String> value)
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
public java.util.List<FieldMapByNameEntry<T>> determineMissingRequiredHeaders(java.lang.String[] headersPresent)
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 of the required headers that were not found
-
generateHeader
public java.lang.String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException
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
public void setColumnOrderOnWrite(java.util.Comparator<java.lang.String> writeOrder)
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
-
-