Package com.opencsv.bean
Class FieldMapByPosition<T>
java.lang.Object
com.opencsv.bean.AbstractFieldMap<String,Integer,PositionToBeanField<T>,T>
com.opencsv.bean.FieldMapByPosition<T>
- Type Parameters:
T
- Type of the bean being converted
- All Implemented Interfaces:
FieldMap<String,
,Integer, PositionToBeanField<T>, T> Iterable<FieldMapByPositionEntry<T>>
public class FieldMapByPosition<T>
extends AbstractFieldMap<String,Integer,PositionToBeanField<T>,T>
implements Iterable<FieldMapByPositionEntry<T>>
This class maintains a mapping from column position out of a CSV file to bean
fields.
- Since:
- 4.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate int
private Comparator
<Integer> 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 TypeMethodDescriptionString[]
generateHeader
(T bean) This method generates a header that can be used for writing beans of the type provided back to a file.iterator()
void
putComplex
(String rangeDefinition, BeanField<T, Integer> field) Adds aBeanField
to this map indexed by the data ininitializer
.void
setColumnOrderOnWrite
(Comparator<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
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
maxIndex
private int maxIndex -
writeOrder
Holds aComparator
to sort columns on writing.
-
-
Constructor Details
-
FieldMapByPosition
Initializes thisFieldMap
.- Parameters:
errorLocale
- The locale to be used for error messages
-
-
Method Details
-
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 can be determined with theComparator
passed in tosetColumnOrderOnWrite(Comparator)
. Otherwise, it is ascending according to position.- Specified by:
generateHeader
in interfaceFieldMap<String,
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
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<String,
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:
-
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
-
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
-