diff options
author | Marius Volkhart <mariusvolkhart@apache.org> | 2021-03-01 00:04:51 +0000 |
---|---|---|
committer | Marius Volkhart <mariusvolkhart@apache.org> | 2021-03-01 00:04:51 +0000 |
commit | 402d0fc5e56349b3d6f447d56c8eef77a91b73ff (patch) | |
tree | e7b5e97819f1ad689f341ffc112af38616249c8c /src/java/org/apache | |
parent | d1c9a07860e365db4e335d24ad67e87544bdcceb (diff) | |
download | poi-402d0fc5e56349b3d6f447d56c8eef77a91b73ff.tar.gz poi-402d0fc5e56349b3d6f447d56c8eef77a91b73ff.zip |
Review EscherContainerRecord#getChildRecords() call sites for unnecessary work
This started off as wanting to add the EscherContainerRecord#getChildCount() function in order to do an efficient check for how many children the container has. This was desirable in new code for editing HSSF pictures. The existing option of calling getChildRecords().size() was undesirable as this requires a list copy first.
In the process of finding call sites that would benefit from replacing getChildRecords().size(), I realized that several other patterns would benefit from eliminating a copy, such as iterating over the children in a for-each loop, and indexed access to specific children.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1887020 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache')
4 files changed, 10 insertions, 6 deletions
diff --git a/src/java/org/apache/poi/ddf/EscherContainerRecord.java b/src/java/org/apache/poi/ddf/EscherContainerRecord.java index 474b24a436..bbe7efac65 100644 --- a/src/java/org/apache/poi/ddf/EscherContainerRecord.java +++ b/src/java/org/apache/poi/ddf/EscherContainerRecord.java @@ -158,6 +158,10 @@ public final class EscherContainerRecord extends EscherRecord implements Iterabl return new ArrayList<>(_childRecords); } + public int getChildCount() { + return _childRecords.size(); + } + /** * @return an iterator over the child records */ diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index b6d60ec471..c5703dfca3 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -1880,7 +1880,7 @@ public final class InternalWorkbook { DrawingManager2 dm = new DrawingManager2(dgg); if(bStore != null){ - for(EscherRecord bs : bStore.getChildRecords()){ + for (EscherRecord bs : bStore) { if(bs instanceof EscherBSERecord) { escherBSERecords.add((EscherBSERecord)bs); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java b/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java index 92d66e1db6..964356f5a6 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFShape.java @@ -186,9 +186,9 @@ public abstract class HSSFShape implements Shape { throw new IllegalArgumentException("Must use client anchors for shapes directly attached to sheet."); EscherClientAnchorRecord anch = _escherContainer.getChildById(EscherClientAnchorRecord.RECORD_ID); if (null != anch) { - for (i=0; i< _escherContainer.getChildRecords().size(); i++){ + for (i=0; i< _escherContainer.getChildCount(); i++){ if (_escherContainer.getChild(i).getRecordId() == EscherClientAnchorRecord.RECORD_ID){ - if (i != _escherContainer.getChildRecords().size() -1){ + if (i != _escherContainer.getChildCount() -1){ recordId = _escherContainer.getChild(i+1).getRecordId(); } } @@ -200,9 +200,9 @@ public abstract class HSSFShape implements Shape { throw new IllegalArgumentException("Must use child anchors for shapes attached to groups."); EscherChildAnchorRecord anch = _escherContainer.getChildById(EscherChildAnchorRecord.RECORD_ID); if (null != anch) { - for (i=0; i< _escherContainer.getChildRecords().size(); i++){ + for (i=0; i< _escherContainer.getChildCount(); i++){ if (_escherContainer.getChild(i).getRecordId() == EscherChildAnchorRecord.RECORD_ID){ - if (i != _escherContainer.getChildRecords().size() -1){ + if (i != _escherContainer.getChildCount() -1){ recordId = _escherContainer.getChild(i+1).getRecordId(); } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java index 4652693837..b8001fc651 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java @@ -54,7 +54,7 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { // read internal and external coordinates from spgrContainer EscherContainerRecord spContainer = spgrContainer.getChildContainers().get(0); _spgrRecord = (EscherSpgrRecord) spContainer.getChild(0); - for (EscherRecord ch : spContainer.getChildRecords()) { + for (EscherRecord ch : spContainer) { switch (EscherRecordTypes.forTypeID(ch.getRecordId())) { case SPGR: break; |