Top Description Fields Constructors Methods
com.opencsv.bean

public Class ColumnPositionMappingStrategy<T>

extends AbstractMappingStrategy<String, Integer, ComplexFieldMapEntry<String, Integer, T>, T>
Class Inheritance
Type Parameters
<T>
Type of object that is being processed.
Imports
com.opencsv.CSVReader, .ICSVParser, com.opencsv.exceptions.CsvRequiredFieldEmptyException, org.apache.commons.collections4.ListValuedMap, org.apache.commons.lang3.ArrayUtils, java.io.IOException, java.lang.annotation.Annotation, java.lang.reflect.Field, java.util.*

Allows for the mapping of columns with their positions. Using this strategy without annotations (com.opencsv.bean.CsvBindByPosition or com.opencsv.bean.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.

Field Summary

Modifier and TypeField and Description
private Integer[]
columnIndexForWriting

Used to store a mapping from presumed input column index to desired output column index, as determined by applying writeOrder.

private boolean
columnsExplicitlySet

Whether the user has programmatically set the map from column positions to field names.

private FieldMapByPosition<T>
fieldMap

The map from column position to BeanField.

private Comparator<Integer>
writeOrder

Holds a java.util.Comparator to sort columns on writing.

Inherited from com.opencsv.bean.AbstractMappingStrategy:
errorLocaleheaderIndexprofilerecursiveTypeTreetype

Constructor Summary

AccessConstructor and Description
public
ColumnPositionMappingStrategy()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
public void
captureHeader(CSVReader reader)

Implements com.opencsv.bean.MappingStrategy.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.

protected Integer

Returns:

For this mapping strategy, it's simply index wrapped as an java.lang.Integer.
chooseMultivaluedFieldIndexFromHeaderIndex
(int index)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.chooseMultivaluedFieldIndexFromHeaderIndex.

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)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.findField.

Gets the field for a given column position.

public String
findHeader(int col)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.findHeader.

Returns the column position for the given column number.

public String[]

Returns:

An empty array
generateHeader
(T bean)

Overrides com.opencsv.bean.AbstractMappingStrategy.generateHeader.

Implements com.opencsv.bean.MappingStrategy.generateHeader.

This method returns an empty array.

protected Set<Class<? extends Annotation>>
getBindingAnnotations()

Overrides com.opencsv.bean.AbstractMappingStrategy.getBindingAnnotations.

Returns a set of the annotations that are used for binding in this mapping strategy.

public String[]

Returns:

String array with the column mappings.
getColumnMapping
()

Retrieves the column mappings.

public String

Returns:

Column name or null if col > number of mappings.
getColumnName
(int
Position of the column.
col
)

Overrides com.opencsv.bean.AbstractMappingStrategy.getColumnName.

Gets a column name.

protected FieldMap<String, Integer, ? extends ComplexFieldMapEntry<String, Integer, T>, T>
getFieldMap()

Implements abstract com.opencsv.bean.AbstractMappingStrategy.getFieldMap.

Returns the FieldMap associated with this mapping strategy.

protected void
initializeFieldMap()

Implements abstract com.opencsv.bean.AbstractMappingStrategy.initializeFieldMap.

Creates an empty binding-type-specific field map that can be filled in later steps.

protected void
loadAnnotatedFieldMap(ListValuedMap<Class<?>, Field> fields)

Overrides com.opencsv.bean.AbstractMappingStrategy.loadAnnotatedFieldMap.

Creates a map of annotated fields in the bean to be processed.

protected void
loadUnadornedFieldMap(ListValuedMap<Class<?>, Field> fields)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.loadUnadornedFieldMap.

Creates a map of fields in the bean to be processed that have no annotations.

private void
registerBinding(CsvBindByPosition
The annotation attached to the bean field
annotation
,
Class<?>
The class/type in which the field resides
localType
,
Field
The bean field
localField
)

Register a binding between a bean field and a simple converter.

private void
registerCustomBinding(CsvCustomBindByPosition
The annotation attached to the bean field
annotation
,
Class<?>
The class/type in which the field resides
localType
,
Field
The bean field
localField
)

