[jdom-interest] children as List

Elliotte Rusty Harold elharo at metalab.unc.edu
Fri Sep 1 04:56:00 PDT 2000

At 10:51 AM +0100 9/1/00, Cameron Smith wrote:
>I think its the right decision to present the children of an Element as a
>List rather than having List-like behaviour in Element itself.
>This is because:
>1)  List is almost a 'basic type' now in java: so using it is 'second
>nature', as opposed to NodeList in DOM.

This is irrelevant. If we made Element a List we'd still be using all 
the familiar methods of java.util.List.  We're not proposing to 
eliminate the use of the List interface, just to make Element 
implement it. It would not be as unfamiliar as NodeList.

>2)  If we take the idea of 'pure objects' to the extreme then every class we
>create which has a variable number of a certain type of object would need
>its own 'list-style' interface.  Instead, whenever I see a '1-n'
>relationship between parent and children I think of a 'List' or 'Iterator'
>interface to provide access to it.

You could get an iterator on the children. And you could get a List 
of the children. It would just be a little easier to do so.

>The main downside of this is the vulnerability of exposing the List to
>mangling by user code.   However user code which wants to protect itself
>(and in many cases will /want/ to be mangling the List) from this can use
>the Collections.unmodifiableList() method.

The List is still exposed in either version.

>Another issue was ClassCastExceptions.  I think this is more an issue of
>whether or not there should be a unified class hierarchy in JDom, so that
>everything can be safely cast to 'Element', (like Node in DOM) than an issue
>about where to put List behaviour.

This can be fixed in either version.

None of these points argue to the question of whether Element should 
contain a List or implement a List. They are all equally applicable 
to both cases.

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |

More information about the jdom-interest mailing list