Top Description Fields Constructors Methods
com.opencsv.bean

public abstract Class HeaderNameBaseMappingStrategy<T>

extends AbstractMappingStrategy<String, String, ComplexFieldMapEntry<String, String, T>, T>
Class Inheritance
Known Direct Subclasses
com.opencsv.bean.HeaderColumnNameMappingStrategy, com.opencsv.bean.HeaderColumnNameTranslateMappingStrategy
Type Parameters
<T>
The type of bean being created or written
Imports
com.opencsv.CSVReader, .ICSVParser, com.opencsv.exceptions.CsvBadConverterException, .CsvRequiredFieldEmptyException, org.apache.commons.collections4.ListValuedMap, org.apache.commons.lang3.ArrayUtils, .StringUtils, java.io.IOException, .Serializable, java.lang.reflect.Field, java.util.*

This class serves as a location to collect code common to a mapping strategy that maps header names to member variables.
Author
Andrew Rucker Jones
Since
5.0

Field Summary

Modifier and TypeField and Description
protected FieldMapByName<T>
fieldMap

Given a header name, this map allows one to find the corresponding BeanField.

protected final boolean
forceCorrectRecordLength

If set, every record will be shortened or lengthened to match the number of headers.

protected Comparator<String>
writeOrder

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

Inherited from com.opencsv.bean.AbstractMappingStrategy:
errorLocaleheaderIndexprofilerecursiveTypeTreetype

Constructor Summary

AccessConstructor and Description
public
HeaderNameBaseMappingStrategy()

Nullary constructor for compatibility.

public
HeaderNameBaseMappingStrategy(boolean
If set, every record will be shortened or lengthened to match the number of headers
forceCorrectRecordLength
)

Constructor to allow setting options for header name mapping.

Method Summary

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

Implements com.opencsv.bean.MappingStrategy.captureHeader.

Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.

protected String
protected BeanField<T, String>
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.

Gets the name (or position number) of the header for the given column number.

protected FieldMap<String, String, ? extends ComplexFieldMapEntry<String, String, 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
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 binding annotations.

public void
setColumnOrderOnWrite(Comparator<String>
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.

public 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:
createBeandetermineConverterfilterIgnoredFieldsgenerateHeadergetBindingAnnotationsgetColumnNamegetTypeignoreFieldsindexBeaninstantiateCustomConverterisForbiddenClassForRecursionloadAnnotatedFieldMaploadFieldMaploadRecursiveClassespartitionFieldspopulateNewBeanselectAnnotationForProfilesetErrorLocalesetFieldValuesetProfilesetTypetransmuteBean

Field Detail

fieldMapback to summary
protected FieldMapByName<T> fieldMap

Given a header name, this map allows one to find the corresponding BeanField.

forceCorrectRecordLengthback to summary
protected final boolean forceCorrectRecordLength

If set, every record will be shortened or lengthened to match the number of headers.

writeOrderback to summary
protected Comparator<String> writeOrder

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

Constructor Detail

HeaderNameBaseMappingStrategyback to summary
public HeaderNameBaseMappingStrategy()

Nullary constructor for compatibility.

HeaderNameBaseMappingStrategyback to summary
public HeaderNameBaseMappingStrategy(boolean forceCorrectRecordLength)

Constructor to allow setting options for header name mapping.

Parameters
forceCorrectRecordLength:boolean

If set, every record will be shortened or lengthened to match the number of headers

Method Detail

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

Implements com.opencsv.bean.MappingStrategy.captureHeader.

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

Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.

Annotations
@Override
Exceptions
IOException:
If parsing fails
CsvRequiredFieldEmptyException:
If a field is required, but the header or column position for the field is not present in the input
chooseMultivaluedFieldIndexFromHeaderIndexback to summary
protected String 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.

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

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

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

Gets the field for a given column position.

Annotations
@Override
Exceptions
CsvBadConverterException:
If a custom converter for a field cannot be initialized
findHeaderback to summary
public String findHeader(int col)

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

Doc from com.opencsv.bean.AbstractMappingStrategy.findHeader.

Gets the name (or position number) of the header for the given column number. The column numbers are zero-based.

Annotations
@Override
getFieldMapback to summary
protected FieldMap<String, String, ? extends ComplexFieldMapEntry<String, String, 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
loadUnadornedFieldMapback to summary
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 binding annotations.

This method is called by loadFieldMap() when absolutely no binding annotations that are relevant for this mapping strategy are found in the type of bean being processed. It is then assumed that every field is to be included, and that the name of the member variable must exactly match the header name of the input.

Two exceptions are made to the rule that everything is written:

  1. Any field annotated with CsvIgnore will be ignored on writing
  2. Any field named "serialVersionUID" will be ignored if the enclosing class implements java.io.Serializable.

CsvRecurse is respected.

Annotations
@Override
setColumnOrderOnWriteback to summary
public void setColumnOrderOnWrite(Comparator<String> 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<String>

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

Since
4.3
verifyLineLengthback to summary
public 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