Register a binding between a bean field and a custom converter.

private void
registerJoinBinding(CsvBindAndJoinByPosition
The annotation attached to the bean field
annotation
,
Class<?>
The class/type in which the field resides
localType
,
Field
The bean field
localField
)

Register a binding between a bean field and a multi-valued converter that joins values from multiple columns.

private void
registerSplitBinding(CsvBindAndSplitByPosition
The annotation attached to the bean field
annotation
,
Class<?>
The class/type in which the field resides
localType
,
Field
The bean field
localField
)

Register a binding between a bean field and a collection converter that splits input into multiple values.

public void
setColumnMapping(String...
Column names to be mapped.
columnMapping
)

Setter for the column mapping.

public void
setColumnOrderOnWrite(Comparator<Integer>
The java.util.Comparator to use. May be null, in which case the natural ordering is used.
writeOrder
)

Sets the java.util.Comparator to be used to sort columns when writing beans to a CSV file.

protected void
verifyLineLength(int numberOfFields)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.verifyLineLength.

Must be called once the length of input for a line/record is known to verify that the line was complete.

Inherited from com.opencsv.bean.AbstractMappingStrategy:
createBeandetermineConverterfilterIgnoredFieldsgetTypeignoreFieldsindexBeaninstantiateCustomConverterisForbiddenClassForRecursionloadFieldMaploadRecursiveClassespartitionFieldspopulateNewBeanselectAnnotationForProfilesetErrorLocalesetFieldValuesetProfilesetTypetransmuteBean

Field Detail

columnIndexForWritingback to summary
private Integer[] columnIndexForWriting

Used to store a mapping from presumed input column index to desired output column index, as determined by applying writeOrder.

columnsExplicitlySetback to summary
private boolean columnsExplicitlySet

Whether the user has programmatically set the map from column positions to field names.

fieldMapback to summary
private FieldMapByPosition<T> fieldMap

The map from column position to BeanField.

writeOrderback to summary
private Comparator<Integer> writeOrder

Holds a java.util.Comparator to sort columns on writing.

Constructor Detail

ColumnPositionMappingStrategyback to summary
public ColumnPositionMappingStrategy()

Default constructor. Considered stable.

See Also
ColumnPositionMappingStrategyBuilder

Method Detail

captureHeaderback to summary
public void captureHeader(CSVReader reader) throws IOException

Implements com.opencsv.bean.MappingStrategy.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.

Annotations
@Override
Exceptions
IOException:

Doc from com.opencsv.bean.MappingStrategy.captureHeader.

If parsing fails

chooseMultivaluedFieldIndexFromHeaderIndexback to summary
protected Integer chooseMultivaluedFieldIndexFromHeaderIndex(int index)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.chooseMultivaluedFieldIndexFromHeaderIndex.

Doc from com.opencsv.bean.AbstractMappingStrategy.chooseMultivaluedFieldIndexFromHeaderIndex.

For BeanField#indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object) it is necessary to determine which index to pass in.

Returns:Integer

For this mapping strategy, it's simply index wrapped as an java.lang.Integer.

Annotations
@Override
findFieldback to summary
protected BeanField<T, Integer> findField(int col)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.findField.

Doc from com.opencsv.bean.AbstractMappingStrategy.findField.

Gets the field for a given column position.

Annotations
@Override
findHeaderback to summary
public String findHeader(int col)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.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.

Annotations
@Override
generateHeaderback to summary
public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException

Overrides com.opencsv.bean.AbstractMappingStrategy.generateHeader.

Implements com.opencsv.bean.MappingStrategy.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.

Returns:String[]

An empty array

Annotations
@Override
Exceptions
CsvRequiredFieldEmptyException:

Doc from com.opencsv.bean.MappingStrategy.generateHeader.

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.

getBindingAnnotationsback to summary
protected Set<Class<? extends Annotation>> getBindingAnnotations()

Overrides com.opencsv.bean.AbstractMappingStrategy.getBindingAnnotations.

Returns a set of the annotations that are used for binding in this mapping strategy.

