Class HeaderNameBaseMappingStrategy<T>
java.lang.Object
com.opencsv.bean.AbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,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<String, String, ComplexFieldMapEntry<String,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
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractMappingStrategy
AbstractMappingStrategy.RecursiveType -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected FieldMapByName<T> Given a header name, this map allows one to find the correspondingBeanField.protected final booleanIf set, every record will be shortened or lengthened to match the number of headers.protected Comparator<String> Holds aComparatorto sort columns on writing.Fields inherited from class AbstractMappingStrategy
errorLocale, headerIndex, profile, recursiveTypeTree, type -
Constructor Summary
ConstructorsConstructorDescriptionNullary constructor for compatibility.HeaderNameBaseMappingStrategy(boolean forceCorrectRecordLength) Constructor to allow setting options for header name mapping. -
Method Summary
Modifier and TypeMethodDescriptionvoidcaptureHeader(CSVReader reader) Implementation of this method can grab the header line before parsing begins to use to map columns to bean properties.protected StringchooseMultivaluedFieldIndexFromHeaderIndex(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) Gets the name (or position number) of the header for the given column number.Returns theFieldMapassociated with this mapping strategy.protected voidCreates an empty binding-type-specific field map that can be filled in later steps.protected voidloadUnadornedFieldMap(org.apache.commons.collections4.ListValuedMap<Class<?>, Field> fields) Creates a map of fields in the bean to be processed that have no binding annotations.voidsetColumnOrderOnWrite(Comparator<String> writeOrder) Sets theComparatorto be used to sort columns when writing beans to a CSV file.voidverifyLineLength(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 AbstractMappingStrategy
createBean, determineConverter, filterIgnoredFields, generateHeader, getBindingAnnotations, getColumnName, getType, ignoreFields, indexBean, instantiateCustomConverter, isForbiddenClassForRecursion, loadAnnotatedFieldMap, loadFieldMap, loadRecursiveClasses, partitionFields, populateNewBean, selectAnnotationForProfile, setErrorLocale, setFieldValue, setProfile, setType, transmuteBeanMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface MappingStrategy
isAnnotationDriven
-
Field Details
-
fieldMap
Given a header name, this map allows one to find the correspondingBeanField. -
writeOrder
Holds aComparatorto sort columns on writing. -
forceCorrectRecordLength
protected final boolean forceCorrectRecordLengthIf set, every record will be shortened or lengthened to match the number of headers.
-
-
Constructor Details
-
HeaderNameBaseMappingStrategy
public HeaderNameBaseMappingStrategy()Nullary constructor for compatibility. -
HeaderNameBaseMappingStrategy
public HeaderNameBaseMappingStrategy(boolean forceCorrectRecordLength) Constructor to allow setting options for header name mapping.- Parameters:
forceCorrectRecordLength- If set, every record will be shortened or lengthened to match the number of headers
-
-
Method Details
-
captureHeader
Description copied from interface:MappingStrategyImplementation 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:
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
Description copied from class:AbstractMappingStrategyForBeanField.indexAndSplitMultivaluedField(java.lang.Object, java.lang.Object)it is necessary to determine which index to pass in.- Specified by:
chooseMultivaluedFieldIndexFromHeaderIndexin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,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
Description copied from class:AbstractMappingStrategyMust 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:
verifyLineLengthin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,String, T>, T> - Parameters:
numberOfFields- The number of fields present in the line of input- Throws:
CsvRequiredFieldEmptyException- If a required column is missing
-
findField
Description copied from class:AbstractMappingStrategyGets the field for a given column position.- Specified by:
findFieldin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,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<Class<?>, Field> fields) Creates a map of fields in the bean to be processed that have no binding annotations.This method is called by
AbstractMappingStrategy.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:
- Any field annotated with
CsvIgnorewill be ignored on writing - Any field named "serialVersionUID" will be ignored if the
enclosing class implements
Serializable.
CsvRecurseis respected.- Specified by:
loadUnadornedFieldMapin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,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:AbstractMappingStrategyCreates 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:
initializeFieldMapin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,String, T>, T>
-
getFieldMap
protected FieldMap<String, String, ? extends ComplexFieldMapEntry<String,String, getFieldMap()T>, T> Description copied from class:AbstractMappingStrategyReturns theFieldMapassociated with this mapping strategy.- Specified by:
getFieldMapin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,String, T>, T> - Returns:
- The
FieldMapused by this strategy
-
findHeader
Description copied from class:AbstractMappingStrategyGets the name (or position number) of the header for the given column number. The column numbers are zero-based.- Specified by:
findHeaderin classAbstractMappingStrategy<String, String, ComplexFieldMapEntry<String,String, T>, T> - Parameters:
col- The column number for which the header is sought- Returns:
- The name of the header
-
setColumnOrderOnWrite
Sets theComparatorto 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- TheComparatorto use. May benull, in which case the natural ordering is used.- Since:
- 4.3
-