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.common;
12  
13  import java.util.Locale;
14  
15  import org.csveed.api.Header;
16  import org.csveed.report.CsvException;
17  import org.csveed.report.GeneralError;
18  
19  /**
20   * The Class Column.
21   */
22  public class Column implements Comparable<Column> {
23  
24      /** The Constant FIRST_COLUMN_INDEX. */
25      public static final int FIRST_COLUMN_INDEX = 1;
26  
27      /** The column index. */
28      private int columnIndex = -1;
29  
30      /** The column name. */
31      private String columnName;
32  
33      /** The header. */
34      private Header header;
35  
36      /** The key. */
37      private ColumnKey key;
38  
39      /**
40       * Instantiates a new column.
41       *
42       * @param columnName
43       *            the column name
44       */
45      public Column(String columnName) {
46          setColumnName(columnName);
47      }
48  
49      /**
50       * Instantiates a new column.
51       */
52      public Column() {
53          this(FIRST_COLUMN_INDEX);
54      }
55  
56      /**
57       * Instantiates a new column.
58       *
59       * @param column
60       *            the column
61       */
62      public Column(Column column) {
63          setColumnIndex(column.getColumnIndex());
64      }
65  
66      /**
67       * Instantiates a new column.
68       *
69       * @param columnIndex
70       *            the column index
71       */
72      public Column(int columnIndex) {
73          if (columnIndex <= 0) {
74              throw new CsvException(
75                      new GeneralError("Column index cannot be set at 0 or lower. Column indexes are 1-based"));
76          }
77          setColumnIndex(columnIndex);
78      }
79  
80      /**
81       * Column index to excel column.
82       *
83       * @param columnIndex
84       *            the column index
85       *
86       * @return the string
87       */
88      private String columnIndexToExcelColumn(int columnIndex) {
89          StringBuilder excelColumn = new StringBuilder();
90          while (columnIndex % 26 > 0) {
91              excelColumn.insert(0, (char) (columnIndex % 26 + 'A' - 1));
92              columnIndex /= 26;
93          }
94          return excelColumn.toString();
95      }
96  
97      /**
98       * Sets the header.
99       *
100      * @param header
101      *            the header
102      *
103      * @return the column
104      */
105     public Column setHeader(Header header) {
106         this.header = header;
107         if (this.header != null) {
108             setColumnName(header.getName(this.columnIndex));
109         }
110         return this;
111     }
112 
113     /**
114      * Sets the column index.
115      *
116      * @param columnIndex
117      *            the new column index
118      */
119     public void setColumnIndex(int columnIndex) {
120         this.columnIndex = columnIndex;
121         setKey(new ColumnIndexKey(this.columnIndex));
122     }
123 
124     /**
125      * Sets the column name.
126      *
127      * @param columnName
128      *            the new column name
129      */
130     public void setColumnName(String columnName) {
131         this.columnName = columnName.toLowerCase(Locale.getDefault());
132         setKey(new ColumnNameKey(this.columnName));
133     }
134 
135     /**
136      * Sets the key.
137      *
138      * @param key
139      *            the new key
140      */
141     public void setKey(ColumnKey key) {
142         this.key = key;
143     }
144 
145     /**
146      * Gets the excel column.
147      *
148      * @return the excel column
149      */
150     public String getExcelColumn() {
151         return columnIndexToExcelColumn(this.columnIndex);
152     }
153 
154     /**
155      * Gets the column index.
156      *
157      * @return the column index
158      */
159     public int getColumnIndex() {
160         return this.columnIndex;
161     }
162 
163     /**
164      * Gets the column name.
165      *
166      * @return the column name
167      */
168     public String getColumnName() {
169         return this.columnName;
170     }
171 
172     /**
173      * Next column.
174      *
175      * @return the column
176      */
177     public Column nextColumn() {
178         return new Column(this.getColumnIndex() + 1).setHeader(header);
179     }
180 
181     /**
182      * Next line.
183      *
184      * @return the column
185      */
186     public Column nextLine() {
187         return new Column().setHeader(header);
188     }
189 
190     /**
191      * Gets the column text.
192      *
193      * @return the column text
194      */
195     public String getColumnText() {
196         return (columnIndex == -1 ? "" : " index " + columnIndex + " (" + getExcelColumn() + ")")
197                 + (columnName == null ? "" : " name \"" + columnName + "\"");
198     }
199 
200     @Override
201     public boolean equals(Object obj) {
202         if (!(obj instanceof Column)) {
203             return false;
204         }
205         return this.key.equals(((Column) obj).key);
206     }
207 
208     @Override
209     public int hashCode() {
210         return this.key.hashCode();
211     }
212 
213     @Override
214     public String toString() {
215         return this.key.toString();
216     }
217 
218     @Override
219     public int compareTo(Column column) {
220         return this.key.compareTo(column.key);
221     }
222 }