1
2
3
4
5
6
7
8
9
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
21
22 public class Column implements Comparable<Column> {
23
24
25 public static final int FIRST_COLUMN_INDEX = 1;
26
27
28 private int columnIndex = -1;
29
30
31 private String columnName;
32
33
34 private Header header;
35
36
37 private ColumnKey key;
38
39
40
41
42
43
44
45 public Column(String columnName) {
46 setColumnName(columnName);
47 }
48
49
50
51
52 public Column() {
53 this(FIRST_COLUMN_INDEX);
54 }
55
56
57
58
59
60
61
62 public Column(Column column) {
63 setColumnIndex(column.getColumnIndex());
64 }
65
66
67
68
69
70
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
82
83
84
85
86
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
99
100
101
102
103
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
115
116
117
118
119 public void setColumnIndex(int columnIndex) {
120 this.columnIndex = columnIndex;
121 setKey(new ColumnIndexKey(this.columnIndex));
122 }
123
124
125
126
127
128
129
130 public void setColumnName(String columnName) {
131 this.columnName = columnName.toLowerCase(Locale.getDefault());
132 setKey(new ColumnNameKey(this.columnName));
133 }
134
135
136
137
138
139
140
141 public void setKey(ColumnKey key) {
142 this.key = key;
143 }
144
145
146
147
148
149
150 public String getExcelColumn() {
151 return columnIndexToExcelColumn(this.columnIndex);
152 }
153
154
155
156
157
158
159 public int getColumnIndex() {
160 return this.columnIndex;
161 }
162
163
164
165
166
167
168 public String getColumnName() {
169 return this.columnName;
170 }
171
172
173
174
175
176
177 public Column nextColumn() {
178 return new Column(this.getColumnIndex() + 1).setHeader(header);
179 }
180
181
182
183
184
185
186 public Column nextLine() {
187 return new Column().setHeader(header);
188 }
189
190
191
192
193
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 }