From d7649a2831b5507edf3c94c55c2a7a8878bccfad Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 1 Nov 2018 10:29:54 +0000 Subject: [PATCH] IDE warnings, tried to reproduce Bug 58927, but could not git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1845434 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/util/CellReference.java | 14 +- .../apache/poi/xssf/usermodel/XSSFSheet.java | 121 ++++++++---------- .../xssf/usermodel/helpers/ColumnHelper.java | 4 +- .../usermodel/helpers/TestColumnHelper.java | 49 ++++++- 4 files changed, 101 insertions(+), 87 deletions(-) diff --git a/src/java/org/apache/poi/ss/util/CellReference.java b/src/java/org/apache/poi/ss/util/CellReference.java index d14670221f..3ef32e30f6 100644 --- a/src/java/org/apache/poi/ss/util/CellReference.java +++ b/src/java/org/apache/poi/ss/util/CellReference.java @@ -132,7 +132,7 @@ public class CellReference { if (rowRef.length() == 0) { _rowIndex = -1; } else { - // throws NumberFormatException if rowRef is not convertable to an int + // throws NumberFormatException if rowRef is not convertible to an int _rowIndex = Integer.parseInt(rowRef)-1; // -1 to convert 1-based to zero-based } } @@ -336,10 +336,10 @@ public class CellReference { if(colStr.toUpperCase(Locale.ROOT).compareTo(lastCol) > 0) { return false; } - } else { + } /*else { // apparent column name has less chars than max // no need to check range - } + }*/ return true; } @@ -426,7 +426,7 @@ public class CellReference { // AreaReference.separateAreaRefs() // SheetNameFormatter.format() (inverse) - StringBuffer sb = new StringBuffer(indexOfSheetNameDelimiter); + StringBuilder sb = new StringBuilder(indexOfSheetNameDelimiter); for(int i=1; i */ +@SuppressWarnings("deprecation") public class XSSFSheet extends POIXMLDocumentPart implements Sheet { private static final POILogger logger = POILogFactory.getLogger(XSSFSheet.class); @@ -472,7 +473,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Verify that candidate region does not intersect with an existing merged region in this sheet * - * @param candidateRegion + * @param candidateRegion the range of cells to verify * @throws IllegalStateException if candidate region intersects an existing merged region in this sheet (or candidateRegion is already merged in this sheet) */ private void validateMergedRegions(CellRangeAddress candidateRegion) { @@ -855,8 +856,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * Get a Hyperlink in this sheet anchored at row, column * - * @param row - * @param column + * @param row The row where the hyperlink is anchored + * @param column The column where the hyperlinkn is anchored * @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null */ @Override @@ -1480,8 +1481,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * * @param startRowNum the first row number in this sheet to return * @param endRowNum the last row number in this sheet to return - * @param createRowIfMissing - * @return All rows between startRow and endRow, inclusive + * @param createRowIfMissing If missing rows should be created. + * @return All rows between startRow and endRow, inclusive. If createRowIfMissing is false, + * only previously existing rows are returned, otherwise empty rows are added as necessary * @throws IllegalArgumentException if startRowNum and endRowNum are not in ascending order */ private List getRows(int startRowNum, int endRowNum, boolean createRowIfMissing) { @@ -2472,7 +2474,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * 'Collapsed' state is stored in a single column col info record * immediately after the outline group * - * @param idx + * @param idx The column-index to check * @return a boolean represented if the column is collapsed */ private boolean isColumnGroupCollapsed(int idx) { @@ -3052,7 +3054,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { rebuildRows(); } - private final void rebuildRows() { + private void rebuildRows() { //rebuild the _rows map List rowList = new ArrayList<>(_rows.values()); _rows.clear(); @@ -3114,25 +3116,22 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // then do the actual moving and also adjust comments/rowHeight // we need to sort it in a way so the shifting does not mess up the structures, // i.e. when shifting down, start from down and go up, when shifting up, vice-versa - SortedMap commentsToShift = new TreeMap<>(new Comparator() { - @Override - public int compare(XSSFComment o1, XSSFComment o2) { - int row1 = o1.getRow(); - int row2 = o2.getRow(); - - if (row1 == row2) { - // ordering is not important when row is equal, but don't return zero to still - // get multiple comments per row into the map - return o1.hashCode() - o2.hashCode(); - } + SortedMap commentsToShift = new TreeMap<>((o1, o2) -> { + int row1 = o1.getRow(); + int row2 = o2.getRow(); + + if (row1 == row2) { + // ordering is not important when row is equal, but don't return zero to still + // get multiple comments per row into the map + return o1.hashCode() - o2.hashCode(); + } - // when shifting down, sort higher row-values first - if (n > 0) { - return row1 < row2 ? 1 : -1; - } else { - // sort lower-row values first when shifting up - return row1 > row2 ? 1 : -1; - } + // when shifting down, sort higher row-values first + if (n > 0) { + return row1 < row2 ? 1 : -1; + } else { + // sort lower-row values first when shifting up + return row1 > row2 ? 1 : -1; } }); @@ -3211,25 +3210,22 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { // then do the actual moving and also adjust comments/rowHeight // we need to sort it in a way so the shifting does not mess up the structures, // i.e. when shifting down, start from down and go up, when shifting up, vice-versa - SortedMap commentsToShift = new TreeMap<>(new Comparator() { - @Override - public int compare(XSSFComment o1, XSSFComment o2) { - int column1 = o1.getColumn(); - int column2 = o2.getColumn(); - - if (column1 == column2) { - // ordering is not important when row is equal, but don't return zero to still - // get multiple comments per row into the map - return o1.hashCode() - o2.hashCode(); - } + SortedMap commentsToShift = new TreeMap<>((o1, o2) -> { + int column1 = o1.getColumn(); + int column2 = o2.getColumn(); + + if (column1 == column2) { + // ordering is not important when row is equal, but don't return zero to still + // get multiple comments per row into the map + return o1.hashCode() - o2.hashCode(); + } - // when shifting down, sort higher row-values first - if (n > 0) { - return column1 < column2 ? 1 : -1; - } else { - // sort lower-row values first when shifting up - return column1 > column2 ? 1 : -1; - } + // when shifting down, sort higher row-values first + if (n > 0) { + return column1 < column2 ? 1 : -1; + } else { + // sort lower-row values first when shifting up + return column1 > column2 ? 1 : -1; } }); @@ -4096,7 +4092,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * Creates a new Table, and associates it with this Sheet. * * @param tableArea - * the area that the table should cover, should not be {@null} + * the area that the table should cover, should not be null * @return the created table * @since 4.0.0 */ @@ -4421,18 +4417,15 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { + "defined source sheet " + sourceSheet.getSheetName() + "."); } - return createPivotTable(position, sourceSheet, new PivotTableReferenceConfigurator() { - @Override - public void configureReference(CTWorksheetSource wsSource) { - final String[] firstCell = source.getFirstCell().getCellRefParts(); - final String firstRow = firstCell[1]; - final String firstCol = firstCell[2]; - final String[] lastCell = source.getLastCell().getCellRefParts(); - final String lastRow = lastCell[1]; - final String lastCol = lastCell[2]; - final String ref = firstCol+firstRow+':'+lastCol+lastRow; //or just source.formatAsString() - wsSource.setRef(ref); - } + return createPivotTable(position, sourceSheet, wsSource -> { + final String[] firstCell = source.getFirstCell().getCellRefParts(); + final String firstRow = firstCell[1]; + final String firstCol = firstCell[2]; + final String[] lastCell = source.getLastCell().getCellRefParts(); + final String lastRow = lastCell[1]; + final String lastCol = lastCell[2]; + final String ref = firstCol+firstRow+':'+lastCol+lastRow; //or just source.formatAsString() + wsSource.setRef(ref); }); } @@ -4494,12 +4487,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { + "defined source sheet " + sourceSheet.getSheetName() + "."); } - return createPivotTable(position, sourceSheet, new PivotTableReferenceConfigurator() { - @Override - public void configureReference(CTWorksheetSource wsSource) { - wsSource.setName(source.getNameName()); - } - }); + return createPivotTable(position, sourceSheet, wsSource -> wsSource.setName(source.getNameName())); } /** @@ -4523,12 +4511,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { */ @Beta public XSSFPivotTable createPivotTable(final Table source, CellReference position) { - return createPivotTable(position, getWorkbook().getSheet(source.getSheetName()), new PivotTableReferenceConfigurator() { - @Override - public void configureReference(CTWorksheetSource wsSource) { - wsSource.setName(source.getName()); - } - }); + return createPivotTable(position, getWorkbook().getSheet(source.getSheetName()), wsSource -> wsSource.setName(source.getName())); } /** @@ -4621,7 +4604,7 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { /** * when a cell with a 'master' shared formula is removed, the next cell in the range becomes the master - * @param cell + * @param cell The cell that is removed * @param evalWb BaseXSSFEvaluationWorkbook in use, if one exists */ protected void onDeleteFormula(XSSFCell cell, BaseXSSFEvaluationWorkbook evalWb){ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java index 0aa7720867..f65856ea69 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java @@ -50,7 +50,7 @@ public class ColumnHelper { TreeSet trackedCols = new TreeSet<>(CTColComparator.BY_MIN_MAX); CTCols newCols = CTCols.Factory.newInstance(); CTCols[] colsArray = worksheet.getColsArray(); - int i = 0; + int i; for (i = 0; i < colsArray.length; i++) { CTCols cols = colsArray[i]; for (CTCol col : cols.getColList()) { @@ -61,7 +61,7 @@ public class ColumnHelper { worksheet.removeCols(y); } - newCols.setColArray(trackedCols.toArray(new CTCol[trackedCols.size()])); + newCols.setColArray(trackedCols.toArray(new CTCol[0])); worksheet.addNewCols(); worksheet.setColsArray(0, newCols); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java index 5ba7ad1d03..9686dcf6a1 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/helpers/TestColumnHelper.java @@ -25,6 +25,9 @@ import static org.junit.Assert.assertTrue; import java.io.IOException; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFSheet; @@ -192,14 +195,14 @@ public final class TestColumnHelper { } @Test - public void testAddCleanColIntoColsExactOverlap() throws Exception { + public void testAddCleanColIntoColsExactOverlap() { CTCols cols = createHiddenAndBestFitColsWithHelper(1, 1, 1, 1); assertEquals(1, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 1, true, true); } @Test - public void testAddCleanColIntoColsOverlapsOverhangingBothSides() throws Exception { + public void testAddCleanColIntoColsOverlapsOverhangingBothSides() { CTCols cols = createHiddenAndBestFitColsWithHelper(2, 2, 1, 3); assertEquals(3, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 1, false, true); @@ -208,7 +211,7 @@ public final class TestColumnHelper { } @Test - public void testAddCleanColIntoColsOverlapsCompletelyNested() throws Exception { + public void testAddCleanColIntoColsOverlapsCompletelyNested() { CTCols cols = createHiddenAndBestFitColsWithHelper(1, 3, 2, 2); assertEquals(3, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 1, true, false); @@ -217,7 +220,7 @@ public final class TestColumnHelper { } @Test - public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRightExactRight() throws Exception { + public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRightExactRight() { CTCols cols = createHiddenAndBestFitColsWithHelper(2, 3, 1, 3); assertEquals(2, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 1, false, true); @@ -225,7 +228,7 @@ public final class TestColumnHelper { } @Test - public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeftExactLeft() throws Exception { + public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeftExactLeft() { CTCols cols = createHiddenAndBestFitColsWithHelper(1, 2, 1, 3); assertEquals(2, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 2, true, true); @@ -233,7 +236,7 @@ public final class TestColumnHelper { } @Test - public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRight() throws Exception { + public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRight() { CTCols cols = createHiddenAndBestFitColsWithHelper(2, 3, 1, 2); assertEquals(3, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 1, false, true); @@ -242,7 +245,7 @@ public final class TestColumnHelper { } @Test - public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeft() throws Exception { + public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeft() { CTCols cols = createHiddenAndBestFitColsWithHelper(1, 2, 2, 3); assertEquals(3, cols.sizeOfColArray()); assertMinMaxHiddenBestFit(cols, 0, 1, 1, true, false); @@ -402,4 +405,36 @@ public final class TestColumnHelper { } return count; } + + @SuppressWarnings("deprecation") + @Test + public void testColumnsCollapsed() { + Workbook wb = new XSSFWorkbook(); + Sheet sheet = wb.createSheet("test"); + Row row = sheet.createRow(0); + row.createCell(0); + row.createCell(1); + row.createCell(2); + + sheet.setColumnWidth(0, 10); + sheet.setColumnWidth(1, 10); + sheet.setColumnWidth(2, 10); + + sheet.groupColumn(0, 1); + sheet.setColumnGroupCollapsed(0, true); + + CTCols ctCols = ((XSSFSheet) sheet).getCTWorksheet().getColsArray()[0]; + assertEquals(3, ctCols.sizeOfColArray()); + assertTrue(ctCols.getColArray(0).isSetCollapsed()); + assertTrue(ctCols.getColArray(1).isSetCollapsed()); + assertTrue(ctCols.getColArray(2).isSetCollapsed()); + + ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory.newInstance()); + helper.setColumnAttributes(ctCols.getColArray(1), ctCols.getColArray(2)); + + ctCols = ((XSSFSheet) sheet).getCTWorksheet().getColsArray()[0]; + assertTrue(ctCols.getColArray(0).isSetCollapsed()); + assertTrue(ctCols.getColArray(1).isSetCollapsed()); + assertTrue(ctCols.getColArray(2).isSetCollapsed()); + } } -- 2.39.5