In this mapping strategy, those are currently:

Annotations
@Override
getColumnMappingback to summary
public String[] getColumnMapping()

Retrieves the column mappings.

Returns:String[]

String array with the column mappings.

getColumnNameback to summary
public String getColumnName(int col)

Overrides com.opencsv.bean.AbstractMappingStrategy.getColumnName.

Gets a column name.

Parameters
col:int

Position of the column.

Returns:String

Column name or null if col > number of mappings.

Annotations
@Override
getFieldMapback to summary
protected FieldMap<String, Integer, ? extends ComplexFieldMapEntry<String, Integer, T>, T> getFieldMap()

Implements abstract com.opencsv.bean.AbstractMappingStrategy.getFieldMap.

Doc from com.opencsv.bean.AbstractMappingStrategy.getFieldMap.

Returns the FieldMap associated with this mapping strategy.

Annotations
@Override
initializeFieldMapback to summary
protected void initializeFieldMap()

Implements abstract com.opencsv.bean.AbstractMappingStrategy.initializeFieldMap.

Doc from com.opencsv.bean.AbstractMappingStrategy.initializeFieldMap.

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.

Annotations
@Override
loadAnnotatedFieldMapback to summary
protected void loadAnnotatedFieldMap(ListValuedMap<Class<?>, Field> fields)

Overrides com.opencsv.bean.AbstractMappingStrategy.loadAnnotatedFieldMap.

Creates a map of annotated fields in the bean to be processed.

This method is called by loadFieldMap() when at least one relevant annotation is found on a member variable.

Annotations
@Override
loadUnadornedFieldMapback to summary
protected void loadUnadornedFieldMap(ListValuedMap<Class<?>, Field> fields)

Implements abstract com.opencsv.bean.AbstractMappingStrategy.loadUnadornedFieldMap.

Doc from com.opencsv.bean.AbstractMappingStrategy.loadUnadornedFieldMap.

Creates a map of fields in the bean to be processed that have no annotations. This method is called by loadFieldMap() when absolutely no annotations that are relevant for this mapping strategy are found in the type of bean being processed.

Annotations
@Override
registerBindingback to summary
private void registerBinding(CsvBindByPosition annotation, Class<?> localType, Field localField)

Register a binding between a bean field and a simple converter.

Parameters
annotation:CsvBindByPosition

The annotation attached to the bean field

localType:Class<?>

The class/type in which the field resides

localField:Field

The bean field

registerCustomBindingback to summary
private void registerCustomBinding(CsvCustomBindByPosition annotation, Class<?> localType, Field localField)

Register a binding between a bean field and a custom converter.

Parameters
annotation:CsvCustomBindByPosition

The annotation attached to the bean field

localType:Class<?>

The class/type in which the field resides

localField:Field

The bean field

registerJoinBindingback to summary
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:CsvBindAndJoinByPosition

The annotation attached to the bean field

localType:Class<?>

The class/type in which the field resides

localField:Field

The bean field

registerSplitBindingback to summary
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:CsvBindAndSplitByPosition

The annotation attached to the bean field

localType:Class<?>

The class/type in which the field resides

localField:Field

The bean field

setColumnMappingback to summary
public void setColumnMapping(String... columnMapping)

Setter for the column mapping. This mapping is for reading. Use of this method in conjunction with writing is undefined.

Parameters
columnMapping:String[]

Column names to be mapped.

setColumnOrderOnWriteback to summary
public void setColumnOrderOnWrite(Comparator<Integer> writeOrder)

Sets the java.util.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.

Parameters
writeOrder:Comparator<Integer>

The java.util.Comparator to use. May be null, in which case the natural ordering is used.

Since
4.3
verifyLineLengthback to summary
protected void verifyLineLength(int numberOfFields) throws CsvRequiredFieldEmptyException

Implements abstract com.opencsv.bean.AbstractMappingStrategy.verifyLineLength.

Doc from com.opencsv.bean.AbstractMappingStrategy.verifyLineLength.

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.

Annotations
@Override
Exceptions
CsvRequiredFieldEmptyException:
If a required column is missing