@SuppressWarnings("deprecation") public void cleanColumns() { this.newCols = CTCols.Factory.newInstance(); CTCols aggregateCols = CTCols.Factory.newInstance(); CTCols[] colsArray = worksheet.getColsArray(); assert (colsArray != null); for (CTCols cols : colsArray) { for (CTCol col : cols.getColArray()) { cloneCol(aggregateCols, col); } } sortColumns(aggregateCols); CTCol[] colArray = aggregateCols.getColArray(); sweepCleanColumns(newCols, colArray, null); int i = colsArray.length; for (int y = i - 1; y >= 0; y--) { worksheet.removeCols(y); } worksheet.addNewCols(); worksheet.setColsArray(0, newCols); }
@SuppressWarnings("deprecation") public CTCols addCleanColIntoCols(CTCols cols, CTCol col) { CTCols newCols = CTCols.Factory.newInstance(); for (CTCol c : cols.getColArray()) { cloneCol(newCols, c); } cloneCol(newCols, col); sortColumns(newCols); CTCol[] colArray = newCols.getColArray(); CTCols returnCols = CTCols.Factory.newInstance(); sweepCleanColumns(returnCols, colArray, col); colArray = returnCols.getColArray(); cols.setColArray(colArray); return returnCols; }
public void testSortColumns() { CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); ColumnHelper helper = new ColumnHelper(worksheet); CTCols cols1 = CTCols.Factory.newInstance(); CTCol col1 = cols1.addNewCol(); col1.setMin(1); col1.setMax(1); col1.setWidth(88); col1.setHidden(true); CTCol col2 = cols1.addNewCol(); col2.setMin(2); col2.setMax(3); CTCol col3 = cols1.addNewCol(); col3.setMin(13); col3.setMax(16750); assertEquals(3, cols1.sizeOfColArray()); CTCol col4 = cols1.addNewCol(); col4.setMin(8); col4.setMax(11); assertEquals(4, cols1.sizeOfColArray()); CTCol col5 = cols1.addNewCol(); col5.setMin(4); col5.setMax(5); assertEquals(5, cols1.sizeOfColArray()); CTCol col6 = cols1.addNewCol(); col6.setMin(8); col6.setMax(9); col6.setHidden(true); CTCol col7 = cols1.addNewCol(); col7.setMin(6); col7.setMax(8); col7.setWidth(17.0); CTCol col8 = cols1.addNewCol(); col8.setMin(25); col8.setMax(27); CTCol col9 = cols1.addNewCol(); col9.setMin(20); col9.setMax(30); assertEquals(9, cols1.sizeOfColArray()); assertEquals(20, cols1.getColArray(8).getMin()); assertEquals(30, cols1.getColArray(8).getMax()); ColumnHelper.sortColumns(cols1); assertEquals(9, cols1.sizeOfColArray()); assertEquals(25, cols1.getColArray(8).getMin()); assertEquals(27, cols1.getColArray(8).getMax()); }
/** @see <a href="http://en.wikipedia.org/wiki/Sweep_line_algorithm">Sweep line algorithm</a> */ private void sweepCleanColumns(CTCols cols, CTCol[] flattenedColsArray, CTCol overrideColumn) { List<CTCol> flattenedCols = new ArrayList<CTCol>(Arrays.asList(flattenedColsArray)); TreeSet<CTCol> currentElements = new TreeSet<CTCol>(CTColComparator.BY_MAX); ListIterator<CTCol> flIter = flattenedCols.listIterator(); CTCol haveOverrideColumn = null; long lastMaxIndex = 0; long currentMax = 0; while (flIter.hasNext()) { CTCol col = flIter.next(); long currentIndex = col.getMin(); long colMax = col.getMax(); long nextIndex = (colMax > currentMax) ? colMax : currentMax; if (flIter.hasNext()) { nextIndex = flIter.next().getMin(); flIter.previous(); } Iterator<CTCol> iter = currentElements.iterator(); while (iter.hasNext()) { CTCol elem = iter.next(); if (currentIndex <= elem.getMax()) break; // all passed elements have been purged iter.remove(); } if (!currentElements.isEmpty() && lastMaxIndex < currentIndex) { // we need to process previous elements first insertCol( cols, lastMaxIndex, currentIndex - 1, currentElements.toArray(new CTCol[currentElements.size()]), true, haveOverrideColumn); } currentElements.add(col); if (colMax > currentMax) currentMax = colMax; if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; while (currentIndex <= nextIndex && !currentElements.isEmpty()) { Set<CTCol> currentIndexElements = new HashSet<CTCol>(); long currentElemIndex; { // narrow scope of currentElem CTCol currentElem = currentElements.first(); currentElemIndex = currentElem.getMax(); currentIndexElements.add(currentElem); while (true) { CTCol higherElem = currentElements.higher(currentElem); if (higherElem == null || higherElem.getMax() != currentElemIndex) break; currentElem = higherElem; currentIndexElements.add(currentElem); if (colMax > currentMax) currentMax = colMax; if (col.equals(overrideColumn)) haveOverrideColumn = overrideColumn; } } if (currentElemIndex < nextIndex || !flIter.hasNext()) { insertCol( cols, currentIndex, currentElemIndex, currentElements.toArray(new CTCol[currentElements.size()]), true, haveOverrideColumn); if (flIter.hasNext()) { if (nextIndex > currentElemIndex) { currentElements.removeAll(currentIndexElements); if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null; } } else { currentElements.removeAll(currentIndexElements); if (currentIndexElements.contains(overrideColumn)) haveOverrideColumn = null; } lastMaxIndex = currentIndex = currentElemIndex + 1; } else { lastMaxIndex = currentIndex; currentIndex = nextIndex + 1; } } } sortColumns(cols); }