T
- Type of object that is being processed.public class ColumnPositionMappingStrategy<T> extends AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
CsvBindByPosition
or
CsvCustomBindByPosition
) requires all the columns
to be present in the CSV file and for them to be in a particular order. Using
annotations allows one to specify arbitrary zero-based column numbers for
each bean member variable to be filled. Also this strategy requires that the
file does NOT have a header. That said, the main use of this strategy is
files that do not have headers.AbstractMappingStrategy.RecursiveType
errorLocale, headerIndex, profile, recursiveTypeTree, type
Constructor and Description |
---|
ColumnPositionMappingStrategy()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
captureHeader(CSVReader reader)
There is no header per se for this mapping strategy, but this method
checks the first line to determine how many fields are present and
adjusts its field map accordingly.
|
protected Integer |
chooseMultivaluedFieldIndexFromHeaderIndex(int index)
For
BeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
it is necessary to determine which index to pass in. |
protected BeanField<T,Integer> |
findField(int col)
Gets the field for a given column position.
|
String |
findHeader(int col)
Returns the column position for the given column number.
|
String[] |
generateHeader(T bean)
This method returns an empty array.
|
protected Set<Class<? extends Annotation>> |
getBindingAnnotations()
Returns a set of the annotations that are used for binding in this
mapping strategy.
|
String[] |
getColumnMapping()
Retrieves the column mappings.
|
String |
getColumnName(int col)
Gets a column name.
|
protected FieldMap<String,Integer,? extends ComplexFieldMapEntry<String,Integer,T>,T> |
getFieldMap()
Returns the
FieldMap associated with this mapping strategy. |
protected void |
initializeFieldMap()
Creates an empty binding-type-specific field map that can be filled in
later steps.
|
protected void |
loadAnnotatedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>,Field> fields)
Creates a map of annotated fields in the bean to be processed.
|
protected void |
loadUnadornedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>,Field> fields)
Creates a map of fields in the bean to be processed that have no
annotations.
|
void |
setColumnMapping(String... columnMapping)
Setter for the column mapping.
|
void |
setColumnOrderOnWrite(Comparator<Integer> writeOrder)
Sets the
Comparator to be used to sort columns when
writing beans to a CSV file. |
protected void |
verifyLineLength(int numberOfFields)
Must be called once the length of input for a line/record is known to
verify that the line was complete.
|
createBean, determineConverter, filterIgnoredFields, getType, ignoreFields, indexBean, instantiateCustomConverter, isForbiddenClassForRecursion, loadFieldMap, loadRecursiveClasses, partitionFields, populateNewBean, selectAnnotationForProfile, setErrorLocale, setFieldValue, setProfile, setType, transmuteBean
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
isAnnotationDriven
public ColumnPositionMappingStrategy()
ColumnPositionMappingStrategyBuilder
public void captureHeader(CSVReader reader) throws IOException
reader
- The CSVReader to use for header parsingIOException
- If parsing failsprotected Integer chooseMultivaluedFieldIndexFromHeaderIndex(int index)
AbstractMappingStrategy
BeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
it is necessary to determine which index to pass in.chooseMultivaluedFieldIndexFromHeaderIndex
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
index
- The current column position while transmuting a bean to CSV
outputBeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
For this mapping strategy, it's simply
index
wrapped as an Integer
.protected BeanField<T,Integer> findField(int col)
AbstractMappingStrategy
findField
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
col
- The column to find the field forpublic String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException
generateHeader
in interface MappingStrategy<T>
generateHeader
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
bean
- One fully populated bean from which the header can be derived.
This is important in the face of joining and splitting. If we have a
MultiValuedMap as a field that is the target for a join on reading, that
same field must be split into multiple columns on writing. Since the
joining is done via regular expressions, it is impossible for opencsv
to know what the column names are supposed to be on writing unless this
bean includes a fully populated map.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.public String getColumnName(int col)
getColumnName
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
col
- Position of the column.public String[] getColumnMapping()
public void setColumnMapping(String... columnMapping)
columnMapping
- Column names to be mapped.protected void loadAnnotatedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>,Field> fields)
This method is called by AbstractMappingStrategy.loadFieldMap()
when at least one
relevant annotation is found on a member variable.
loadAnnotatedFieldMap
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
fields
- A list of fields annotated with a binding annotation
in the bean to be processedprotected void loadUnadornedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>,Field> fields)
AbstractMappingStrategy
AbstractMappingStrategy.loadFieldMap()
when absolutely no
annotations that are relevant for this mapping strategy are found in the
type of bean being processed.loadUnadornedFieldMap
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
fields
- A list of all non-synthetic fields in the bean to be
processedprotected Set<Class<? extends Annotation>> getBindingAnnotations()
In this mapping strategy, those are currently:
getBindingAnnotations
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
CsvBindByName
or
CsvBindByPosition
that are relevant for binding input fields to
bean members in this mapping strategyprotected void initializeFieldMap()
AbstractMappingStrategy
This method may be called multiple times and must erase any state information from previous calls.
initializeFieldMap
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
protected void verifyLineLength(int numberOfFields) throws CsvRequiredFieldEmptyException
AbstractMappingStrategy
verifyLineLength
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
numberOfFields
- The number of fields present in the line of inputCsvRequiredFieldEmptyException
- If a required column is missingpublic String findHeader(int col)
findHeader
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
col
- The column number for which the header is soughtprotected FieldMap<String,Integer,? extends ComplexFieldMapEntry<String,Integer,T>,T> getFieldMap()
AbstractMappingStrategy
FieldMap
associated with this mapping strategy.getFieldMap
in class AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
FieldMap
used by this strategypublic void setColumnOrderOnWrite(Comparator<Integer> writeOrder)
Comparator
to be used to sort columns when
writing beans to a CSV file.
Behavior of this method when used on a mapping strategy intended for
reading data from a CSV source is not defined.writeOrder
- The Comparator
to use. May be
null
, in which case the natural ordering is used.Copyright © 2005–2023. All rights reserved.