From 22badcef272f4c61313b2ea1e520f88f9c65a400 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sat, 16 Jul 2022 18:08:50 +0000 Subject: [PATCH] [github-342] Use foreach. Thanks to XenoAmess. This closes #342 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902778 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/XSSFDataValidationHelper.java | 3 +-- .../org/apache/poi/hpbf/model/EscherPart.java | 8 +++---- .../apache/poi/hslf/dev/SLWTTextListing.java | 14 +++++------ .../org/apache/poi/hslf/model/MovieShape.java | 8 +++---- .../apache/poi/hslf/record/Environment.java | 10 ++++---- .../org/apache/poi/hslf/record/ExObjList.java | 14 +++++------ .../poi/hslf/usermodel/HSLFSoundData.java | 14 +++++------ .../java/org/apache/poi/hwpf/model/Ffn.java | 4 ++-- .../org/apache/poi/hwpf/model/FontTable.java | 5 ++-- .../poi/hssf/eventusermodel/HSSFRequest.java | 3 +-- .../org/apache/poi/hssf/model/LinkTable.java | 5 +--- .../org/apache/poi/hssf/record/ObjRecord.java | 3 +-- .../ChartSubstreamRecordAggregate.java | 5 ++-- .../CustomViewSettingsRecordAggregate.java | 5 ++-- .../aggregates/ValueRecordsAggregate.java | 23 +++++++++---------- .../poi/hssf/usermodel/HSSFDataFormat.java | 4 +--- .../poi/hssf/usermodel/HSSFObjectData.java | 4 +--- .../apache/poi/poifs/dev/POIFSViewEngine.java | 9 ++++---- .../apache/poi/ss/formula/CellCacheEntry.java | 8 +++---- .../poi/ss/formula/FormulaCellCache.java | 5 ++-- .../poi/ss/formula/FormulaCellCacheEntry.java | 7 +++--- .../ss/formula/FormulaCellCacheEntrySet.java | 3 +-- .../poi/ss/formula/functions/Count.java | 4 ++-- .../poi/ss/formula/functions/Counta.java | 4 ++-- .../apache/poi/ss/formula/functions/Gcd.java | 6 ++--- .../apache/poi/ss/formula/functions/Lcm.java | 6 ++--- .../MultiOperandNumericFunction.java | 4 ++-- .../poi/ss/formula/functions/StatsLib.java | 16 ++++++------- .../apache/poi/ss/util/TestCellRangeUtil.java | 4 ++-- 29 files changed, 94 insertions(+), 114 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java index 06bfb3e4f9..e10d8b6816 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFDataValidationHelper.java @@ -160,8 +160,7 @@ public class XSSFDataValidationHelper implements DataValidationHelper { CellRangeAddress[] cellRangeAddresses = cellRangeAddressList.getCellRangeAddresses(); List sqref = new ArrayList<>(); - for (int i = 0; i < cellRangeAddresses.length; i++) { - CellRangeAddress cellRangeAddress = cellRangeAddresses[i]; + for (CellRangeAddress cellRangeAddress : cellRangeAddresses) { sqref.add(cellRangeAddress.formatAsString()); } newDataValidation.setSqref(sqref); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hpbf/model/EscherPart.java b/poi-scratchpad/src/main/java/org/apache/poi/hpbf/model/EscherPart.java index 44cc2aee7f..cdda185953 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hpbf/model/EscherPart.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hpbf/model/EscherPart.java @@ -85,15 +85,15 @@ public abstract class EscherPart extends HPBFPart { */ protected void generateData() { int size = 0; - for(int i=0; i links = new ArrayList<>(); - for(int i=0; i<_children.length; i++) { - if(_children[i] instanceof ExHyperlink) { - links.add( (ExHyperlink)_children[i] ); + for (Record child : _children) { + if (child instanceof ExHyperlink) { + links.add((ExHyperlink) child); } } @@ -114,10 +114,10 @@ public class ExObjList extends RecordContainer { * @return found ExHyperlink or null */ public ExHyperlink get(int id){ - for(int i=0; i<_children.length; i++) { - if(_children[i] instanceof ExHyperlink) { - ExHyperlink rec = (ExHyperlink)_children[i]; - if (rec.getExHyperlinkAtom().getNumber() == id){ + for (Record child : _children) { + if (child instanceof ExHyperlink) { + ExHyperlink rec = (ExHyperlink) child; + if (rec.getExHyperlinkAtom().getNumber() == id) { return rec; } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSoundData.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSoundData.java index d6403b204e..10b299cf4f 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSoundData.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSoundData.java @@ -75,13 +75,13 @@ public final class HSLFSoundData { public static HSLFSoundData[] find(Document document){ ArrayList lst = new ArrayList<>(); org.apache.poi.hslf.record.Record[] ch = document.getChildRecords(); - for (int i = 0; i < ch.length; i++) { - if(ch[i].getRecordType() == RecordTypes.SoundCollection.typeID){ - RecordContainer col = (RecordContainer)ch[i]; - org.apache.poi.hslf.record.Record[] sr = col.getChildRecords(); - for (int j = 0; j < sr.length; j++) { - if(sr[j] instanceof Sound){ - lst.add(new HSLFSoundData((Sound)sr[j])); + for (Record value : ch) { + if (value.getRecordType() == RecordTypes.SoundCollection.typeID) { + RecordContainer col = (RecordContainer) value; + Record[] sr = col.getChildRecords(); + for (Record record : sr) { + if (record instanceof Sound) { + lst.add(new HSLFSoundData((Sound) record)); } } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/Ffn.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/Ffn.java index 7c30f2346a..3f925f8626 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/Ffn.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/Ffn.java @@ -161,8 +161,8 @@ public final class Ffn { System.arraycopy(_fontSig, 0, buf, offset, _fontSig.length); offset += _fontSig.length; - for (int i = 0; i < _xszFfn.length; i++) { - LittleEndian.putShort(buf, offset, (short) _xszFfn[i]); + for (char c : _xszFfn) { + LittleEndian.putShort(buf, offset, (short) c); offset += LittleEndianConsts.SHORT_SIZE; } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/FontTable.java b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/FontTable.java index 1a67d1c87d..947f8e1009 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/FontTable.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/model/FontTable.java @@ -129,9 +129,8 @@ public final class FontTable LittleEndian.putShort(buf, 0, _extraDataSz); tableStream.write(buf); - for(int i = 0; i < _fontNames.length; i++) - { - tableStream.write(_fontNames[i].toByteArray()); + for (Ffn fontName : _fontNames) { + tableStream.write(fontName.toByteArray()); } } diff --git a/poi/src/main/java/org/apache/poi/hssf/eventusermodel/HSSFRequest.java b/poi/src/main/java/org/apache/poi/hssf/eventusermodel/HSSFRequest.java index d2ea4b259f..5aa3bd396e 100644 --- a/poi/src/main/java/org/apache/poi/hssf/eventusermodel/HSSFRequest.java +++ b/poi/src/main/java/org/apache/poi/hssf/eventusermodel/HSSFRequest.java @@ -93,8 +93,7 @@ public class HSSFRequest { if (listeners != null) { - for (int k = 0; k < listeners.size(); k++) { - Object listenObj = listeners.get(k); + for (Object listenObj : listeners) { if (listenObj instanceof AbortableHSSFListener) { AbortableHSSFListener listener = (AbortableHSSFListener) listenObj; userCode = listener.abortableProcessRecord(rec); diff --git a/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java b/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java index a1fdb089c4..25f4aff843 100644 --- a/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java +++ b/poi/src/main/java/org/apache/poi/hssf/model/LinkTable.java @@ -294,10 +294,7 @@ final class LinkTable { * @param sheetNumber 1-based sheet number */ public NameRecord getSpecificBuiltinRecord(byte builtInCode, int sheetNumber) { - Iterator iterator = _definedNames.iterator(); - while (iterator.hasNext()) { - NameRecord record = iterator.next(); - + for (NameRecord record : _definedNames) { //print areas are one based if (record.getBuiltInName() == builtInCode && record.getSheetNumber() == sheetNumber) { return record; diff --git a/poi/src/main/java/org/apache/poi/hssf/record/ObjRecord.java b/poi/src/main/java/org/apache/poi/hssf/record/ObjRecord.java index beea8ac7e8..e87c7b6fc4 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/ObjRecord.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/ObjRecord.java @@ -173,8 +173,7 @@ public final class ObjRecord extends Record { if (_uninterpretedData == null) { - for (int i = 0; i < subrecords.size(); i++) { - SubRecord record = subrecords.get(i); + for (SubRecord record : subrecords) { record.serialize(out); } int expectedEndIx = offset + dataSize; diff --git a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ChartSubstreamRecordAggregate.java b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ChartSubstreamRecordAggregate.java index 7b508cb82b..8ddee0d453 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ChartSubstreamRecordAggregate.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ChartSubstreamRecordAggregate.java @@ -69,12 +69,11 @@ public final class ChartSubstreamRecordAggregate extends RecordAggregate { return; } rv.visitRecord(_bofRec); - for (int i = 0; i < _recs.size(); i++) { - RecordBase rb = _recs.get(i); + for (RecordBase rb : _recs) { if (rb instanceof RecordAggregate) { ((RecordAggregate) rb).visitContainedRecords(rv); } else { - rv.visitRecord((org.apache.poi.hssf.record.Record) rb); + rv.visitRecord((Record) rb); } } rv.visitRecord(EOFRecord.instance); diff --git a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CustomViewSettingsRecordAggregate.java b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CustomViewSettingsRecordAggregate.java index 75ea21f79a..9347fcf79e 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CustomViewSettingsRecordAggregate.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/CustomViewSettingsRecordAggregate.java @@ -73,12 +73,11 @@ public final class CustomViewSettingsRecordAggregate extends RecordAggregate { return; } rv.visitRecord(_begin); - for (int i = 0; i < _recs.size(); i++) { - RecordBase rb = _recs.get(i); + for (RecordBase rb : _recs) { if (rb instanceof RecordAggregate) { ((RecordAggregate) rb).visitContainedRecords(rv); } else { - rv.visitRecord((org.apache.poi.hssf.record.Record) rb); + rv.visitRecord((Record) rb); } } rv.visitRecord(_end); diff --git a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java index d5a9ba5478..185a484969 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/aggregates/ValueRecordsAggregate.java @@ -131,11 +131,10 @@ public final class ValueRecordsAggregate implements Iterable i = workbook.getFormats().iterator(); - while (i.hasNext()) { - FormatRecord r = i.next(); + for (FormatRecord r : workbook.getFormats()) { ensureFormatsSize(r.getIndexCode()); _formats.set(r.getIndexCode(), r.getFormatString()); } diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java index 3909f41400..27ea1deebe 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java @@ -82,10 +82,8 @@ public final class HSSFObjectData extends HSSFPicture implements ObjectData { * Exception if there wasn't one */ protected EmbeddedObjectRefSubRecord findObjectRecord() { - Iterator subRecordIter = getObjRecord().getSubRecords().iterator(); - while (subRecordIter.hasNext()) { - Object subRecord = subRecordIter.next(); + for (Object subRecord : getObjRecord().getSubRecords()) { if (subRecord instanceof EmbeddedObjectRefSubRecord) { return (EmbeddedObjectRefSubRecord) subRecord; } diff --git a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSViewEngine.java b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSViewEngine.java index 8eee26e13c..f9712b67f4 100644 --- a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSViewEngine.java +++ b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSViewEngine.java @@ -67,11 +67,10 @@ public class POIFSViewEngine { Object[] data = inspected.getViewableArray(); - for (int j = 0; j < data.length; j++) - { - objects.addAll(inspectViewable(data[ j ], drilldown, - indentLevel + 1, - indentString)); + for (Object datum : data) { + objects.addAll(inspectViewable(datum, drilldown, + indentLevel + 1, + indentString)); } } else diff --git a/poi/src/main/java/org/apache/poi/ss/formula/CellCacheEntry.java b/poi/src/main/java/org/apache/poi/ss/formula/CellCacheEntry.java index 5b10f8d99b..6f428dc692 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/CellCacheEntry.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/CellCacheEntry.java @@ -111,8 +111,7 @@ abstract class CellCacheEntry implements ICacheEntry { protected final void recurseClearCachedFormulaResults() { FormulaCellCacheEntry[] formulaCells = getConsumingCells(); - for (int i = 0; i < formulaCells.length; i++) { - FormulaCellCacheEntry fc = formulaCells[i]; + for (FormulaCellCacheEntry fc : formulaCells) { fc.clearFormulaEntry(); if (fc != this) { fc.recurseClearCachedFormulaResults(); @@ -127,11 +126,10 @@ abstract class CellCacheEntry implements ICacheEntry { FormulaCellCacheEntry[] formulaCells = getConsumingCells(); listener.sortDependentCachedValues(formulaCells); - for (int i = 0; i < formulaCells.length; i++) { - FormulaCellCacheEntry fc = formulaCells[i]; + for (FormulaCellCacheEntry fc : formulaCells) { listener.onClearDependentCachedValue(fc, depth); fc.clearFormulaEntry(); - fc.recurseClearCachedFormulaResults(listener, depth+1); + fc.recurseClearCachedFormulaResults(listener, depth + 1); } } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCache.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCache.java index ccb808bbe0..7cd620e5d0 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCache.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCache.java @@ -61,9 +61,8 @@ final class FormulaCellCache { } public void applyOperation(IEntryOperation operation) { - Iterator i = _formulaEntriesByCell.values().iterator(); - while (i.hasNext()) { - operation.processEntry(i.next()); + for (FormulaCellCacheEntry formulaCellCacheEntry : _formulaEntriesByCell.values()) { + operation.processEntry(formulaCellCacheEntry); } } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntry.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntry.java index 0a7b8fbdbf..901f5f1656 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntry.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntry.java @@ -80,8 +80,8 @@ final class FormulaCellCacheEntry extends CellCacheEntry { CellCacheEntry[] prevUsedCells = _sensitiveInputCells; int nUsed = usedCells.length; - for (int i = 0; i < nUsed; i++) { - usedCells[i].addConsumingCell(this); + for (CellCacheEntry usedCell : usedCells) { + usedCell.addConsumingCell(this); } if (prevUsedCells == null) { return; @@ -97,8 +97,7 @@ final class FormulaCellCacheEntry extends CellCacheEntry { usedSet = new HashSet<>(nUsed * 3 / 2); usedSet.addAll(Arrays.asList(usedCells).subList(0, nUsed)); } - for (int i = 0; i < nPrevUsed; i++) { - CellCacheEntry prevUsed = prevUsedCells[i]; + for (CellCacheEntry prevUsed : prevUsedCells) { if (!usedSet.contains(prevUsed)) { // previously was used by cellLoc, but not anymore prevUsed.clearConsumingCell(this); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntrySet.java b/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntrySet.java index d4a376c4d6..8e15f54fd6 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntrySet.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/FormulaCellCacheEntrySet.java @@ -41,8 +41,7 @@ final class FormulaCellCacheEntrySet { } FormulaCellCacheEntry[] result = new FormulaCellCacheEntry[nItems]; int j=0; - for(int i=0; i<_arr.length; i++) { - FormulaCellCacheEntry cce = _arr[i]; + for (FormulaCellCacheEntry cce : _arr) { if (cce != null) { result[j++] = cce; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java index 9404e22599..eca5d422b4 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Count.java @@ -61,8 +61,8 @@ public final class Count implements Function { int temp = 0; - for(int i=0; i evals = new ArrayList<>(); - for (int i = 0; i < args.length; i++) { - ValueEval ve = OperandResolver.getSingleValue(args[i], ec.getRowIndex(), ec.getColumnIndex()); + for (ValueEval arg : args) { + ValueEval ve = OperandResolver.getSingleValue(arg, ec.getRowIndex(), ec.getColumnIndex()); double d = OperandResolver.coerceValueToDouble(ve); if (isInvalidInput(d)) { return ErrorEval.NUM_ERROR; } - evals.add((long)d); + evals.add((long) d); } long result = evals.get(0); for (int i = 1; i < evals.size(); i++) { diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Lcm.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Lcm.java index 25d61b2395..a2858892f7 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Lcm.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Lcm.java @@ -63,13 +63,13 @@ public class Lcm implements FreeRefFunction { } else { try { ArrayList evals = new ArrayList<>(); - for (int i = 0; i < args.length; i++) { - ValueEval ve = OperandResolver.getSingleValue(args[i], ec.getRowIndex(), ec.getColumnIndex()); + for (ValueEval arg : args) { + ValueEval ve = OperandResolver.getSingleValue(arg, ec.getRowIndex(), ec.getColumnIndex()); double d = OperandResolver.coerceValueToDouble(ve); if (isInvalidInput(d)) { return ErrorEval.NUM_ERROR; } - evals.add((long)d); + evals.add((long) d); } long result = evals.get(0); for (int i = 1; i < evals.size(); i++) { diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java index adbddd8ba4..2723c7dc0c 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/MultiOperandNumericFunction.java @@ -114,8 +114,8 @@ public abstract class MultiOperandNumericFunction implements Function { } DoubleList retval = new DoubleList(); - for (int i = 0, iSize = operands.length; i < iSize; i++) { - collectValues(operands[i], retval); + for (ValueEval operand : operands) { + collectValues(operand, retval); } return retval.toArray(); } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/StatsLib.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/StatsLib.java index fb5aefc1aa..1db7abda32 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/StatsLib.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/StatsLib.java @@ -37,13 +37,13 @@ final class StatsLib { double r = 0; double m = 0; double s = 0; - for (int i=0, iSize=v.length; i getCellAddresses(CellRangeAddress[] ranges) { final Set set = new HashSet<>(); for (final CellRangeAddress range : ranges) { - for (Iterator iter = range.iterator(); iter.hasNext(); ) { - set.add(iter.next()); + for (CellAddress cellAddress : range) { + set.add(cellAddress); } } return set; -- 2.39.5