Package com.opencsv.bean
Class HeaderNameBaseMappingStrategy<T>
- java.lang.Object
-
- com.opencsv.bean.AbstractMappingStrategy<java.lang.String,java.lang.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
-
- com.opencsv.bean.HeaderNameBaseMappingStrategy<T>
-
- Type Parameters:
T
- The type of bean being created or written
- All Implemented Interfaces:
MappingStrategy<T>
- Direct Known Subclasses:
HeaderColumnNameMappingStrategy
,HeaderColumnNameTranslateMappingStrategy
public abstract class HeaderNameBaseMappingStrategy<T> extends AbstractMappingStrategy<java.lang.String,java.lang.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
This class serves as a location to collect code common to a mapping strategy that maps header names to member variables.- Since:
- 5.0
- Author:
- Andrew Rucker Jones
-
-
Field Summary
Fields Modifier and Type Field Description protected FieldMapByName<T>
fieldMap
Given a header name, this map allows one to find the correspondingBeanField
.protected java.util.Comparator<java.lang.String>
writeOrder
Holds aComparator
to sort columns on writing.-
Fields inherited from class com.opencsv.bean.AbstractMappingStrategy
errorLocale, headerIndex, type
-
-
Constructor Summary
Constructors Constructor Description HeaderNameBaseMappingStrategy()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
captureHeader(CSVReader reader)
Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.protected java.lang.String
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.String>
findField(int col)
Gets the field for a given column position.java.lang.String
findHeader(int col)
Gets the name (or position number) of the header for the given column number.protected FieldMap<java.lang.String,java.lang.String,? extends ComplexFieldMapEntry<java.lang.String,java.lang.String,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
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
setColumnOrderOnWrite(java.util.Comparator<java.lang.String> writeOrder)
Sets theComparator
to be used to sort columns when writing beans to a CSV file.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, generateHeader, getBindingAnnotations, getType, ignoreFields, indexBean, instantiateCustomConverter, loadAnnotatedFieldMap, 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
-
-
-
-
Field Detail
-
fieldMap
protected FieldMapByName<T> fieldMap
Given a header name, this map allows one to find the correspondingBeanField
.
-
writeOrder
protected java.util.Comparator<java.lang.String> writeOrder
Holds aComparator
to sort columns on writing.
-
-
Method Detail
-
captureHeader
public void captureHeader(CSVReader reader) throws java.io.IOException, CsvRequiredFieldEmptyException
Description copied from interface:MappingStrategy
Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.- Parameters:
reader
- The CSVReader to use for header parsing- Throws:
java.io.IOException
- If parsing failsCsvRequiredFieldEmptyException
- If a field is required, but the header or column position for the field is not present in the input
-
chooseMultivaluedFieldIndexFromHeaderIndex
protected java.lang.String 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.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,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)
-
verifyLineLength
public 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.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
- Parameters:
numberOfFields
- The number of fields present in the line of input- Throws:
CsvRequiredFieldEmptyException
- If a required column is missing
-
findField
protected BeanField<T,java.lang.String> findField(int col) throws CsvBadConverterException
Description copied from class:AbstractMappingStrategy
Gets the field for a given column position.- Specified by:
findField
in classAbstractMappingStrategy<java.lang.String,java.lang.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,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
- Throws:
CsvBadConverterException
- If a custom converter for a field cannot be initialized
-
loadUnadornedFieldMap
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.This method is called by
AbstractMappingStrategy.loadFieldMap()
when absolutely no 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:
- Any field annotated with
CsvIgnore
will be ignored on writing - Any field named "serialVersionUID" will be ignored if the
enclosing class implements
Serializable
.
- Specified by:
loadUnadornedFieldMap
in classAbstractMappingStrategy<java.lang.String,java.lang.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
- Parameters:
fields
- A list of all non-synthetic fields in the bean to be processed
- Any field annotated with
-
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.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
-
getFieldMap
protected FieldMap<java.lang.String,java.lang.String,? extends ComplexFieldMapEntry<java.lang.String,java.lang.String,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.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
- Returns:
- The
FieldMap
used by this strategy
-
findHeader
public java.lang.String findHeader(int col)
Description copied from class:AbstractMappingStrategy
Gets the name (or position number) of the header for the given column number. The column numbers are zero-based.- Specified by:
findHeader
in classAbstractMappingStrategy<java.lang.String,java.lang.String,ComplexFieldMapEntry<java.lang.String,java.lang.String,T>,T>
- Parameters:
col
- The column number for which the header is sought- Returns:
- The name of the header
-
setColumnOrderOnWrite
public void setColumnOrderOnWrite(java.util.Comparator<java.lang.String> 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
-
-