liquibase.util.csv.opencsv.bean
Class HeaderColumnNameMappingStrategy<T>

java.lang.Object
  extended by liquibase.util.csv.opencsv.bean.HeaderColumnNameMappingStrategy<T>
Type Parameters:
T -
All Implemented Interfaces:
MappingStrategy<T>
Direct Known Subclasses:
ColumnPositionMappingStrategy, HeaderColumnNameTranslateMappingStrategy

public class HeaderColumnNameMappingStrategy<T>
extends Object
implements MappingStrategy<T>

Maps data to objects using the column names in the first row of the csv file as reference. This way the column order does not matter.


Field Summary
protected  boolean annotationDriven
           
protected  Map<String,PropertyDescriptor> descriptorMap
           
protected  boolean determinedIfAnnotationDriven
           
protected  Map<String,BeanField> fieldMap
           
protected  String[] header
           
protected  Map<String,Integer> indexLookup
           
protected  Class<T> type
           
 
Constructor Summary
HeaderColumnNameMappingStrategy()
          Default constructor.
 
Method Summary
 void captureHeader(CSVReader reader)
          Retrieves the header from the CSVReader.
 T createBean()
          Creates an object to be mapped.
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.
protected  BeanField findField(String name)
          Find the field for a given column.
 Integer getColumnIndex(String name)
          Gets the column index that corresponds to a specific colum name.
 String getColumnName(int col)
          Get the column name for a given column position.
 Class<T> getType()
          get the class type that the Strategy is mapping.
 boolean isAnnotationDriven()
          Determines whether the mapping strategy is driven by CsvBind annotations.
protected  Map<String,PropertyDescriptor> loadDescriptorMap()
          builds a map of property descriptors for the Bean.
protected  Map<String,BeanField> loadFieldMap()
          Builds a map of fields (and whether they're mandatory) for the Bean.
protected  boolean matches(String name, PropertyDescriptor desc)
          Determines if the name of a property descriptor matches the column name.
protected  void resetIndexMap()
          Resets index map of column names to column position.
 void setType(Class<T> type)
          Sets the class type that is being mapped.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

header

protected String[] header

indexLookup

protected Map<String,Integer> indexLookup

descriptorMap

protected Map<String,PropertyDescriptor> descriptorMap

fieldMap

protected Map<String,BeanField> fieldMap

type

protected Class<T> type

annotationDriven

protected boolean annotationDriven

determinedIfAnnotationDriven

protected boolean determinedIfAnnotationDriven
Constructor Detail

HeaderColumnNameMappingStrategy

public HeaderColumnNameMappingStrategy()
Default constructor.

Method Detail

captureHeader

public void captureHeader(CSVReader reader)
                   throws IOException
Retrieves the header from the CSVReader.

Specified by:
captureHeader in interface MappingStrategy<T>
Parameters:
reader - the CSVReader to use for header parsing
Throws:
IOException - - thrown on error reading from the CSVReader.

createIndexLookup

protected void createIndexLookup(String[] values)
Creates an index map of column names to column position.

Parameters:
values - - array of header values.

resetIndexMap

protected void resetIndexMap()
Resets index map of column names to column position.


getColumnIndex

public Integer getColumnIndex(String name)
Gets the column index that corresponds to a specific colum name. If the CSV file doesn't have a header row, this method will always return null.

Specified by:
getColumnIndex in interface MappingStrategy<T>
Parameters:
name - the column name
Returns:
the column index, or null if the name doesn't exist

findDescriptor

public PropertyDescriptor findDescriptor(int col)
                                  throws IntrospectionException
Gets the property descriptor for a given column position.

Specified by:
findDescriptor in interface MappingStrategy<T>
Parameters:
col - the column to find the description for
Returns:
- the property descriptor for the column position or null if one could not be found.
Throws:
IntrospectionException - - thrown on error retrieving the property description.

findField

public BeanField findField(int col)
Gets the field for a given column position.

Specified by:
findField in interface MappingStrategy<T>
Parameters:
col - the column to find the field for
Returns:
- BeanField containing the field - and whether it is mandatory - for a given column position, or null if one could not be found

getColumnName

public String getColumnName(int col)
Get the column name for a given column position.

Parameters:
col - - column position.
Returns:
- the column name or null if the position is larger than the header array or there is no headers defined.

findDescriptor

protected PropertyDescriptor findDescriptor(String name)
                                     throws IntrospectionException
Find the property descriptor for a given column.

Parameters:
name - - column name to look up.
Returns:
- the property descriptor for the column.
Throws:
IntrospectionException - - thrown on error loading the property descriptors.

findField

protected BeanField findField(String name)
Find the field for a given column.

Parameters:
name - - the column name to look up.
Returns:
- BeanField containing the field - and whether it is mandatory - for the column.

matches

protected boolean matches(String name,
                          PropertyDescriptor desc)
Determines if the name of a property descriptor matches the column name. Currently only used by unit tests.

Parameters:
name - - name of the column.
desc - - property descriptor to check against
Returns:
- true if the name matches the name in the property descriptor.

loadDescriptorMap

protected Map<String,PropertyDescriptor> loadDescriptorMap()
                                                    throws IntrospectionException
builds a map of property descriptors for the Bean.

Returns:
- map of property descriptors
Throws:
IntrospectionException - - thrown on error getting information about the bean.

loadFieldMap

protected Map<String,BeanField> loadFieldMap()
Builds a map of fields (and whether they're mandatory) for the Bean.

Returns:
- a map of fields (and whether they're mandatory)

createBean

public T createBean()
             throws InstantiationException,
                    IllegalAccessException
Creates an object to be mapped.

Specified by:
createBean in interface MappingStrategy<T>
Returns:
an object of type T.
Throws:
InstantiationException - - thrown on error creating object.
IllegalAccessException - - thrown on error creating object.

getType

public Class<T> getType()
get the class type that the Strategy is mapping.

Returns:
Class of the object that mapper will create.

setType

public void setType(Class<T> type)
Sets the class type that is being mapped.

Parameters:
type - Class type.

isAnnotationDriven

public boolean isAnnotationDriven()
Determines whether the mapping strategy is driven by CsvBind annotations.

Specified by:
isAnnotationDriven in interface MappingStrategy<T>
Returns:
whether the mapping strategy is driven by annotations


Copyright © 2016 Liquibase.org. All rights reserved.