Package com.opencsv.bean
Class HeaderIndex
- java.lang.Object
-
- com.opencsv.bean.HeaderIndex
-
public class HeaderIndex extends java.lang.ObjectA bi-directional mapping between column positions and header names. A simpleBidiMapwill 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
Fields Modifier and Type Field Description private org.apache.commons.collections4.MultiValuedMap<java.lang.String,java.lang.Integer>headerToPositionThe uni-directional map from header name to (possibly multiple) column positions.private java.lang.String[]positionToHeaderThe uni-directional map from column position to header name.
-
Constructor Summary
Constructors Constructor Description HeaderIndex()Useless but explicit nullary constructor to make the style checker happy.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Empties the entire mapping.intfindMaxIndex()Finds and returns the highest index in this mapping.int[]getByName(java.lang.String headerName)Retrieves the column position(s) associated with the given header name.java.lang.StringgetByPosition(int i)Retrieves the header associated with the given column position.java.lang.String[]getHeaderIndex()intgetHeaderIndexLength()voidinitializeHeaderIndex(java.lang.String[] header)Initializes the index with a list of header names in proper encounter order.booleanisEmpty()voidput(int k, java.lang.String v)Adds a new mapping between a column position and a header.
-
-
-
Field Detail
-
positionToHeader
private java.lang.String[] positionToHeader
The uni-directional map from column position to header name.
-
headerToPosition
private org.apache.commons.collections4.MultiValuedMap<java.lang.String,java.lang.Integer> headerToPosition
The uni-directional map from header name to (possibly multiple) column positions.
-
-
Method Detail
-
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(java.lang.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 withCsvBindAndJoinByPositionorCsvBindAndJoinByName. Values ofnullindicate 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(java.lang.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 java.lang.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 java.lang.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, java.lang.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 mappingv- The header to be associated with the column position
-
-