From af5351527946edb6b543f0e30f87d440b9f6b33a Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Sat, 2 Jul 2016 18:28:43 +0000 Subject: [PATCH] bug 59773: move loop invariants outside of loop or change for loops to for-each loops git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1751086 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/usermodel/HSSFName.java | 3 ++- .../apache/poi/hssf/usermodel/HSSFSheet.java | 7 ++++-- .../poi/openxml4j/opc/PackagePartName.java | 5 ++-- .../poi/openxml4j/opc/PackagingURIHelper.java | 5 ++-- .../apache/poi/xdgf/geom/SplineRenderer.java | 8 ++++--- .../xssf/extractor/XSSFExcelExtractor.java | 7 +++--- .../poi/xssf/streaming/SXSSFWorkbook.java | 3 ++- .../usermodel/XSSFEvaluationWorkbook.java | 7 +++--- .../xssf/usermodel/XSSFRichTextString.java | 13 +++++------ .../apache/poi/xssf/usermodel/XSSFSheet.java | 3 ++- .../poi/xssf/usermodel/XSSFWorkbook.java | 23 +++++++++++-------- .../usermodel/helpers/XSSFFormulaUtils.java | 3 ++- .../usermodel/helpers/XSSFRowShifter.java | 3 ++- 13 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java index 0778361ffb..f4250d52e1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java @@ -135,7 +135,8 @@ public final class HSSFName implements Name { int sheetNumber = _definedNameRec.getSheetNumber(); //Check to ensure no other names have the same case-insensitive name - for ( int i = wb.getNumNames()-1; i >=0; i-- ) + final int lastNameIndex = wb.getNumNames()-1; + for ( int i = lastNameIndex; i >=0; i-- ) { NameRecord rec = wb.getNameRecord(i); if (rec != _definedNameRec) { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index d1de9ba7ac..9fe9c49c6b 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -1640,7 +1640,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { if (moveComments) { // This code would get simpler if NoteRecords could be organised by HSSFRow. HSSFPatriarch patriarch = createDrawingPatriarch(); - for (int i = patriarch.getChildren().size() - 1; i >= 0; i--) { + final int lastChildIndex = patriarch.getChildren().size() - 1; + for (int i = lastChildIndex; i >= 0; i--) { HSSFShape shape = patriarch.getChildren().get(i); if (!(shape instanceof HSSFComment)) { continue; @@ -2372,7 +2373,9 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet { //create a combobox control for each column HSSFPatriarch p = createDrawingPatriarch(); - for (int col = range.getFirstColumn(); col <= range.getLastColumn(); col++) { + final int firstColumn = range.getFirstColumn(); + final int lastColumn = range.getLastColumn(); + for (int col = firstColumn; col <= lastColumn; col++) { p.createComboBox(new HSSFClientAnchor(0, 0, 0, 0, (short) col, firstRow, (short) (col + 1), firstRow + 1)); } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePartName.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePartName.java index 78a18e269d..37ee631776 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePartName.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagePartName.java @@ -289,7 +289,8 @@ public final class PackagePartName implements Comparable { private static void checkPCharCompliance(String segment) throws InvalidFormatException { boolean errorFlag; - for (int i = 0; i < segment.length(); ++i) { + final int length = segment.length(); + for (int i = 0; i < length; ++i) { char c = segment.charAt(i); errorFlag = true; @@ -328,7 +329,7 @@ public final class PackagePartName implements Comparable { if (errorFlag && c == '%') { // We certainly found an encoded character, check for length // now ( '%' HEXDIGIT HEXDIGIT) - if (((segment.length() - i) < 2)) { + if (((length - i) < 2)) { throw new InvalidFormatException("The segment " + segment + " contain invalid encoded character !"); } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java index 5d83e9f223..f44bf73673 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackagingURIHelper.java @@ -606,12 +606,13 @@ public final class PackagingURIHelper { StringBuffer retVal = new StringBuffer(); String uriStr = uri.toASCIIString(); char c; - for (int i = 0; i < uriStr.length(); ++i) { + final int length = uriStr.length(); + for (int i = 0; i < length; ++i) { c = uriStr.charAt(i); if (c == '%') { // We certainly found an encoded character, check for length // now ( '%' HEXDIGIT HEXDIGIT) - if (((uriStr.length() - i) < 2)) { + if (((length - i) < 2)) { throw new IllegalArgumentException("The uri " + uriStr + " contain invalid encoded character !"); } diff --git a/src/ooxml/java/org/apache/poi/xdgf/geom/SplineRenderer.java b/src/ooxml/java/org/apache/poi/xdgf/geom/SplineRenderer.java index a478ae4a9a..c16835604c 100644 --- a/src/ooxml/java/org/apache/poi/xdgf/geom/SplineRenderer.java +++ b/src/ooxml/java/org/apache/poi/xdgf/geom/SplineRenderer.java @@ -30,15 +30,17 @@ public class SplineRenderer { ValueVector knots, ValueVector weights, int degree) { double firstKnot = knots.get(0); - double lastKnot = knots.get(knots.size() - 1); + final int count = knots.size(); + double lastKnot = knots.get(count - 1); // scale knots to [0, 1] based on first/last knots - for (int i = 0; i < knots.size(); i++) { + for (int i = 0; i < count; i++) { knots.set((knots.get(i) - firstKnot) / lastKnot, i); } // if we don't have enough knots, duplicate the last knot until we do - for (int i = knots.size(); i < controlPoints.numPoints() + degree + 1; i++) { + final int knotsToAdd = controlPoints.numPoints() + degree + 1; + for (int i = count; i < knotsToAdd; i++) { knots.add(1); } diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java index 0bcb4b72e7..923c5aed84 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java @@ -30,6 +30,7 @@ import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.DataFormatter; import org.apache.poi.ss.usermodel.HeaderFooter; import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFRelation; @@ -134,10 +135,10 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor } StringBuffer text = new StringBuffer(); - for(int i=0; i= pos && index < pos + r.getT().length()) { + for(CTRElt r : st.getRArray()){ + final int length = r.getT().length(); + if(index >= pos && index < pos + length) { XSSFFont fnt = new XSSFFont(toCTFont(r.getRPr())); - fnt.setThemesTable(getThemesTable()); + fnt.setThemesTable(themes); return fnt; } - pos += r.getT().length(); + pos += length; } return null; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index 53beb9c505..50a898a5c5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -2674,7 +2674,8 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { public int findEndOfRowOutlineGroup(int row) { short level = getRow(row).getCTRow().getOutlineLevel(); int currentRow; - for (currentRow = row; currentRow < getLastRowNum(); currentRow++) { + final int lastRowNum = getLastRowNum(); + for (currentRow = row; currentRow < lastRowNum; currentRow++) { if (getRow(currentRow) == null || getRow(currentRow).getCTRow().getOutlineLevel() < level) { break; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 9133046239..1e9b3d6cee 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -1084,11 +1084,12 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { */ @Override public int getSheetIndex(String name) { - for (int i = 0 ; i < sheets.size() ; ++i) { - XSSFSheet sheet = sheets.get(i); - if (name.equalsIgnoreCase(sheet.getSheetName())) { - return i; + int idx = 0; + for (XSSFSheet sh : sheets) { + if (name.equalsIgnoreCase(sh.getSheetName())) { + return idx; } + idx++; } return -1; } @@ -1264,12 +1265,13 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { @Override public void removeName(String name) { - for (int i = 0; i < namedRanges.size(); i++) { - XSSFName nm = namedRanges.get(i); + int idx = 0; + for (XSSFName nm : namedRanges) { if(nm.getNameName().equalsIgnoreCase(name)) { - removeName(i); + removeName(idx); return; } + idx++; } throw new IllegalArgumentException("Named range was not found: " + name); } @@ -1551,9 +1553,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { */ @Override public void setSelectedTab(int index) { - for (int i = 0 ; i < sheets.size() ; ++i) { - XSSFSheet sheet = sheets.get(i); - sheet.setSelected(i == index); + int idx = 0; + for (XSSFSheet sh : sheets) { + sh.setSelected(idx == index); + idx++; } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java index 8aa3de6014..99e8a1e408 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java @@ -64,7 +64,8 @@ public final class XSSFFormulaUtils { */ public void updateSheetName(final int sheetIndex, final String oldName, final String newName) { // update named ranges - for (int i = 0; i < _wb.getNumberOfNames(); i++) { + final int numberOfNames = _wb.getNumberOfNames(); + for (int i = 0; i < numberOfNames; i++) { XSSFName nm = _wb.getNameAt(i); if (nm.getSheetIndex() == -1 || nm.getSheetIndex() == sheetIndex) { updateName(nm, oldName, newName); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java index 6f6d06d3c5..765b4c0a4b 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java @@ -85,7 +85,8 @@ public final class XSSFRowShifter extends RowShifter { public void updateNamedRanges(FormulaShifter shifter) { Workbook wb = sheet.getWorkbook(); XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb); - for (int i = 0; i < wb.getNumberOfNames(); i++) { + final int numberOfNames = wb.getNumberOfNames(); + for (int i = 0; i < numberOfNames; i++) { Name name = wb.getNameAt(i); String formula = name.getRefersToFormula(); int sheetIndex = name.getSheetIndex(); -- 2.39.5