View Javadoc
1   package org.csveed.row;
2   
3   import java.util.HashMap;
4   import java.util.Iterator;
5   import java.util.Map;
6   
7   import org.csveed.api.Header;
8   import org.csveed.common.Column;
9   import org.csveed.report.CsvException;
10  import org.csveed.report.GeneralError;
11  import org.csveed.report.RowReport;
12  
13  public class HeaderImpl implements Header {
14  
15      private Line header;
16      private Map<Column, String> indexToName = new HashMap<>();
17      private Map<String, Column> nameToIndex = new HashMap<>();
18  
19      public HeaderImpl(Line row) {
20          this.header = row;
21          ColumnColumn currentColumn = new Column();
22          for (String headerCell : header) {
23              this.indexToName.put(currentColumn, headerCell);
24              this.nameToIndex.put(headerCell.toLowerCase(), currentColumn);
25              currentColumn = currentColumn.nextColumn();
26          }
27      }
28  
29      @Override
30      public int size() {
31          return header.size();
32      }
33  
34      @Override
35      public String getName(int columnIndex) {
36          Columnolumn">Column column = new Column(columnIndex);
37          String name = this.indexToName.get(column);
38          if (name == null) {
39              throw new CsvException(new GeneralError("No column name found for index "+column.getColumnIndex()));
40          }
41          return name;
42      }
43  
44      @Override
45      public int getIndex(String columnName) {
46          Column column = this.nameToIndex.get(columnName.toLowerCase());
47          if (column == null) {
48              throw new CsvException(new GeneralError("No column index found for name "+columnName));
49          }
50          return column.getColumnIndex();
51      }
52  
53      @Override
54      public Iterator<String> iterator() {
55          return header.iterator();
56      }
57  
58      @Override
59      public RowReport reportOnEndOfLine() {
60          return header.reportOnEndOfLine();
61      }
62  
63  }