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

Reply via email to