[jdom-interest] Sorting a JDOM tree

Robertson, Jason Jason.Robertson at acs-inc.com
Fri Sep 13 13:41:02 PDT 2002


Ok, so this whole "doesn't work with Collections.sort" this was just bugging
me so I decided to waste some time looking at it. Here's what I've found,
and I don't understand...

I'll go through the whole thought process:

First, I modified the check in ContentList.add(int, Element) to this (the
2nd check is new):

if ((element.getParent() != null) && 
    (element.getParent() != parent)) {
  throw new IllegalAddException(
    "The element already has an existing parent \"" +
    element.getParent().getQualifiedName() + "\"");
}

I quickly realized this was bad because while Collections.sort now worked it
created other problems (like the ability to add an element multiple times to
the same parent).

So, after looking at remove(int) and seeing the parent is cleared when an
element is removed (as expected), I wondered how Collections.sort worked,
specifically wondering if it did a remove before reinserting into the sorted
location (which seemed reasonable). If it did this, I would expect things to
work, because you obviously wouldn't get the "already has a parent" error if
the parent was being cleared out before re-adding.

So I added code to print a debug statement when the add and remove methods
were called, and saw this (see attached test code for impl details, but
summary is the sort should group all 'elem1' elements then 'elem2'
elements):

Starting to sort...
Comparing elem1 to elem2; returning -1
Comparing elem2 to elem1; returning 1
Comparing elem1 to elem1; returning 0
Inside remove(1)
Inside add(1, 'elem1')
Inside remove(3)
Inside add(3, 'elem1')
Inside remove(5)
Inside add(5, 'elem2')
...

Well, this is exactly what I was hoping for...so why don't things work? I
took out the extra check I'd added above and re-ran and got this:

Starting to sort...
Comparing elem1 to elem2; returning -1
Comparing elem2 to elem1; returning 1
Comparing elem1 to elem1; returning 0
Inside remove(1)
Inside add(1, 'elem1')
Inside remove(3)
Inside add(3, 'elem1')
Inside add(3, 'elem2')
Exception in thread "main" ...

Uhhh, what happened to remove(5) and add(5, 'elem2')??? Why in the world
would adding the check in the ContentList add method have any effect on how
Collections.sort works?

I've now officially wasted too much time. Anyone have any ideas? Is it too
late on a Friday and I'm not seeing something obvious?

Jason


-----Original Message-----
From: Alex Rosen [mailto:arosen at silverstream.com]
Sent: Friday, September 13, 2002 11:21 AM
To: 'Philip Nelson'; 'Olivier Coppel'; jdom-interest at jdom.org
Subject: RE: [jdom-interest] Sorting a JDOM tree


> At any rate, any reference on sorting a java.util.List will
> do as the the list returned from getContent sorts of calls are live.

Unfortunately that won't actually work. See the comments in the code at:

http://www.servlets.com/archive/servlet/ReadMsg?msgId=211785&listName=jdom-i
nterest

Alex

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com

-------------- next part --------------
A non-text attachment was scrubbed...
Name: JdomSort.java
Type: application/octet-stream
Size: 2691 bytes
Desc: not available
Url : http://jdom.org/pipermail/jdom-interest/attachments/20020913/a42f6ca5/JdomSort.obj


More information about the jdom-interest mailing list