[jdom-interest] PartialList.remove(int)

Tom Fennelly Tom.Fennelly at MobileAware.com
Thu May 24 04:13:13 PDT 2001


Noticed something which may be of interest.  I'm sure it's already been
noted but from a search of your mailing archives I didn't find any
references to it.

AbstractList.indexOf(Object) returns a matching list index based on the
first successful Object.equals() comparison.  Therefore if you've got a list
with the same string in it more than once (i.e. different String object
instances but containing the same string - not multiple list entries to the
same object reference) you can run into a problem with the integrity of your
PartialList/backingList relationship.  In the case of
PartialList.remove(int), if you attempt to remove e.g. the 2nd string from
the list you end up removing the 2nd from the PartialList (OK) but the 1st
in the backingList (not OK).  In the case of PartialList.remove(Object) I
guess things will get screwed also.  I've seen this happen in practice with
the JDOM - it's not something I'm predicting will happen.

    public Object remove(int index) {
        Object o = super.remove(index);
        backingList.remove(o);
        if (o instanceof Element) {
            ((Element)o).setParent(null);
        }
        return o;
    }

Of course in theory it's not going to happen with just Strings - any Object
who's equals() method performs more than a simple reference comparison.
I've only seen it with String objects.

Obviously this could be sorted by overriding indexOf in PartialList to only
perform a reference comparison with some similar work around for getting the
proper index within the backingList but you mightn't want to do that because
of the possible side effects.

Cheers.

Tom.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010524/d9d2e8b7/attachment.htm


More information about the jdom-interest mailing list