[jdom-interest] Collections.sort and multiple parent-child re lationships

Robertson, Jason Jason.Robertson at acs-inc.com
Wed Sep 18 07:45:31 PDT 2002

Without getting into is it right or wrong to do the check, would it be that
horrible to just turn it off temporarily for sorting since that seems to be
the one place that it messes things up? Something like this on the Element
object, perhaps:

public void sortChildren(Comparator c)
    Collections.sort(getChildren(), c);


-----Original Message-----
From: Stephan Trebels [mailto:stephan at ncube.de]
Sent: Wednesday, September 18, 2002 2:56 AM
To: JDOM Interest List
Subject: [jdom-interest] Collections.sort and multiple parent-child


we had the "Collections sort on the children list of an element" problem
a bunch of times.  This is Collections.sort in 1.4.0_01:

    public static void sort(List list) {
	Object a[] = list.toArray();
	ListIterator i = list.listIterator();
	for (int j=0; j<a.length; j++) {

IOW, the sorting is not the problem, it is the storing via the
ListIterator later.  JDOM will complain that an element can only be
the child of another element once.

What is the technical reason for this (except, that it looks cleaner)?
I do understand that we want a directed graph, but this is ensured by
parent attribute of the element already.  I don't see why a temporary
or even permanent state ala 

element1.GetChildren() == [ element2 element2 element2 ]

would hurt anything in current JDOM code.  We don't really do coloured
graph traversion or anything similar?

So, do we _really need_ the assertion, that a child is only contained
in a parent at most once?


[------------ Stephan Trebels <stephan at ncube.de>, Consultant -----------]
company: nCUBE Deutschland GmbH, Hanauer Str. 56, 80992 Munich, Germany
phone: cell:+49 172 8433111  office:+49 89 149893 0  fax:+49 89 149893 50
To control your jdom-interest membership:

More information about the jdom-interest mailing list