View Javadoc
1   package org.csveed.common;
2   
3   import org.csveed.api.Header;
4   import org.csveed.report.CsvException;
5   import org.csveed.report.GeneralError;
6   
7   public class Column implements Comparable<Column> {
8   
9       public static final int FIRST_COLUMN_INDEX = 1;
10  
11      private int columnIndex = -1;
12  
13      private String columnName;
14  
15      private Header header;
16  
17      private ColumnKey key;
18  
19      public Column(String columnName) {
20          setColumnName(columnName);
21      }
22  
23      public Column() {
24          this(FIRST_COLUMN_INDEX);
25      }
26  
27      public Columnmn" href="../../../org/csveed/common/Column.html#Column">Column(Column column) {
28          setColumnIndex(column.getColumnIndex());
29      }
30  
31      public Column(int columnIndex) {
32          if (columnIndex <= 0) {
33              throw new CsvException(new GeneralError("Column index cannot be set at 0 or lower. Column indexes are 1-based"));
34          }
35          setColumnIndex(columnIndex);
36      }
37  
38      private String columnIndexToExcelColumn(int columnIndex) {
39          StringBuilder excelColumn = new StringBuilder();
40          while (columnIndex % 26 > 0) {
41              excelColumn.insert(0, (char) (columnIndex % 26 + 'A' - 1));
42              columnIndex /= 26;
43          }
44          return excelColumn.toString();
45      }
46  
47      public Column setHeader(Header header) {
48          this.header = header;
49          if (this.header != null) {
50              setColumnName(header.getName(this.columnIndex));
51          }
52          return this;
53      }
54  
55      public void setColumnIndex(int columnIndex) {
56          this.columnIndex = columnIndex;
57          setKey(new ColumnIndexKey(this.columnIndex));
58      }
59  
60      public void setColumnName(String columnName) {
61          this.columnName = columnName.toLowerCase();
62          setKey(new ColumnNameKey(this.columnName));
63      }
64  
65      public void setKey(ColumnKey key) {
66          this.key = key;
67      }
68  
69      public String getExcelColumn() {
70          return columnIndexToExcelColumn(this.columnIndex);
71      }
72  
73      public int getColumnIndex() {
74          return this.columnIndex;
75      }
76  
77      public String getColumnName() {
78          return this.columnName;
79      }
80  
81      public Column nextColumn() {
82          return new Column(this.getColumnIndex()+1).setHeader(header);
83      }
84  
85      public Column nextLine() {
86          return new Column().setHeader(header);
87      }
88  
89      public String getColumnText() {
90          return
91                  (columnIndex == -1 ? "" : " index "+columnIndex+" ("+getExcelColumn()+")") +
92                  (columnName == null ? "" : " name \""+columnName+"\"");
93      }
94  
95      @Override
96      public boolean equals(Object obj) {
97          if (!(obj instanceof Column)) {
98              return false;
99          }
100         return this.key.equals(((Column)obj).key);
101     }
102 
103     @Override
104     public int hashCode() {
105         return this.key.hashCode();
106     }
107 
108     @Override
109     public String toString() {
110         return this.key.toString();
111     }
112 
113     @Override
114     public int compareTo(Column column) {
115         return this.key.compareTo(column.key);
116     }
117 }