[jdom-interest] minore possible bug in ContentList.ensureCapacity?

Laurent Bihanic laurent.bihanic at atosorigin.com
Thu Jun 26 07:30:39 PDT 2003


The attached patch fixes the problem for both AttributeList and ContentList 
and also optimizes the capacity when adding collections.

Laurent


Vadim.Strizhevsky at morganstanley.com wrote:
> ensureCapacity in ContentList seems to ignore passed minCapacity when
> elementData==null. Instead it always uses 5. Now as far as I can tell
> there's no way to actually trigger this to cause a bug, but it feels
> wrong, could cause problem in future?, and I think if you do
> element.setContent(LargeList) it will have at least one extra
> allocation/copy to do, and possibly more.
> 
> I check and this is both in b9 and last nights build.
> 
> Thanks,
> -Vadim
> 
-------------- next part --------------
Index: AttributeList.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/AttributeList.java,v
retrieving revision 1.18
diff -r1.18 AttributeList.java
82a83,84
>     private static final int INITIAL_ARRAY_SIZE = 5;
> 
227a230
>         ensureCapacity(size() + collection.size());
279d281
<             ensureCapacity(collection.size());
308c310,313
<             elementData = new Attribute[minCapacity];
---
>             int newCapacity = INITIAL_ARRAY_SIZE;
>             if (newCapacity < minCapacity)
>                 newCapacity = minCapacity;
>             elementData = new Attribute[newCapacity];
-------------- next part --------------
Index: ContentList.java
===================================================================
RCS file: /home/cvspublic/jdom/src/java/org/jdom/ContentList.java,v
retrieving revision 1.27
diff -r1.27 ContentList.java
538a539
>         ensureCapacity(size() + collection.size());
589d589
<             ensureCapacity(collection.size());
616,618c616,622
<         if( elementData==null ) {
<             elementData = new Object[INITIAL_ARRAY_SIZE];
<         } else {
---
>         if (elementData == null) {
>             int newCapacity = INITIAL_ARRAY_SIZE;
>             if (newCapacity < minCapacity)
>                 newCapacity = minCapacity;
>             elementData = new Object[newCapacity];
>         }
>         else {


More information about the jdom-interest mailing list