diff options
author | Javen O'Neal <onealj@apache.org> | 2016-07-02 18:28:43 +0000 |
---|---|---|
committer | Javen O'Neal <onealj@apache.org> | 2016-07-02 18:28:43 +0000 |
commit | af5351527946edb6b543f0e30f87d440b9f6b33a (patch) | |
tree | e22751ef10a540e6433ab52d4971d4d0fd2856e2 /src/ooxml | |
parent | 80e3a64280b0e099ba0b0d2d94520cf9ad025362 (diff) | |
download | poi-af5351527946edb6b543f0e30f87d440b9f6b33a.tar.gz poi-af5351527946edb6b543f0e30f87d440b9f6b33a.zip |
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
Diffstat (limited to 'src/ooxml')
11 files changed, 46 insertions, 34 deletions
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<PackagePartName> { 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<PackagePartName> { 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<workbook.getNumberOfSheets(); i++) { - XSSFSheet sheet = workbook.getSheetAt(i); + for(Sheet sh : workbook) { + XSSFSheet sheet = (XSSFSheet) sh; if(includeSheetNames) { - text.append(workbook.getSheetName(i)).append("\n"); + text.append(sheet.getSheetName()).append("\n"); } // Header(s), if present diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 69c99833cd..8188c9e725 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -239,7 +239,8 @@ public class SXSSFWorkbook implements Workbook { { _wb=workbook; _sharedStringSource = useSharedStringsTable ? _wb.getSharedStringSource() : null; - for ( int i = 0; i < _wb.getNumberOfSheets(); i++ ) + final int numberOfSheets = _wb.getNumberOfSheets(); + for ( int i = 0; i < numberOfSheets; i++ ) { XSSFSheet sheet = _wb.getSheetAt( i ); createAndRegisterSXSSFSheet( sheet ); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java index d97119cdb7..1e849b673c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationWorkbook.java @@ -50,11 +50,12 @@ public final class XSSFEvaluationWorkbook extends BaseXSSFEvaluationWorkbook { public EvaluationSheet getSheet(int sheetIndex) { // Performance optimization: build sheet cache the first time this is called // to avoid re-creating the XSSFEvaluationSheet each time a new cell is evaluated - // EvaluationWorkbooks make not guarentee to syncronize changes made to + // EvaluationWorkbooks make not guarantee to synchronize changes made to // the underlying workbook after the EvaluationWorkbook is created. if (_sheetCache == null) { - _sheetCache = new XSSFEvaluationSheet[_uBook.getNumberOfSheets()]; - for (int i=0; i < _uBook.getNumberOfSheets(); i++) { + final int numberOfSheets = _uBook.getNumberOfSheets(); + _sheetCache = new XSSFEvaluationSheet[numberOfSheets]; + for (int i=0; i < numberOfSheets; i++) { _sheetCache[i] = new XSSFEvaluationSheet(_uBook.getSheetAt(i)); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 19176392d1..980f1b7ad6 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -379,18 +379,17 @@ public class XSSFRichTextString implements RichTextString { * index is out of range. */ public XSSFFont getFontAtIndex( int index ) { - if(st.sizeOfRArray() == 0) return null; - + final ThemesTable themes = getThemesTable(); int pos = 0; - for(int i = 0; i < st.sizeOfRArray(); i++){ - CTRElt r = st.getRArray(i); - if(index >= 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(); |