Class ColumnMapping

java.lang.Object
com.univocity.parsers.common.fields.ColumnMapping
All Implemented Interfaces:
ColumnMapper, Cloneable

public final class ColumnMapping extends Object implements ColumnMapper
Implementation the ColumnMapper interface which allows users to manually define mappings from attributes/methods of a given class to columns to be parsed or written.
See Also:
  • Field Details

  • Constructor Details

    • ColumnMapping

      public ColumnMapping()
      Creates a new column mapping instance
    • ColumnMapping

      public ColumnMapping(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 names
      parent - the mappings of the parent object in the nested structure.
  • Method Details

    • getCurrentAttributePrefix

      private static String getCurrentAttributePrefix(String prefix, String name)
    • attributeToColumnName

      public void attributeToColumnName(String attributeName, String columnName)
      Description copied from interface: ColumnMapper
      Maps an attribute to a column name.
      Specified by:
      attributeToColumnName in interface ColumnMapper
      Parameters:
      attributeName - the name of the attribute. Use the dot character to access attributes of nested objects, e.g. contact.mobile will 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(String attributeName, Enum<?> column)
      Description copied from interface: ColumnMapper
      Maps an attribute to a column.
      Specified by:
      attributeToColumn in interface ColumnMapper
      Parameters:
      attributeName - the name of the attribute. Use the dot character to access attributes of nested objects, e.g. contact.mobile will 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(String attributeName, int columnIndex)
      Description copied from interface: ColumnMapper
      Maps an attribute to a column position.
      Specified by:
      attributeToIndex in interface ColumnMapper
      Parameters:
      attributeName - the name of the attribute. Use the dot character to access attributes of nested objects, e.g. contact.mobile will 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(Map<String,String> mappings)
      Description copied from interface: ColumnMapper
      Maps multiple attributes to multiple column names.
      Specified by:
      attributesToColumnNames in interface ColumnMapper
      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.mobile will 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;
    • attributesToColumns

      public void attributesToColumns(Map<String,Enum<?>> mappings)
      Description copied from interface: ColumnMapper
      Maps multiple attributes to multiple columns.
      Specified by:
      attributesToColumns in interface ColumnMapper
      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.mobile will 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;
    • attributesToIndexes

      public void attributesToIndexes(Map<String,Integer> mappings)
      Description copied from interface: ColumnMapper
      Maps multiple attributes to multiple column positions.
      Specified by:
      attributesToIndexes in interface ColumnMapper
      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.mobile will 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;
    • methodToColumnName

      private void methodToColumnName(MethodDescriptor method, String columnName)
    • methodToColumn

      private void methodToColumn(MethodDescriptor method, Enum<?> column)
    • methodToIndex

      private void methodToIndex(MethodDescriptor method, int columnIndex)
    • isMapped

      public boolean isMapped(MethodDescriptor method, String targetName)
      Tests whether a method or attribute has been mapped to a column.
      Parameters:
      method - a descriptor of getter/setter methods (can be null)
      targetName - name of a method or attribute
      Returns:
      true if the given method or attribute has been mapped to a column
    • updateMapping

      public boolean updateMapping(FieldMapping fieldMapping, 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 column
      targetName - name of a method or attribute
      method - a descriptor of getter/setter methods (can be null)
      Returns:
      true if the mapping has been successfully updated.
    • getPrefix

      public 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(String methodName, String columnName)
      Description copied from interface: ColumnMapper
      Maps 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, use ColumnMapper.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:
      methodToColumnName in interface ColumnMapper
      Parameters:
      methodName - the name of the method. Use the dot character to access methods of nested objects, e.g. contact.mobile will 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(String methodName, Enum<?> column)
      Description copied from interface: ColumnMapper
      Maps 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, use ColumnMapper.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:
      methodToColumn in interface ColumnMapper
      Parameters:
      methodName - the name of the method. Use the dot character to access methods of nested objects, * e.g. contact.mobile will 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(String methodName, int columnIndex)
      Description copied from interface: ColumnMapper
      Maps 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, use ColumnMapper.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:
      methodToIndex in interface ColumnMapper
      Parameters:
      methodName - the name of the method. Use the dot character to access methods of nested objects, * e.g. contact.mobile will 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(Map<String,String> mappings)
      Description copied from interface: ColumnMapper
      Maps multiple methods to multiple column names.
      Specified by:
      methodsToColumnNames in interface ColumnMapper
      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.mobile will 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;
    • methodsToColumns

      public void methodsToColumns(Map<String,Enum<?>> mappings)
      Description copied from interface: ColumnMapper
      Maps multiple methods to multiple columns.
      Specified by:
      methodsToColumns in interface ColumnMapper
      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.mobile will 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;
    • methodsToIndexes

      public void methodsToIndexes(Map<String,Integer> mappings)
      Description copied from interface: ColumnMapper
      Maps multiple methods to multiple column positions.
      Specified by:
      methodsToIndexes in interface ColumnMapper
      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.mobile will 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;
    • remove

      public void remove(String methodOrAttributeName)
      Description copied from interface: ColumnMapper
      Removes any mappings that target a given method or attribute name.
      Specified by:
      remove in interface ColumnMapper
      Parameters:
      methodOrAttributeName - the name of the method or attribute to be removed.
    • methodToColumnName

      public void methodToColumnName(String setterName, Class<?> parameterType, String columnName)
      Description copied from interface: ColumnMapper
      Maps a setter method to a column name. Use when ColumnMapper.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:
      methodToColumnName in interface ColumnMapper
      Parameters:
      setterName - the name of the setter method. Use the dot character to access methods of nested objects, e.g. contact.mobile will 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(String setterName, Class<?> parameterType, Enum<?> column)
      Description copied from interface: ColumnMapper
      Maps a setter method to a column. Use when ColumnMapper.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:
      methodToColumn in interface ColumnMapper
      Parameters:
      setterName - the name of the setter method. Use the dot character to access methods of nested objects, e.g. contact.mobile will 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(String setterName, Class<?> parameterType, int columnIndex)
      Description copied from interface: ColumnMapper
      Maps a setter method to a column position. Use when ColumnMapper.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:
      methodToIndex in interface ColumnMapper
      Parameters:
      setterName - the name of the setter method. Use the dot character to access methods of nested objects, e.g. contact.mobile will 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 Set<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: ColumnMapper
      Creates a deep copy of this object with all its mappings. Changes to the clone won't affect the original instance.
      Specified by:
      clone in interface ColumnMapper
      Overrides:
      clone in class Object
      Returns:
      a clone of the current mappings.