Package com.opencsv.bean
Class FieldMapByPosition<T>
- java.lang.Object
-
- com.opencsv.bean.AbstractFieldMap<java.lang.String,java.lang.Integer,PositionToBeanField<T>,T>
-
- com.opencsv.bean.FieldMapByPosition<T>
-
- Type Parameters:
T
- Type of the bean being converted
- All Implemented Interfaces:
FieldMap<java.lang.String,java.lang.Integer,PositionToBeanField<T>,T>
,java.lang.Iterable<FieldMapByPositionEntry<T>>
public class FieldMapByPosition<T> extends AbstractFieldMap<java.lang.String,java.lang.Integer,PositionToBeanField<T>,T> implements java.lang.Iterable<FieldMapByPositionEntry<T>>
This class maintains a mapping from column position out of a CSV file to bean fields.- 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 FieldMapByPosition(java.util.Locale errorLocale)
Initializes thisFieldMap
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.java.util.Iterator<FieldMapByPositionEntry<T>>
iterator()
void
putComplex(java.lang.String rangeDefinition, BeanField<T,java.lang.Integer> field)
Adds aBeanField
to this map indexed by the data ininitializer
.void
setColumnOrderOnWrite(java.util.Comparator<java.lang.Integer> writeOrder)
Sets theComparator
to be used to sort columns when writing beans to a CSV file.void
setMaxIndex(int maxIndex)
Sets the maximum index for all ranges specified in the entire field map.-
Methods inherited from class com.opencsv.bean.AbstractFieldMap
get, put, setErrorLocale, values
-
-
-
-
Constructor Detail
-
FieldMapByPosition
public FieldMapByPosition(java.util.Locale errorLocale)
Initializes thisFieldMap
.- Parameters:
errorLocale
- The locale to be used for error messages
-
-
Method Detail
-
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 can be determined with theComparator
passed in tosetColumnOrderOnWrite(Comparator)
. Otherwise, it is ascending according to position.- Specified by:
generateHeader
in interfaceFieldMap<java.lang.String,java.lang.Integer,PositionToBeanField<T>,T>
- 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.
-
putComplex
public void putComplex(java.lang.String rangeDefinition, BeanField<T,java.lang.Integer> field)
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.- Specified by:
putComplex
in interfaceFieldMap<java.lang.String,java.lang.Integer,PositionToBeanField<T>,T>
- Parameters:
rangeDefinition
- A string describing the column positions to be matched.field
- TheBeanField
that is to be returned on a later match- See Also:
CsvBindAndJoinByPosition.position()
-
setMaxIndex
public void setMaxIndex(int maxIndex)
Sets the maximum index for all ranges specified in the entire field map. No ranges or mappings are ever removed so as to preserve information about required fields, but upper boundries are shortened as much as possible. If ranges or individual column positions were specified that lie wholly abovemaxIndex
, these are preserved, though ranges are shortened to a single value (the lower boundry).- Parameters:
maxIndex
- The maximum index in the data being imported
-
iterator
public java.util.Iterator<FieldMapByPositionEntry<T>> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<T>
-
setColumnOrderOnWrite
public void setColumnOrderOnWrite(java.util.Comparator<java.lang.Integer> 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
-
-