Package com.opencsv.bean
Class ColumnPositionMappingStrategy<T>
java.lang.Object
com.opencsv.bean.AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
com.opencsv.bean.ColumnPositionMappingStrategy<T>
- Type Parameters:
T
- Type of object that is being processed.
- All Implemented Interfaces:
MappingStrategy<T>
public class ColumnPositionMappingStrategy<T>
extends AbstractMappingStrategy<String,Integer,ComplexFieldMapEntry<String,Integer,T>,T>
Allows for the mapping of columns with their positions. Using this strategy
without annotations (
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.-
Nested Class Summary
Nested classes/interfaces inherited from class com.opencsv.bean.AbstractMappingStrategy
AbstractMappingStrategy.RecursiveType
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Integer[]
Used to store a mapping from presumed input column index to desired output column index, as determined by applyingwriteOrder
.private boolean
Whether the user has programmatically set the map from column positions to field names.private FieldMapByPosition
<T> The map from column position toBeanField
.private Comparator
<Integer> Holds aComparator
to sort columns on writing.Fields inherited from class com.opencsv.bean.AbstractMappingStrategy
errorLocale, headerIndex, profile, recursiveTypeTree, type
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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) ForBeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
it is necessary to determine which index to pass in.findField
(int col) Gets the field for a given column position.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>> Returns a set of the annotations that are used for binding in this mapping strategy.String[]
Retrieves the column mappings.getColumnName
(int col) Gets a column name.Returns theFieldMap
associated with this mapping strategy.protected void
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.private void
registerBinding
(CsvBindByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a simple converter.private void
registerCustomBinding
(CsvCustomBindByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a custom converter.private void
registerJoinBinding
(CsvBindAndJoinByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a multi-valued converter that joins values from multiple columns.private void
registerSplitBinding
(CsvBindAndSplitByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a collection converter that splits input into multiple values.void
setColumnMapping
(String... columnMapping) Setter for the column mapping.void
setColumnOrderOnWrite
(Comparator<Integer> writeOrder) Sets theComparator
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.Methods inherited from class com.opencsv.bean.AbstractMappingStrategy
createBean, determineConverter, filterIgnoredFields, getType, ignoreFields, indexBean, instantiateCustomConverter, isForbiddenClassForRecursion, loadFieldMap, loadRecursiveClasses, partitionFields, populateNewBean, selectAnnotationForProfile, setErrorLocale, setFieldValue, setProfile, setType, transmuteBean
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.opencsv.bean.MappingStrategy
isAnnotationDriven
-
Field Details
-
columnsExplicitlySet
private boolean columnsExplicitlySetWhether the user has programmatically set the map from column positions to field names. -
fieldMap
The map from column position toBeanField
. -
writeOrder
Holds aComparator
to sort columns on writing. -
columnIndexForWriting
Used to store a mapping from presumed input column index to desired output column index, as determined by applyingwriteOrder
.
-
-
Constructor Details
-
ColumnPositionMappingStrategy
public ColumnPositionMappingStrategy()Default constructor. Considered stable.- See Also:
-
-
Method Details
-
captureHeader
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.- Parameters:
reader
- The CSVReader to use for header parsing- Throws:
IOException
- If parsing fails
-
chooseMultivaluedFieldIndexFromHeaderIndex
Description copied from class:AbstractMappingStrategy
ForBeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
it is necessary to determine which index to pass in.- Specified by:
chooseMultivaluedFieldIndexFromHeaderIndex
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
index
- The current column position while transmuting a bean to CSV output- Returns:
- The index to be used for this mapping strategy for
BeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
For this mapping strategy, it's simplyindex
wrapped as anInteger
.
-
findField
Description copied from class:AbstractMappingStrategy
Gets the field for a given column position.- Specified by:
findField
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
col
- The column to find the field for- Returns:
- BeanField containing the field for a given column position, or null if one could not be found
-
generateHeader
This method returns an empty array. The column position mapping strategy assumes that there is no header, and thus it also does not write one, accordingly.- Specified by:
generateHeader
in interfaceMappingStrategy<T>
- Overrides:
generateHeader
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
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.- Returns:
- An empty array
- 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.
-
getColumnName
Gets a column name.- Overrides:
getColumnName
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
col
- Position of the column.- Returns:
- Column name or null if col > number of mappings.
-
getColumnMapping
Retrieves the column mappings.- Returns:
- String array with the column mappings.
-
setColumnMapping
Setter for the column mapping. This mapping is for reading. Use of this method in conjunction with writing is undefined.- Parameters:
columnMapping
- Column names to be mapped.
-
registerCustomBinding
private void registerCustomBinding(CsvCustomBindByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a custom converter.- Parameters:
annotation
- The annotation attached to the bean fieldlocalType
- The class/type in which the field resideslocalField
- The bean field
-
registerSplitBinding
private void registerSplitBinding(CsvBindAndSplitByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a collection converter that splits input into multiple values.- Parameters:
annotation
- The annotation attached to the bean fieldlocalType
- The class/type in which the field resideslocalField
- The bean field
-
registerJoinBinding
private void registerJoinBinding(CsvBindAndJoinByPosition annotation, Class<?> localType, Field localField) Register a binding between a bean field and a multi-valued converter that joins values from multiple columns.- Parameters:
annotation
- The annotation attached to the bean fieldlocalType
- The class/type in which the field resideslocalField
- The bean field
-
registerBinding
Register a binding between a bean field and a simple converter.- Parameters:
annotation
- The annotation attached to the bean fieldlocalType
- The class/type in which the field resideslocalField
- The bean field
-
loadAnnotatedFieldMap
protected void loadAnnotatedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>, Field> fields) Creates a map of annotated fields in the bean to be processed.This method is called by
AbstractMappingStrategy.loadFieldMap()
when at least one relevant annotation is found on a member variable.- Overrides:
loadAnnotatedFieldMap
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
fields
- A list of fields annotated with a binding annotation in the bean to be processed
-
loadUnadornedFieldMap
protected void loadUnadornedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>, Field> fields) Description copied from class:AbstractMappingStrategy
Creates a map of fields in the bean to be processed that have no annotations. This method is called byAbstractMappingStrategy.loadFieldMap()
when absolutely no annotations that are relevant for this mapping strategy are found in the type of bean being processed.- Specified by:
loadUnadornedFieldMap
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
fields
- A list of all non-synthetic fields in the bean to be processed
-
getBindingAnnotations
Returns a set of the annotations that are used for binding in this mapping strategy.In this mapping strategy, those are currently:
- Overrides:
getBindingAnnotations
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Returns:
- Annotations of the sort
CsvBindByName
orCsvBindByPosition
that are relevant for binding input fields to bean members in this mapping strategy
-
initializeFieldMap
protected void initializeFieldMap()Description copied from class:AbstractMappingStrategy
Creates an empty binding-type-specific field map that can be filled in later steps.This method may be called multiple times and must erase any state information from previous calls.
- Specified by:
initializeFieldMap
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T>
-
verifyLineLength
Description copied from class:AbstractMappingStrategy
Must be called once the length of input for a line/record is known to verify that the line was complete. Complete in this context means, no required fields are missing. The issue here is, as long as a column is present but empty, we can check whether the field is required and throw an exception if it is not, but if the data end prematurely, we never have this chance without indication that no more data are on the way. Another validation is that the number of fields must match the number of headers to prevent a data mismatch situation.- Specified by:
verifyLineLength
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
numberOfFields
- The number of fields present in the line of input- Throws:
CsvRequiredFieldEmptyException
- If a required column is missing
-
findHeader
Returns the column position for the given column number. Yes, they're the same thing. For this mapping strategy, it's a simple conversion from an integer to a string.- Specified by:
findHeader
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Parameters:
col
- The column number for which the header is sought- Returns:
- The name of the header
-
getFieldMap
Description copied from class:AbstractMappingStrategy
Returns theFieldMap
associated with this mapping strategy.- Specified by:
getFieldMap
in classAbstractMappingStrategy<String,
Integer, ComplexFieldMapEntry<String, Integer, T>, T> - Returns:
- The
FieldMap
used by this strategy
-
setColumnOrderOnWrite
Sets theComparator
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.- Parameters:
writeOrder
- TheComparator
to use. May benull
, in which case the natural ordering is used.- Since:
- 4.3
-