View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.row;
12  
13  import java.util.Iterator;
14  
15  import org.csveed.api.Header;
16  import org.csveed.api.Row;
17  import org.csveed.common.Column;
18  import org.csveed.report.CsvException;
19  import org.csveed.report.GeneralError;
20  import org.csveed.report.RowReport;
21  
22  /**
23   * The Class RowImpl.
24   */
25  public class RowImpl implements Row {
26  
27      /** The line. */
28      private Line line;
29  
30      /** The header. */
31      private Header header;
32  
33      /**
34       * Instantiates a new row impl.
35       *
36       * @param line
37       *            the line
38       * @param header
39       *            the header
40       */
41      public RowImpl(Line line, Header header) {
42          this.line = line;
43          this.header = header;
44      }
45  
46      @Override
47      public Header getHeader() {
48          if (this.header == null) {
49              throw new CsvException(new GeneralError(
50                      "No header has been found for this file. Set @CsvFile#useHeaders to read the header"));
51          }
52          return this.header;
53      }
54  
55      @Override
56      public RowReport reportOnEndOfLine() {
57          return line.reportOnEndOfLine();
58      }
59  
60      @Override
61      public RowReport reportOnColumn(int columnIndex) {
62          return line.reportOnColumn(new Column(columnIndex));
63      }
64  
65      @Override
66      public String get(String columnName) {
67          return line.get(header.getIndex(columnName) - 1);
68      }
69  
70      @Override
71      public String getColumnName(int columnIndex) {
72          return getHeader().getName(columnIndex);
73      }
74  
75      @Override
76      public boolean hasHeader() {
77          return header != null;
78      }
79  
80      @Override
81      public int size() {
82          return line.size();
83      }
84  
85      @Override
86      public String get(int columnIndex) {
87          return line.get(columnIndex - 1);
88      }
89  
90      /**
91       * Returns an iterator over the individual cells of a Row
92       *
93       * @return iterator over the cells in String format
94       */
95      @Override
96      public Iterator<String> iterator() {
97          return line.iterator();
98      }
99  
100 }