Top Description Fields Constructors Methods
com.opencsv.bean

public Class HeaderIndex

extends Object
Class Inheritance
Imports
java.util.Arrays, .Collection, org.apache.commons.collections4.MultiValuedMap, org.apache.commons.collections4.multimap.ArrayListValuedHashMap, org.apache.commons.lang3.ArrayUtils

A bi-directional mapping between column positions and header names. A simple org.apache.commons.collections4.BidiMap will not do the trick, because header names (or null in place of a header name) may appear more than once.
Author
Andrew Rucker Jones
Since
4.2

Field Summary

Modifier and TypeField and Description
private MultiValuedMap<String, Integer>
headerToPosition

The uni-directional map from header name to (possibly multiple) column positions.

private String[]
positionToHeader

The uni-directional map from column position to header name.

Constructor Summary

AccessConstructor and Description
public
HeaderIndex()

Useless but explicit nullary constructor to make the style checker happy.

Method Summary

Modifier and TypeMethod and Description
public void
clear()

Empties the entire mapping.

public int

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.
findMaxIndex
()

Finds and returns the highest index in this mapping.

public int[]

Returns:

The column positions associated with headerName
getByName
(String
The header name for which the associated column positions should be returned
headerName
)

Retrieves the column position(s) associated with the given header name.

public String

Returns:

The header name mapped by position i
getByPosition
(int
The column position for which the header name is to be retrieved
i
)

Retrieves the header associated with the given column position.

public String[]
getHeaderIndex()

Returns: The current list of headers mapped by this index in the proper order

public int
getHeaderIndexLength()

Returns: The length of the current mapping, including all fields unmapped

public void
initializeHeaderIndex(String[]
A list of header names in the order in which they are expected in the CSV input
header
)

Initializes the index with a list of header names in proper encounter order.

public boolean
isEmpty()

Returns: Whether or not the mapping is empty

public void
put(int
The column position for the mapping
k
,
String
The header to be associated with the column position
v
)

Adds a new mapping between a column position and a header.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

headerToPositionback to summary
private MultiValuedMap<String, Integer> headerToPosition

The uni-directional map from header name to (possibly multiple) column positions.

positionToHeaderback to summary
private String[] positionToHeader

The uni-directional map from column position to header name.

Constructor Detail

HeaderIndexback to summary
public HeaderIndex()

Useless but explicit nullary constructor to make the style checker happy.

Method Detail

clearback to summary
public void clear()

Empties the entire mapping.

findMaxIndexback to summary
public int findMaxIndex()

Finds and returns the highest index in this mapping.

Returns:int

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.

getByNameback to summary
public int[] getByName(String headerName)

Retrieves the column position(s) associated with the given header name.

Parameters
headerName:String

The header name for which the associated column positions should be returned

Returns:int[]

The column positions associated with headerName

getByPositionback to summary
public String getByPosition(int i)

Retrieves the header associated with the given column position.

Parameters
i:int

The column position for which the header name is to be retrieved

Returns:String

The header name mapped by position i

getHeaderIndexback to summary
public String[] getHeaderIndex()
Returns:String[]

The current list of headers mapped by this index in the proper order

getHeaderIndexLengthback to summary
public int getHeaderIndexLength()
Returns:int

The length of the current mapping, including all fields unmapped

initializeHeaderIndexback to summary
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:String[]

A list of header names in the order in which they are expected in the CSV input

isEmptyback to summary
public boolean isEmpty()
Returns:boolean

Whether or not the mapping is empty

putback to summary
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:int

The column position for the mapping

v:String

The header to be associated with the column position