Package com.opencsv.bean
Class HeaderIndex
java.lang.Object
com.opencsv.bean.HeaderIndex
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 Summary
FieldsModifier and TypeFieldDescriptionThe uni-directional map from header name to (possibly multiple) column positions.private String[]
The uni-directional map from column position to header name. -
Constructor Summary
ConstructorsConstructorDescriptionUseless but explicit nullary constructor to make the style checker happy. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Empties the entire mapping.int
Finds and returns the highest index in this mapping.int[]
Retrieves the column position(s) associated with the given header name.getByPosition
(int i) Retrieves the header associated with the given column position.String[]
int
void
initializeHeaderIndex
(String[] header) Initializes the index with a list of header names in proper encounter order.boolean
isEmpty()
void
Adds a new mapping between a column position and a header.
-
Field Details
-
positionToHeader
The uni-directional map from column position to header name. -
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
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 withCsvBindAndJoinByPosition
orCsvBindAndJoinByName
. Values ofnull
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
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
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
- 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
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 mappingv
- The header to be associated with the column position
-