Lines 19-25
Link Here
|
19 |
|
19 |
|
20 |
import java.util.ArrayList; |
20 |
import java.util.ArrayList; |
21 |
import java.util.Arrays; |
21 |
import java.util.Arrays; |
22 |
import java.util.Comparator; |
|
|
23 |
import java.util.HashSet; |
22 |
import java.util.HashSet; |
24 |
import java.util.Iterator; |
23 |
import java.util.Iterator; |
25 |
import java.util.List; |
24 |
import java.util.List; |
Lines 54-101
Link Here
|
54 |
this.newCols = CTCols.Factory.newInstance(); |
53 |
this.newCols = CTCols.Factory.newInstance(); |
55 |
|
54 |
|
56 |
CTCols aggregateCols = CTCols.Factory.newInstance(); |
55 |
CTCols aggregateCols = CTCols.Factory.newInstance(); |
57 |
List<CTCols> colsList = worksheet.getColsList(); |
56 |
CTCols[] colsArray = worksheet.getColsArray(); |
58 |
assert(colsList != null); |
57 |
assert(colsArray != null); |
59 |
|
58 |
|
60 |
for (CTCols cols : colsList) { |
59 |
for (CTCols cols : colsArray) { |
61 |
for (CTCol col : cols.getColList()) { |
60 |
for (CTCol col : cols.getColArray()) { |
62 |
cloneCol(aggregateCols, col); |
61 |
cloneCol(aggregateCols, col); |
63 |
} |
62 |
} |
64 |
} |
63 |
} |
65 |
|
64 |
|
66 |
sortColumns(aggregateCols); |
65 |
sortColumns(aggregateCols); |
67 |
|
66 |
|
68 |
CTCol[] colArray = new CTCol[aggregateCols.getColList().size()]; |
67 |
CTCol[] colArray = aggregateCols.getColArray(); |
69 |
aggregateCols.getColList().toArray(colArray); |
|
|
70 |
sweepCleanColumns(newCols, colArray, null); |
68 |
sweepCleanColumns(newCols, colArray, null); |
71 |
|
69 |
|
72 |
int i = colsList.size(); |
70 |
int i = colsArray.length; |
73 |
for (int y = i - 1; y >= 0; y--) { |
71 |
for (int y = i - 1; y >= 0; y--) { |
74 |
worksheet.removeCols(y); |
72 |
worksheet.removeCols(y); |
75 |
} |
73 |
} |
76 |
worksheet.addNewCols(); |
74 |
worksheet.addNewCols(); |
77 |
worksheet.setColsArray(0, newCols); |
75 |
worksheet.setColsArray(0, newCols); |
78 |
} |
76 |
} |
79 |
|
77 |
|
80 |
private static class CTColByMaxComparator implements Comparator<CTCol> { |
|
|
81 |
|
82 |
public int compare(CTCol arg0, CTCol arg1) { |
83 |
if (arg0.getMax() < arg1.getMax()) { |
84 |
return -1; |
85 |
} else { |
86 |
if (arg0.getMax() > arg1.getMax()) return 1; |
87 |
else return 0; |
88 |
} |
89 |
} |
90 |
|
91 |
} |
92 |
|
93 |
/** |
78 |
/** |
94 |
* @see <a href="http://en.wikipedia.org/wiki/Sweep_line_algorithm">Sweep line algorithm</a> |
79 |
* @see <a href="http://en.wikipedia.org/wiki/Sweep_line_algorithm">Sweep line algorithm</a> |
95 |
*/ |
80 |
*/ |
96 |
private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) { |
81 |
private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) { |
97 |
List<CTCol> flattenedCols = new ArrayList<CTCol>(Arrays.asList(flattenedColsArray)); |
82 |
List<CTCol> flattenedCols = new ArrayList<CTCol>(Arrays.asList(flattenedColsArray)); |
98 |
TreeSet<CTCol> currentElements = new TreeSet<CTCol>(new CTColByMaxComparator()); |
83 |
TreeSet<CTCol> currentElements = new TreeSet<CTCol>(CTColComparator.BY_MAX); |
99 |
ListIterator<CTCol> flIter = flattenedCols.listIterator(); |
84 |
ListIterator<CTCol> flIter = flattenedCols.listIterator(); |
100 |
CTCol haveOverrideColumn = null; |
85 |
CTCol haveOverrideColumn = null; |
101 |
long lastMaxIndex = 0; |
86 |
long lastMaxIndex = 0; |
Lines 103-109
Link Here
|
103 |
while (flIter.hasNext()) { |
88 |
while (flIter.hasNext()) { |
104 |
CTCol col = flIter.next(); |
89 |
CTCol col = flIter.next(); |
105 |
long currentIndex = col.getMin(); |
90 |
long currentIndex = col.getMin(); |
106 |
long nextIndex = (col.getMax() > currentMax) ? col.getMax() : currentMax; |
91 |
long colMax = col.getMax(); |
|
|
92 |
long nextIndex = (colMax > currentMax) ? colMax : currentMax; |
107 |
if (flIter.hasNext()) { |
93 |
if (flIter.hasNext()) { |
108 |
nextIndex = flIter.next().getMin(); |
94 |
nextIndex = flIter.next().getMin(); |
109 |
flIter.previous(); |
95 |
flIter.previous(); |
Lines 116-125
Link Here
|
116 |
} |
102 |
} |
117 |
if (!currentElements.isEmpty() && lastMaxIndex < currentIndex) { |
103 |
if (!currentElements.isEmpty() && lastMaxIndex < currentIndex) { |
118 |
// we need to process previous elements first |
104 |
// we need to process previous elements first |
119 |
insertCol(cols, lastMaxIndex, currentIndex - 1, currentElements.toArray(new CTCol[]{}), true, haveOverrideColumn); |
105 |
insertCol(cols, lastMaxIndex, currentIndex - 1, currentElements.toArray(new CTCol[currentElements.size()]), true, haveOverrideColumn); |
120 |
} |
106 |
} |
121 |
currentElements.add(col); |
107 |
currentElements.add(col); |
122 |
if (col.getMax() > currentMax) currentMax = col.getMax(); |
108 |
if (colMax > currentMax) currentMax = colMax; |
123 |
if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; |
109 |
if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; |
124 |
while (currentIndex <= nextIndex && !currentElements.isEmpty()) { |
110 |
while (currentIndex <= nextIndex && !currentElements.isEmpty()) { |
125 |
Set<CTCol> currentIndexElements = new HashSet<CTCol>(); |
111 |
Set<CTCol> currentIndexElements = new HashSet<CTCol>(); |
Lines 130-155
Link Here
|
130 |
CTCol currentElem = currentElements.first(); |
116 |
CTCol currentElem = currentElements.first(); |
131 |
currentElemIndex = currentElem.getMax(); |
117 |
currentElemIndex = currentElem.getMax(); |
132 |
currentIndexElements.add(currentElem); |
118 |
currentIndexElements.add(currentElem); |
133 |
|
119 |
|
134 |
for (CTCol cc : currentElements.tailSet(currentElem)) { |
120 |
while (true) { |
135 |
if (cc == null || cc.getMax() == currentElemIndex) break; |
121 |
CTCol higherElem = currentElements.higher(currentElem); |
136 |
currentIndexElements.add(cc); |
122 |
if (higherElem == null || higherElem.getMax() != currentElemIndex) |
137 |
if (col.getMax() > currentMax) currentMax = col.getMax(); |
123 |
break; |
|
|
124 |
currentElem = higherElem; |
125 |
currentIndexElements.add(currentElem); |
126 |
if (colMax > currentMax) currentMax = colMax; |
138 |
if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; |
127 |
if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; |
139 |
} |
128 |
} |
140 |
|
|
|
141 |
// JDK 6 code |
142 |
// while (currentElements.higher(currentElem) != null && currentElements.higher(currentElem).getMax() == currentElemIndex) { |
143 |
// currentElem = currentElements.higher(currentElem); |
144 |
// currentIndexElements.add(currentElem); |
145 |
// if (col.getMax() > currentMax) currentMax = col.getMax(); |
146 |
// if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; |
147 |
// } |
148 |
} |
129 |
} |
149 |
|
130 |
|
150 |
|
131 |
|
151 |
if (currentElemIndex < nextIndex || !flIter.hasNext()) { |
132 |
if (currentElemIndex < nextIndex || !flIter.hasNext()) { |
152 |
insertCol(cols, currentIndex, currentElemIndex, currentElements.toArray(new CTCol[]{}), true, haveOverrideColumn); |
133 |
insertCol(cols, currentIndex, currentElemIndex, currentElements.toArray(new CTCol[currentElements.size()]), true, haveOverrideColumn); |
153 |
if (flIter.hasNext()) { |
134 |
if (flIter.hasNext()) { |
154 |
if (nextIndex > currentElemIndex) { |
135 |
if (nextIndex > currentElemIndex) { |
155 |
currentElements.removeAll(currentIndexElements); |
136 |
currentElements.removeAll(currentIndexElements); |
Lines 171-179
Link Here
|
171 |
} |
152 |
} |
172 |
|
153 |
|
173 |
public static void sortColumns(CTCols newCols) { |
154 |
public static void sortColumns(CTCols newCols) { |
174 |
CTCol[] colArray = new CTCol[newCols.getColList().size()]; |
155 |
CTCol[] colArray = newCols.getColArray(); |
175 |
newCols.getColList().toArray(colArray); |
156 |
Arrays.sort(colArray, CTColComparator.BY_MIN_MAX); |
176 |
Arrays.sort(colArray, new CTColComparator()); |
|
|
177 |
newCols.setColArray(colArray); |
157 |
newCols.setColArray(colArray); |
178 |
} |
158 |
} |
179 |
|
159 |
|
Lines 198-243
Link Here
|
198 |
* as 1 based. |
178 |
* as 1 based. |
199 |
*/ |
179 |
*/ |
200 |
public CTCol getColumn1Based(long index1, boolean splitColumns) { |
180 |
public CTCol getColumn1Based(long index1, boolean splitColumns) { |
201 |
CTCols colsArray = worksheet.getColsArray(0); |
181 |
CTCols cols = worksheet.getColsArray(0); |
202 |
|
182 |
|
203 |
// Fetching the array is quicker than working on the new style |
183 |
// Fetching the array is quicker than working on the new style |
204 |
// list, assuming we need to read many of them (which we often do), |
184 |
// list, assuming we need to read many of them (which we often do), |
205 |
// and assuming we're not making many changes (which we're not) |
185 |
// and assuming we're not making many changes (which we're not) |
206 |
@SuppressWarnings("deprecation") |
186 |
@SuppressWarnings("deprecation") |
207 |
CTCol[] cols = colsArray.getColArray(); |
187 |
CTCol[] colArray = cols.getColArray(); |
208 |
|
188 |
|
209 |
for (int i = 0; i < cols.length; i++) { |
189 |
for (CTCol col : colArray) { |
210 |
CTCol colArray = cols[i]; |
190 |
long colMin = col.getMin(); |
211 |
if (colArray.getMin() <= index1 && colArray.getMax() >= index1) { |
191 |
long colMax = col.getMax(); |
|
|
192 |
if (colMin <= index1 && colMax >= index1) { |
212 |
if (splitColumns) { |
193 |
if (splitColumns) { |
213 |
if (colArray.getMin() < index1) { |
194 |
if (colMin < index1) { |
214 |
insertCol(colsArray, colArray.getMin(), (index1 - 1), new CTCol[]{colArray}); |
195 |
insertCol(cols, colMin, (index1 - 1), new CTCol[]{col}); |
215 |
} |
196 |
} |
216 |
if (colArray.getMax() > index1) { |
197 |
if (colMax > index1) { |
217 |
insertCol(colsArray, (index1 + 1), colArray.getMax(), new CTCol[]{colArray}); |
198 |
insertCol(cols, (index1 + 1), colMax, new CTCol[]{col}); |
218 |
} |
199 |
} |
219 |
colArray.setMin(index1); |
200 |
col.setMin(index1); |
220 |
colArray.setMax(index1); |
201 |
col.setMax(index1); |
221 |
} |
202 |
} |
222 |
return colArray; |
203 |
return col; |
223 |
} |
204 |
} |
224 |
} |
205 |
} |
225 |
return null; |
206 |
return null; |
226 |
} |
207 |
} |
227 |
|
208 |
|
228 |
public CTCols addCleanColIntoCols(CTCols cols, CTCol col) { |
209 |
public CTCols addCleanColIntoCols(CTCols cols, CTCol col) { |
229 |
CTCols newCols = CTCols.Factory.newInstance(); |
210 |
CTCols newCols = CTCols.Factory.newInstance(); |
230 |
for (CTCol c : cols.getColList()) { |
211 |
for (CTCol c : cols.getColArray()) { |
231 |
cloneCol(newCols, c); |
212 |
cloneCol(newCols, c); |
232 |
} |
213 |
} |
233 |
cloneCol(newCols, col); |
214 |
cloneCol(newCols, col); |
234 |
sortColumns(newCols); |
215 |
sortColumns(newCols); |
235 |
CTCol[] colArray = new CTCol[newCols.getColList().size()]; |
216 |
CTCol[] colArray = newCols.getColArray(); |
236 |
newCols.getColList().toArray(colArray); |
|
|
237 |
CTCols returnCols = CTCols.Factory.newInstance(); |
217 |
CTCols returnCols = CTCols.Factory.newInstance(); |
238 |
sweepCleanColumns(returnCols, colArray, col); |
218 |
sweepCleanColumns(returnCols, colArray, col); |
239 |
colArray = new CTCol[returnCols.getColList().size()]; |
219 |
colArray = returnCols.getColArray(); |
240 |
returnCols.getColList().toArray(colArray); |
|
|
241 |
cols.setColArray(colArray); |
220 |
cols.setColArray(colArray); |
242 |
return returnCols; |
221 |
return returnCols; |
243 |
} |
222 |
} |
Lines 273-280
Link Here
|
273 |
return columnExists1Based(cols, index+1); |
252 |
return columnExists1Based(cols, index+1); |
274 |
} |
253 |
} |
275 |
private boolean columnExists1Based(CTCols cols, long index1) { |
254 |
private boolean columnExists1Based(CTCols cols, long index1) { |
276 |
for (int i = 0; i < cols.sizeOfColArray(); i++) { |
255 |
for (CTCol col : cols.getColArray()) { |
277 |
if (cols.getColArray(i).getMin() == index1) { |
256 |
if (col.getMin() == index1) { |
278 |
return true; |
257 |
return true; |
279 |
} |
258 |
} |
280 |
} |
259 |
} |
Lines 344-362
Link Here
|
344 |
} |
323 |
} |
345 |
|
324 |
|
346 |
private boolean columnExists(CTCols cols, long min, long max) { |
325 |
private boolean columnExists(CTCols cols, long min, long max) { |
347 |
for (int i = 0; i < cols.sizeOfColArray(); i++) { |
326 |
for (CTCol col : cols.getColArray()) { |
348 |
if (cols.getColArray(i).getMin() == min && cols.getColArray(i).getMax() == max) { |
327 |
if (col.getMin() == min && col.getMax() == max) { |
349 |
return true; |
328 |
return true; |
350 |
} |
329 |
} |
351 |
} |
330 |
} |
352 |
return false; |
331 |
return false; |
353 |
} |
332 |
} |
354 |
|
333 |
|
355 |
public int getIndexOfColumn(CTCols cols, CTCol col) { |
334 |
public int getIndexOfColumn(CTCols cols, CTCol searchCol) { |
356 |
for (int i = 0; i < cols.sizeOfColArray(); i++) { |
335 |
int i = 0; |
357 |
if (cols.getColArray(i).getMin() == col.getMin() && cols.getColArray(i).getMax() == col.getMax()) { |
336 |
for (CTCol col : cols.getColArray()) { |
|
|
337 |
if (col.getMin() == searchCol.getMin() && col.getMax() == searchCol.getMax()) { |
358 |
return i; |
338 |
return i; |
359 |
} |
339 |
} |
|
|
340 |
i++; |
360 |
} |
341 |
} |
361 |
return -1; |
342 |
return -1; |
362 |
} |
343 |
} |