Class HeaderIndex

java.lang.Object
com.opencsv.bean.HeaderIndex

public class HeaderIndex extends Object
A bi-directional mapping between column positions and header names. A simple BidiMap will not do the trick, because header names (or null in place of a header name) may appear more than once.
Since:
4.2
  • Field Details

    • positionToHeader

      private String[] positionToHeader
      The uni-directional map from column position to header name.
    • headerToPosition

      private org.apache.commons.collections4.MultiValuedMap<String,Integer> headerToPosition
      The uni-directional map from header name to (possibly multiple) column positions.
  • Constructor Details

    • HeaderIndex

      public HeaderIndex()
      Useless but explicit nullary constructor to make the style checker happy.
  • Method Details

    • clear

      public void clear()
      Empties the entire mapping.
    • findMaxIndex

      public int findMaxIndex()
      Finds and returns the highest index in this mapping.
      Returns:
      The maximum index that is mapped and will return a header name (or null if specifically mapped that way). If there are no columns in the mapping, returns -1.
    • initializeHeaderIndex

      public void initializeHeaderIndex(String[] header)
      Initializes the index with a list of header names in proper encounter order. "Proper encounter order" means the order in which they are expected to be found in the input CSV. Header names may be listed more than once if the destination field is annotated with CsvBindAndJoinByPosition or CsvBindAndJoinByName. Values of null indicate the column from the input should not be mapped to a bean field.
      Parameters:
      header - A list of header names in the order in which they are expected in the CSV input
    • isEmpty

      public boolean isEmpty()
      Returns:
      Whether or not the mapping is empty
    • getByName

      public int[] getByName(String headerName)
      Retrieves the column position(s) associated with the given header name.
      Parameters:
      headerName - The header name for which the associated column positions should be returned
      Returns:
      The column positions associated with headerName
    • getByPosition

      public String getByPosition(int i)
      Retrieves the header associated with the given column position.
      Parameters:
      i - The column position for which the header name is to be retrieved
      Returns:
      The header name mapped by position i
    • getHeaderIndex

      public String[] getHeaderIndex()
      Returns:
      The current list of headers mapped by this index in the proper order
    • getHeaderIndexLength

      public int getHeaderIndexLength()
      Returns:
      The length of the current mapping, including all fields unmapped
    • put

      public void put(int k, String v)
      Adds a new mapping between a column position and a header. The header may already be present, in which case the column position is added to the list of column positions mapped to the header.
      Parameters:
      k - The column position for the mapping
      v - The header to be associated with the column position