Hi Andy Thanks for this patch! Well found.
Sorry this took a little time to apply and test. I made a minor modification to your patch, where the code now tests the index to be >= 0 and <= size() first and throws IndexOutOfBoundsException as per the List javadoc. By the time you get this mail the code should be committed into CVS. Hopefully the daily build will be working again soon and a 1.4 release is just around the corner... James ----- Original Message ----- From: "Andy Yang" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, May 27, 2002 8:25 PM Subject: [dom4j-dev] BackedList::add() > Hi there, > > The Element::elements() methods return a List > interface which is implemented as a BackedList. When > we sought to use the List::add() method as follows: > > root.elements().add(5, element); > > where root currently already contains 5 elements. > > We were getting an IndexOutOfBoundsException which > reported itself with index=5, size=5 which doesn't > match the conditions specified in the List interface > JavaDoc: > > IndexOutOfBoundsException - if the index is out of > range (index < 0 || index > size()). > > Looking into BackedList::add() it becomes obvious that > the exception was actually being thrown by the > List::get() method in which case, the exception makes > perfect sense. > > I'd like to propose the following change to the add > method to make BackedList::add() behave like > List::add() - if there are reasons not to do this, > then that's fine as well - but perhaps it should be a > note somewhere - I didn't notice anything like that in > the Cookbook (but I didn't do an exhaustive search). > > public void add(int index, Object object) > { > int realIndex = branchContent.size(); > if (index < realIndex) > { > realIndex = branchContent.indexOf( get(index) ); > } > if ( realIndex < 0 ) { > realIndex = ( index == 0 ) ? 0 : > Integer.MAX_VALUE; > } > if ( realIndex < branchContent.size() ) { > branchContent.add(realIndex, object); > } > else { > branchContent.add(object); > } > branch.childAdded( asNode( object ) ); > super.add(index, object); > } > > Regards, > Andy > > ______________________________________________________________________ > Find, Connect, Date! http://personals.yahoo.ca > > _______________________________________________________________ > > Don't miss the 2002 Sprint PCS Application Developer's Conference > August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm > > _______________________________________________ > dom4j-dev mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/dom4j-dev > _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com _______________________________________________________________ Don't miss the 2002 Sprint PCS Application Developer's Conference August 25-28 in Las Vegas -- http://devcon.sprintpcs.com/adp/index.cfm _______________________________________________ dom4j-dev mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-dev