Package com.opencsv.bean
Class ColumnPositionMappingStrategy<T>
- java.lang.Object
-
- com.opencsv.bean.AbstractMappingStrategy<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
Allows for the mapping of columns with their positions. Using this strategy without annotations (CsvBindByPosition
orCsvCustomBindByPosition
) 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.
-
-
Field Summary
-
Fields inherited from class com.opencsv.bean.AbstractMappingStrategy
errorLocale, headerIndex, type
-
-
Constructor Summary
Constructors Constructor Description ColumnPositionMappingStrategy()
Default constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 java.lang.Integer
chooseMultivaluedFieldIndexFromHeaderIndex(int index)
ForBeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)
it is necessary to determine which index to pass in.protected BeanField<T,java.lang.Integer>
findField(int col)
Gets the field for a given column position.java.lang.String
findHeader(int col)
Returns the column position for the given column number.java.lang.String[]
generateHeader(T bean)
This method returns an empty array.protected java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>>
getBindingAnnotations()
Returns a set of the annotations that are used for binding in this mapping strategy.java.lang.String[]
getColumnMapping()
Retrieves the column mappings.java.lang.String
getColumnName(int col)
Gets a column name.protected FieldMap<java.lang.String,java.lang.Integer,? extends ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
getFieldMap()
Returns theFieldMap
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<java.lang.Class<?>,java.lang.reflect.Field> fields)
Creates a map of annotated fields in the bean to be processed.protected void
loadUnadornedFieldMap(org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,java.lang.reflect.Field> fields)
Creates a map of fields in the bean to be processed that have no annotations.void
setColumnMapping(java.lang.String... columnMapping)
Setter for the column mapping.void
setColumnOrderOnWrite(java.util.Comparator<java.lang.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, getType, ignoreFields, indexBean, instantiateCustomConverter, loadFieldMap, loadRecursiveClasses, partitionFields, populateNewBean, setErrorLocale, setFieldValue, 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
-
-
-
-
Method Detail
-
captureHeader
public void captureHeader(CSVReader reader) throws java.io.IOException
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:
java.io.IOException
- If parsing fails
-
chooseMultivaluedFieldIndexFromHeaderIndex
protected java.lang.Integer chooseMultivaluedFieldIndexFromHeaderIndex(int index)
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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.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
protected BeanField<T,java.lang.Integer> findField(int col)
Description copied from class:AbstractMappingStrategy
Gets the field for a given column position.- Specified by:
findField
in classAbstractMappingStrategy<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.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
public java.lang.String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException
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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.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
public java.lang.String getColumnName(int col)
Gets a column name.- Parameters:
col
- Position of the column.- Returns:
- Column name or null if col > number of mappings.
-
getColumnMapping
public java.lang.String[] getColumnMapping()
Retrieves the column mappings.- Returns:
- String array with the column mappings.
-
setColumnMapping
public void setColumnMapping(java.lang.String... columnMapping)
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.
-
loadAnnotatedFieldMap
protected void loadAnnotatedFieldMap(org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,java.lang.reflect.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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.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<java.lang.Class<?>,java.lang.reflect.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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
- Parameters:
fields
- A list of all non-synthetic fields in the bean to be processed
-
getBindingAnnotations
protected java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> 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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
-
verifyLineLength
protected void verifyLineLength(int numberOfFields) throws CsvRequiredFieldEmptyException
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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
- Parameters:
numberOfFields
- The number of fields present in the line of input- Throws:
CsvRequiredFieldEmptyException
- If a required column is missing
-
findHeader
public java.lang.String findHeader(int col)
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<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
- Parameters:
col
- The column number for which the header is sought- Returns:
- The name of the header
-
getFieldMap
protected FieldMap<java.lang.String,java.lang.Integer,? extends ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T> getFieldMap()
Description copied from class:AbstractMappingStrategy
Returns theFieldMap
associated with this mapping strategy.- Specified by:
getFieldMap
in classAbstractMappingStrategy<java.lang.String,java.lang.Integer,ComplexFieldMapEntry<java.lang.String,java.lang.Integer,T>,T>
- Returns:
- The
FieldMap
used by this strategy
-
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. 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
-
-