Class ColumnPositionMappingStrategy<T>

  • Type Parameters:
    T - Type of object that is being processed.
    All Implemented Interfaces:
    MappingStrategy<T>

    public class ColumnPositionMappingStrategy<T>
    extends AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
    Allows for the mapping of columns with their positions. Using this strategy without annotations (CsvBindByPosition or CsvCustomBindByPosition) requires all the columns to be present in the CSV file and for them to be in a particular order. Using annotations allows one to specify arbitrary zero-based column numbers for each bean member variable to be filled. Also this strategy requires that the file does NOT have a header. That said, the main use of this strategy is files that do not have headers.
    • Constructor Detail

      • ColumnPositionMappingStrategy

        public ColumnPositionMappingStrategy()
        Default constructor.
    • Method Detail

      • captureHeader

        public void captureHeader​(CSVReader reader)
                           throws java.io.IOException
        There is no header per se for this mapping strategy, but this method checks the first line to determine how many fields are present and adjusts its field map accordingly.
        Parameters:
        reader - The CSVReader to use for header parsing
        Throws:
        java.io.IOException - If parsing fails
      • findField

        protected BeanField<T,​java.lang.Integer> findField​(int col)
        Description copied from class: AbstractMappingStrategy
        Gets the field for a given column position.
        Specified by:
        findField in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​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
      • generateHeader

        public java.lang.String[] generateHeader​(T bean)
                                          throws CsvRequiredFieldEmptyException
        This method returns an empty array. The column position mapping strategy assumes that there is no header, and thus it also does not write one, accordingly.
        Specified by:
        generateHeader in interface MappingStrategy<T>
        Overrides:
        generateHeader in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
        Parameters:
        bean - One fully populated bean from which the header can be derived. This is important in the face of joining and splitting. If we have a MultiValuedMap as a field that is the target for a join on reading, that same field must be split into multiple columns on writing. Since the joining is done via regular expressions, it is impossible for opencsv to know what the column names are supposed to be on writing unless this bean includes a fully populated map.
        Returns:
        An empty array
        Throws:
        CsvRequiredFieldEmptyException - If a required header is missing while attempting to write. Since every other header is hard-wired through the bean fields and their associated annotations, this can only happen with multi-valued fields.
      • getColumnName

        public java.lang.String getColumnName​(int col)
        Gets a column name.
        Parameters:
        col - Position of the column.
        Returns:
        Column name or null if col > number of mappings.
      • getColumnMapping

        public java.lang.String[] getColumnMapping()
        Retrieves the column mappings.
        Returns:
        String array with the column mappings.
      • setColumnMapping

        public void setColumnMapping​(java.lang.String... columnMapping)
        Setter for the column mapping. This mapping is for reading. Use of this method in conjunction with writing is undefined.
        Parameters:
        columnMapping - Column names to be mapped.
      • loadAnnotatedFieldMap

        protected void loadAnnotatedFieldMap​(org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,​java.lang.reflect.Field> fields)
        Creates a map of annotated fields in the bean to be processed.

        This method is called by AbstractMappingStrategy.loadFieldMap() when at least one relevant annotation is found on a member variable.

        Overrides:
        loadAnnotatedFieldMap in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
        Parameters:
        fields - A list of fields annotated with a binding annotation in the bean to be processed
      • loadUnadornedFieldMap

        protected void loadUnadornedFieldMap​(org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,​java.lang.reflect.Field> fields)
        Description copied from class: AbstractMappingStrategy
        Creates a map of fields in the bean to be processed that have no annotations. This method is called by AbstractMappingStrategy.loadFieldMap() when absolutely no annotations that are relevant for this mapping strategy are found in the type of bean being processed.
        Specified by:
        loadUnadornedFieldMap in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
        Parameters:
        fields - A list of all non-synthetic fields in the bean to be processed
      • initializeFieldMap

        protected void initializeFieldMap()
        Description copied from class: AbstractMappingStrategy
        Creates 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:
        initializeFieldMap in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
      • verifyLineLength

        protected void verifyLineLength​(int numberOfFields)
                                 throws CsvRequiredFieldEmptyException
        Description copied from class: AbstractMappingStrategy
        Must 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:
        verifyLineLength in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
        Parameters:
        numberOfFields - The number of fields present in the line of input
        Throws:
        CsvRequiredFieldEmptyException - If a required column is missing
      • findHeader

        public java.lang.String findHeader​(int col)
        Returns the column position for the given column number. Yes, they're the same thing. For this mapping strategy, it's a simple conversion from an integer to a string.
        Specified by:
        findHeader in class AbstractMappingStrategy<java.lang.String,​java.lang.Integer,​ComplexFieldMapEntry<java.lang.String,​java.lang.Integer,​T>,​T>
        Parameters:
        col - The column number for which the header is sought
        Returns:
        The name of the header
      • setColumnOrderOnWrite

        public void setColumnOrderOnWrite​(java.util.Comparator<java.lang.Integer> writeOrder)
        Sets the Comparator to 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 - The Comparator to use. May be null, in which case the natural ordering is used.
        Since:
        4.3