Class ColumnMapping
- java.lang.Object
-
- com.univocity.parsers.common.fields.ColumnMapping
-
- All Implemented Interfaces:
ColumnMapper,java.lang.Cloneable
public final class ColumnMapping extends java.lang.Object implements ColumnMapper
Implementation theColumnMapperinterface which allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written.- See Also:
ColumnMapper
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classColumnMapping.MethodMappingprivate classColumnMapping.NameMapping
-
Field Summary
Fields Modifier and Type Field Description private ColumnMapping.NameMappingattributeMappingprivate ColumnMapping.MethodMappingmethodMappingprivate ColumnMapping.NameMappingmethodNameMapping
-
Constructor Summary
Constructors Constructor Description ColumnMapping()Creates a new column mapping instanceColumnMapping(java.lang.String prefix, ColumnMapping parent)Creates a nested column mapping instance for handling nested attributes.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidattributesToColumnNames(java.util.Map<java.lang.String,java.lang.String> mappings)Maps multiple attributes to multiple column names.voidattributesToColumns(java.util.Map<java.lang.String,java.lang.Enum<?>> mappings)Maps multiple attributes to multiple columns.voidattributesToIndexes(java.util.Map<java.lang.String,java.lang.Integer> mappings)Maps multiple attributes to multiple column positions.voidattributeToColumn(java.lang.String attributeName, java.lang.Enum<?> column)Maps an attribute to a column.voidattributeToColumnName(java.lang.String attributeName, java.lang.String columnName)Maps an attribute to a column name.voidattributeToIndex(java.lang.String attributeName, int columnIndex)Maps an attribute to a column position.ColumnMapperclone()Creates a deep copy of this object with all its mappings.private static java.lang.StringgetCurrentAttributePrefix(java.lang.String prefix, java.lang.String name)java.util.Set<java.lang.String>getNestedAttributeNames()Returns the first-level names of all nested members whose attributes or methods have been mappedjava.lang.StringgetPrefix()Returns object the nesting path associated with the current mapping.booleanisMapped(MethodDescriptor method, java.lang.String targetName)Tests whether a method or attribute has been mapped to a column.voidmethodsToColumnNames(java.util.Map<java.lang.String,java.lang.String> mappings)Maps multiple methods to multiple column names.voidmethodsToColumns(java.util.Map<java.lang.String,java.lang.Enum<?>> mappings)Maps multiple methods to multiple columns.voidmethodsToIndexes(java.util.Map<java.lang.String,java.lang.Integer> mappings)Maps multiple methods to multiple column positions.private voidmethodToColumn(MethodDescriptor method, java.lang.Enum<?> column)voidmethodToColumn(java.lang.String setterName, java.lang.Class<?> parameterType, java.lang.Enum<?> column)Maps a setter method to a column.voidmethodToColumn(java.lang.String methodName, java.lang.Enum<?> column)Maps a method to a column.private voidmethodToColumnName(MethodDescriptor method, java.lang.String columnName)voidmethodToColumnName(java.lang.String setterName, java.lang.Class<?> parameterType, java.lang.String columnName)Maps a setter method to a column name.voidmethodToColumnName(java.lang.String methodName, java.lang.String columnName)Maps a method to a column name.private voidmethodToIndex(MethodDescriptor method, int columnIndex)voidmethodToIndex(java.lang.String methodName, int columnIndex)Maps a method to a column position.voidmethodToIndex(java.lang.String setterName, java.lang.Class<?> parameterType, int columnIndex)Maps a setter method to a column position.voidremove(java.lang.String methodOrAttributeName)Removes any mappings that target a given method or attribute name.booleanupdateMapping(FieldMapping fieldMapping, java.lang.String targetName, MethodDescriptor method)Updates the mapping of a attribute/method so a mapped class member can target a user provided column.
-
-
-
Field Detail
-
attributeMapping
private ColumnMapping.NameMapping attributeMapping
-
methodNameMapping
private ColumnMapping.NameMapping methodNameMapping
-
methodMapping
private ColumnMapping.MethodMapping methodMapping
-
-
Constructor Detail
-
ColumnMapping
public ColumnMapping()
Creates a new column mapping instance
-
ColumnMapping
public ColumnMapping(java.lang.String prefix, ColumnMapping parent)Creates a nested column mapping instance for handling nested attributes. For internal use.- Parameters:
prefix- the current nesting path, denoted by a dot separated string of attribute namesparent- the mappings of the parent object in the nested structure.
-
-
Method Detail
-
getCurrentAttributePrefix
private static java.lang.String getCurrentAttributePrefix(java.lang.String prefix, java.lang.String name)
-
attributeToColumnName
public void attributeToColumnName(java.lang.String attributeName, java.lang.String columnName)Description copied from interface:ColumnMapperMaps an attribute to a column name.- Specified by:
attributeToColumnNamein interfaceColumnMapper- Parameters:
attributeName- the name of the attribute. Use the dot character to access attributes of nested objects, e.g.contact.mobilewill target the attribute "mobile" from a Contact attribute inside a Customer class.columnName- the name of the column that: (a) when parsing, will be read from to populate the given attribute of an object; (b) when writing, will receive the value of the given attribute of an object;
-
attributeToColumn
public void attributeToColumn(java.lang.String attributeName, java.lang.Enum<?> column)Description copied from interface:ColumnMapperMaps an attribute to a column.- Specified by:
attributeToColumnin interfaceColumnMapper- Parameters:
attributeName- the name of the attribute. Use the dot character to access attributes of nested objects, e.g.contact.mobilewill target the attribute "mobile" from a Contact attribute inside a Customer class.column- an enumeration representing the column that: (a) when parsing, will be read from to populate the given attribute of an object; (b) when writing, will receive the value of the given attribute of an object;
-
attributeToIndex
public void attributeToIndex(java.lang.String attributeName, int columnIndex)Description copied from interface:ColumnMapperMaps an attribute to a column position.- Specified by:
attributeToIndexin interfaceColumnMapper- Parameters:
attributeName- the name of the attribute. Use the dot character to access attributes of nested objects, e.g.contact.mobilewill target the attribute "mobile" from a Contact attribute inside a Customer class.columnIndex- the position of the column that: (a) when parsing, will be read from to populate the given attribute of an object; (b) when writing, will receive the value of the given attribute of an object;
-
attributesToColumnNames
public void attributesToColumnNames(java.util.Map<java.lang.String,java.lang.String> mappings)
Description copied from interface:ColumnMapperMaps multiple attributes to multiple column names.- Specified by:
attributesToColumnNamesin interfaceColumnMapper- Parameters:
mappings- a map of attribute names associated with a corresponding column name, where:- Each key is the name of the attribute. Use the dot character to access attributes of nested objects,
e.g.
contact.mobilewill target the attribute "mobile" from a Contact attribute inside a Customer class. - Each value is the name of the column that: (a) when parsing, will be read from to populate the given attribute of an object; (b) when writing, will receive the value of the given attribute of an object;
- Each key is the name of the attribute. Use the dot character to access attributes of nested objects,
e.g.
-
attributesToColumns
public void attributesToColumns(java.util.Map<java.lang.String,java.lang.Enum<?>> mappings)
Description copied from interface:ColumnMapperMaps multiple attributes to multiple columns.- Specified by:
attributesToColumnsin interfaceColumnMapper- Parameters:
mappings- a map of attribute names associated with a corresponding column, where:- Each key is the name of the attribute. Use the dot character to access attributes of nested objects,
e.g.
contact.mobilewill target the attribute "mobile" from a Contact attribute inside a Customer class. - Each value is an enumeration representing the column that: (a) when parsing, will be read from to populate the given attribute of an object; (b) when writing, will receive the value of the given attribute of an object;
- Each key is the name of the attribute. Use the dot character to access attributes of nested objects,
e.g.
-
attributesToIndexes
public void attributesToIndexes(java.util.Map<java.lang.String,java.lang.Integer> mappings)
Description copied from interface:ColumnMapperMaps multiple attributes to multiple column positions.- Specified by:
attributesToIndexesin interfaceColumnMapper- Parameters:
mappings- a map of attribute names associated with a corresponding column, where:- Each key is the name of the attribute. Use the dot character to access attributes of nested objects,
e.g.
contact.mobilewill target the attribute "mobile" from a Contact attribute inside a Customer class. - Each value is an integer representing the position of the column that: (a) when parsing, will be read from to populate the given attribute of an object; (b) when writing, will receive the value of the given attribute of an object;
- Each key is the name of the attribute. Use the dot character to access attributes of nested objects,
e.g.
-
methodToColumnName
private void methodToColumnName(MethodDescriptor method, java.lang.String columnName)
-
methodToColumn
private void methodToColumn(MethodDescriptor method, java.lang.Enum<?> column)
-
methodToIndex
private void methodToIndex(MethodDescriptor method, int columnIndex)
-
isMapped
public boolean isMapped(MethodDescriptor method, java.lang.String targetName)
Tests whether a method or attribute has been mapped to a column.- Parameters:
method- a descriptor of getter/setter methods (can benull)targetName- name of a method or attribute- Returns:
trueif the given method or attribute has been mapped to a column
-
updateMapping
public boolean updateMapping(FieldMapping fieldMapping, java.lang.String targetName, MethodDescriptor method)
Updates the mapping of a attribute/method so a mapped class member can target a user provided column.- Parameters:
fieldMapping- a class member that has should be mapped to a columntargetName- name of a method or attributemethod- a descriptor of getter/setter methods (can benull)- Returns:
trueif the mapping has been successfully updated.
-
getPrefix
public java.lang.String getPrefix()
Returns object the nesting path associated with the current mapping.- Returns:
- a dot separated string of nested attribute names
-
methodToColumnName
public void methodToColumnName(java.lang.String methodName, java.lang.String columnName)Description copied from interface:ColumnMapperMaps a method to a column name. When parsing, only "setter" methods will be used i.e. the given method accepts one parameter. If the method is overridden, useColumnMapper.methodToColumnName(String, Class, String)to specify the exact parameter type to match the appropriate setter method. When writing, only "getter" methods will be used i.e. the given method doesn't accept any parameters and returns a value.- Specified by:
methodToColumnNamein interfaceColumnMapper- Parameters:
methodName- the name of the method. Use the dot character to access methods of nested objects, e.g.contact.mobilewill target the method "mobile(String)" when parsing, or "String mobile()" when writing, from a Contact attribute inside a Customer class.columnName- the name of the column that: (a) when parsing, will be read from to invoke given setter method of an object; (b) when writing, will receive the value returned by the given getter method of an object;
-
methodToColumn
public void methodToColumn(java.lang.String methodName, java.lang.Enum<?> column)Description copied from interface:ColumnMapperMaps a method to a column. When parsing, only "setter" methods will be used i.e. the given method accepts one parameter. If the method is overridden, useColumnMapper.methodToColumnName(String, Class, String)to specify the exact parameter type to match the appropriate setter method. When writing, only "getter" methods will be used i.e. the given method doesn't accept any parameters and returns a value.- Specified by:
methodToColumnin interfaceColumnMapper- Parameters:
methodName- the name of the method. Use the dot character to access methods of nested objects, * e.g.contact.mobilewill target the method "mobile(String)" when parsing, or "String mobile()" when writing, * from a Contact attribute inside a Customer class.column- an enumeration representing the column that: (a) when parsing, will be read from to invoke given setter method of an object; (b) when writing, will receive the value returned by the given getter method of an object;
-
methodToIndex
public void methodToIndex(java.lang.String methodName, int columnIndex)Description copied from interface:ColumnMapperMaps a method to a column position. When parsing, only "setter" methods will be used i.e. the given method accepts one parameter. If the method is overridden, useColumnMapper.methodToColumnName(String, Class, String)to specify the exact parameter type to match the appropriate setter method. When writing, only "getter" methods will be used i.e. the given method doesn't accept any parameters and returns a value.- Specified by:
methodToIndexin interfaceColumnMapper- Parameters:
methodName- the name of the method. Use the dot character to access methods of nested objects, * e.g.contact.mobilewill target the method "mobile(String)" when parsing, or "String mobile()" when writing, * from a Contact attribute inside a Customer class.columnIndex- the position of the column that: (a) when parsing, will be read from to invoke given setter method of an object; (b) when writing, will receive the value returned by the given getter method of an object;
-
methodsToColumnNames
public void methodsToColumnNames(java.util.Map<java.lang.String,java.lang.String> mappings)
Description copied from interface:ColumnMapperMaps multiple methods to multiple column names.- Specified by:
methodsToColumnNamesin interfaceColumnMapper- Parameters:
mappings- a map of methods names associated with a corresponding column name, where:- Each key is the name of a method. Use the dot character to access attributes of nested objects,
e.g.
contact.mobilewill target the method "mobile(String)" when parsing, or "String mobile()" when writing, from a Contact attribute inside a Customer class. - Each value is the name of the column that: (a) when parsing, will be read from to invoke given setter method of an object; (b) when writing, will receive the value returned by the given getter method of an object;
- Each key is the name of a method. Use the dot character to access attributes of nested objects,
e.g.
-
methodsToColumns
public void methodsToColumns(java.util.Map<java.lang.String,java.lang.Enum<?>> mappings)
Description copied from interface:ColumnMapperMaps multiple methods to multiple columns.- Specified by:
methodsToColumnsin interfaceColumnMapper- Parameters:
mappings- a map of methods names associated with a corresponding column, where:- Each key is the name of a method. Use the dot character to access attributes of nested objects,
e.g.
contact.mobilewill target the method "mobile(String)" when parsing, or "String mobile()" when writing, from a Contact attribute inside a Customer class. - Each value is an enumeration representing the column that: (a) when parsing, will be read from to invoke given setter method of an object; (b) when writing, will receive the value returned by the given getter method of an object;
- Each key is the name of a method. Use the dot character to access attributes of nested objects,
e.g.
-
methodsToIndexes
public void methodsToIndexes(java.util.Map<java.lang.String,java.lang.Integer> mappings)
Description copied from interface:ColumnMapperMaps multiple methods to multiple column positions.- Specified by:
methodsToIndexesin interfaceColumnMapper- Parameters:
mappings- a map of methods names associated with a corresponding column position, where:- Each key is the name of a method. Use the dot character to access attributes of nested objects,
e.g.
contact.mobilewill target the method "mobile(String)" when parsing, or "String mobile()" when writing, from a Contact attribute inside a Customer class. - Each value is an integer representing the position of the column that: (a) when parsing, will be read from to invoke given setter method of an object; (b) when writing, will receive the value returned by the given getter method of an object;
- Each key is the name of a method. Use the dot character to access attributes of nested objects,
e.g.
-
remove
public void remove(java.lang.String methodOrAttributeName)
Description copied from interface:ColumnMapperRemoves any mappings that target a given method or attribute name.- Specified by:
removein interfaceColumnMapper- Parameters:
methodOrAttributeName- the name of the method or attribute to be removed.
-
methodToColumnName
public void methodToColumnName(java.lang.String setterName, java.lang.Class<?> parameterType, java.lang.String columnName)Description copied from interface:ColumnMapperMaps a setter method to a column name. Use whenColumnMapper.methodToColumnName(String, String)is not enough to uniquely identify the method you need (e.g. when there are overloaded methods with different parameter types) Used only for parsing. Will be ignored when writing.- Specified by:
methodToColumnNamein interfaceColumnMapper- Parameters:
setterName- the name of the setter method. Use the dot character to access methods of nested objects, e.g.contact.mobilewill target the setter method "mobile(String)" from a Contact attribute inside a Customer class.parameterType- the type of the parameter used in the given setter name.columnName- the name of the column that when parsing, will be read from to invoke given setter method of an object
-
methodToColumn
public void methodToColumn(java.lang.String setterName, java.lang.Class<?> parameterType, java.lang.Enum<?> column)Description copied from interface:ColumnMapperMaps a setter method to a column. Use whenColumnMapper.methodToColumnName(String, String)is not enough to uniquely identify the method you need (e.g. when there are overloaded methods with different parameter types) Used only for parsing. Will be ignored when writing.- Specified by:
methodToColumnin interfaceColumnMapper- Parameters:
setterName- the name of the setter method. Use the dot character to access methods of nested objects, e.g.contact.mobilewill target the setter method "mobile(String)" from a Contact attribute inside a Customer class.parameterType- the type of the parameter used in the given setter name.column- an enumeration representing the column that when parsing, will be read from to invoke given setter method of an object
-
methodToIndex
public void methodToIndex(java.lang.String setterName, java.lang.Class<?> parameterType, int columnIndex)Description copied from interface:ColumnMapperMaps a setter method to a column position. Use whenColumnMapper.methodToColumnName(String, String)is not enough to uniquely identify the method you need (e.g. when there are overloaded methods with different parameter types) Used only for parsing. Will be ignored when writing.- Specified by:
methodToIndexin interfaceColumnMapper- Parameters:
setterName- the name of the setter method. Use the dot character to access methods of nested objects, e.g.contact.mobilewill target the setter method "mobile(String)" from a Contact attribute inside a Customer class.parameterType- the type of the parameter used in the given setter name.columnIndex- the position of the column that when parsing, will be read from to invoke given setter method of an object
-
getNestedAttributeNames
public java.util.Set<java.lang.String> getNestedAttributeNames()
Returns the first-level names of all nested members whose attributes or methods have been mapped- Returns:
- the names of nested objects to visit from the current object
-
clone
public ColumnMapper clone()
Description copied from interface:ColumnMapperCreates a deep copy of this object with all its mappings. Changes to the clone won't affect the original instance.- Specified by:
clonein interfaceColumnMapper- Overrides:
clonein classjava.lang.Object- Returns:
- a clone of the current mappings.
-
-