T
- Type of the bean to be returnedpublic class HeaderColumnNameMappingStrategy<T> extends Object implements MappingStrategy<T>
Modifier and Type | Field and Description |
---|---|
protected boolean |
annotationDriven
Whether or not annotations were found and should be used for determining
the binding between columns in a CSV source or destination and fields in
a bean.
|
protected List<Field> |
copyOfRequiredFields
A copy of
requiredFields that is used for reading in each record
to make certain all required fields were present. |
protected Map<String,PropertyDescriptor> |
descriptorMap
Given a header name, this map allows one to find the corresponding
property descriptor.
|
protected Map<String,BeanField> |
fieldMap
Given a header name, this map allows one to find the corresponding
BeanField . |
protected String[] |
header
An ordered array of the headers for the columns of a CSV input.
|
protected Map<String,Integer> |
indexLookup
This map makes finding the column index of a header name easy.
|
protected List<Field> |
requiredFields
A list of all fields in the bean that are required.
|
protected Class<? extends T> |
type
This is the class of the bean to be manipulated.
|
Constructor and Description |
---|
HeaderColumnNameMappingStrategy()
Default constructor.
|
Modifier and Type | Method and 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.
|
T |
createBean()
Implementation will return a bean of the type of object you are mapping.
|
protected void |
createIndexLookup(String[] values)
Creates an index map of column names to column position.
|
PropertyDescriptor |
findDescriptor(int col)
Gets the property descriptor for a given column position.
|
protected PropertyDescriptor |
findDescriptor(String name)
Find the property descriptor for a given column.
|
BeanField |
findField(int col)
Gets the field for a given column position.
|
int |
findMaxFieldIndex()
Finds and returns the highest index in this mapping.
|
String[] |
generateHeader()
This method generates a header that can be used for writing beans of the
type provided back to a file.
|
Integer |
getColumnIndex(String name)
Gets the column index that corresponds to a specific column name.
|
String |
getColumnName(int col)
Get the column name for a given column position.
|
Class<? extends T> |
getType()
Get the class type that the Strategy is mapping.
|
protected BeanField |
instantiateCustomConverter(Class<? extends AbstractBeanField> converter)
Attempts to instantiate the class of the custom converter specified.
|
boolean |
isAnnotationDriven()
Determines whether the mapping strategy is driven by annotations.
|
protected Map<String,PropertyDescriptor> |
loadDescriptorMap()
Builds a map of property descriptors for the bean.
|
protected void |
loadFieldMap()
Builds a map of fields for the bean.
|
void |
registerBeginningOfRecordForReading()
Indicate to the mapping strategy that we are beginning to read a new
record.
|
void |
registerEndOfRecordForReading()
Must be called at the end of input for a line/record to verify that the
line was complete.
|
protected void |
resetIndexMap()
Resets index map of column names to column position.
|
void |
setType(Class<? extends T> type)
Sets the class type that is being mapped.
|
protected String[] header
protected Map<String,Integer> indexLookup
protected Map<String,PropertyDescriptor> descriptorMap
protected Map<String,BeanField> fieldMap
BeanField
.protected final List<Field> requiredFields
protected List<Field> copyOfRequiredFields
requiredFields
that is used for reading in each record
to make certain all required fields were present.protected boolean annotationDriven
public HeaderColumnNameMappingStrategy()
public void captureHeader(CSVReader reader) throws IOException, CsvRequiredFieldEmptyException
MappingStrategy
captureHeader
in interface MappingStrategy<T>
reader
- The CSVReader to use for header parsingIOException
- If parsing failsCsvRequiredFieldEmptyException
- If a field is required, but the
header or column position for the field is not present in the inputpublic void registerBeginningOfRecordForReading()
MappingStrategy
registerBeginningOfRecordForReading
in interface MappingStrategy<T>
public void registerEndOfRecordForReading() throws CsvRequiredFieldEmptyException
MappingStrategy
registerEndOfRecordForReading
in interface MappingStrategy<T>
CsvRequiredFieldEmptyException
- If a required column is missingpublic String[] generateHeader()
generateHeader
in interface MappingStrategy<T>
protected void createIndexLookup(String[] values)
values
- Array of header values.protected void resetIndexMap()
public Integer getColumnIndex(String name)
MappingStrategy
getColumnIndex
in interface MappingStrategy<T>
name
- The column namepublic PropertyDescriptor findDescriptor(int col) throws IntrospectionException
MappingStrategy
findDescriptor
in interface MappingStrategy<T>
col
- The column to find the description forIntrospectionException
- Thrown on error retrieving the property
description.public BeanField findField(int col) throws CsvBadConverterException
MappingStrategy
MappingStrategy.registerBeginningOfRecordForReading()
, this method also marks
the field indicated as having been present in the input.findField
in interface MappingStrategy<T>
col
- The column to find the field forCsvBadConverterException
- If a custom converter for a field cannot
be initializedpublic int findMaxFieldIndex()
MappingStrategy
MappingStrategy.findField(int)
will return null for these columns, so we need
a way to know when to stop writing new columns.findMaxFieldIndex
in interface MappingStrategy<T>
public String getColumnName(int col)
col
- Column position.protected PropertyDescriptor findDescriptor(String name) throws IntrospectionException
name
- Column name to look up.IntrospectionException
- Thrown on error loading the property
descriptors.protected Map<String,PropertyDescriptor> loadDescriptorMap() throws IntrospectionException
IntrospectionException
- Thrown on error getting information
about the bean.protected BeanField instantiateCustomConverter(Class<? extends AbstractBeanField> converter) throws CsvBadConverterException
converter
- The class for a custom converterCsvBadConverterException
- If the class cannot be instantiatedprotected void loadFieldMap() throws CsvBadConverterException
CsvBadConverterException
- If there is a problem instantiating the
custom converter for an annotated fieldpublic T createBean() throws InstantiationException, IllegalAccessException, IllegalStateException
MappingStrategy
createBean
in interface MappingStrategy<T>
InstantiationException
- Thrown on error creating object.IllegalAccessException
- Thrown on error creating object.IllegalStateException
public Class<? extends T> getType()
public void setType(Class<? extends T> type) throws CsvBadConverterException
type
- Class type.CsvBadConverterException
- If a field in the bean is annotated
with a custom converter that cannot be initialized. If you are not
using custom converters that you have written yourself, it should be
safe to catch this exception and ignore it.public boolean isAnnotationDriven()
isAnnotationDriven
in interface MappingStrategy<T>
Copyright © 2017. All rights reserved.