Class FieldMapByName<T>

  • Type Parameters:
    T - Type of the bean being converted
    All Implemented Interfaces:
    FieldMap<java.lang.String,​java.lang.String,​RegexToBeanField<T>,​T>

    public class FieldMapByName<T>
    extends AbstractFieldMap<java.lang.String,​java.lang.String,​RegexToBeanField<T>,​T>
    This class maintains a mapping from header names out of a CSV file to bean fields. Simple entries are matched using string equality. Complex entries are matched using regular expressions.
    Since:
    4.2
    Author:
    Andrew Rucker Jones
    • Constructor Detail

      • FieldMapByName

        public FieldMapByName​(java.util.Locale errorLocale)
        Initializes this FieldMap.
        Parameters:
        errorLocale - The locale to be used for error messages
    • Method Detail

      • putComplex

        public void putComplex​(java.lang.String key,
                               BeanField<T,​java.lang.String> value)
        Description copied from interface: FieldMap
        Adds a BeanField to this map indexed by the data in initializer. This is what makes this map special: It allows one to define characteristics of a method to match multiple keys.
        Parameters:
        key - A regular expression matching header names
        value - The BeanField that is to be returned on a later match
      • determineMissingRequiredHeaders

        public java.util.List<FieldMapByNameEntry<T>> determineMissingRequiredHeaders​(java.lang.String[] headersPresent)
        Returns a list of required headers that were not present in the input.
        Parameters:
        headersPresent - An array of all headers present from the input
        Returns:
        A list of name + field for all of the required headers that were not found
      • generateHeader

        public java.lang.String[] generateHeader​(T bean)
                                          throws CsvRequiredFieldEmptyException
        This method generates a header that can be used for writing beans of the type provided back to a file.

        The ordering of the headers is determined by the Comparator passed in to setColumnOrderOnWrite(Comparator), should that method be called, otherwise the natural ordering is used (alphabetically ascending).

        This implementation will not write headers discovered in multi-valued bean fields if the headers would not be matched by the bean field on reading. There are two reasons for this:

        1. opencsv always tries to create data that are round-trip equivalent, and that would not be the case if it generated data on writing that it would discard on reading.
        2. As the code is currently written, the header name is used on writing each bean field to determine the appropriate BeanField for information concerning conversions, locales, necessity (whether or not the field is required). Without this information, conversion is impossible, and every value written under the unmatched header is blank, regardless of the contents of the bean.
        Parameters:
        bean - One perfect, shining example of how the bean to be written should look. The most crucial thing is, for fields that result from joining multiple fields on reading and thus need to be split on writing, the MultiValuedMap in question must have the complete structure of the header to be generated, even if some values are empty.
        Returns:
        An array of header names for the output file
        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.
      • setColumnOrderOnWrite

        public void setColumnOrderOnWrite​(java.util.Comparator<java.lang.String> writeOrder)
        Sets the Comparator to be used to sort columns when writing beans to a CSV file.
        Parameters:
        writeOrder - The Comparator to use. May be null, in which case the natural ordering is used.
        Since:
        4.3