[jdom-interest] Ideas for API changes

Tom Bradford bradford at dbxmlgroup.com
Sun Apr 29 15:52:20 PDT 2001

Jason Hunter wrote:
> > I'm not sure there needs to be more than that. I think having the
> > common type alone because it really strengthens type checking
> > throughout the API and makes a lot of code simpler. For instance, in
> > addContent() we no longer need to check for seven different types at
> > runtime before adding the object. All the checks can be performed at
> > compile time. I don't know that this would be significantly faster,
> > but it would be conceptually much simpler.
> Node does not take care of this issue!  You removed from your message my
> discussion of the problem.  Having node *weakens* type checking.
> If Element had an addContent(Node) and Document implemented Node then an
> Element could add a Document.  Similarly, if Document has an
> addContent(Node) then Document could add more than one element, Document
> could add Entity objects which don't belong at the Document level.
> Heck, Documents could add Documents.
> So that means with everything accepting Node is we'd still have to do
> lots of runtime checks because not all Nodes are created equal.  I
> prefer having the compiler do the checks.  Better to catch errors at
> compile time than runtime.  Plus it makes clear to people that Document
> only accepts certain types of objects, Element only accepts certain
> types, etc.
> Also, taking a Node interface parameter isn't really safer than taking
> Object because I could still write MyStupidClass implements Node (but
> nothing else) and have it added to any Element even though it's not
> related to XML.  How would you catch that?  By doing exhaustive runtime
> checking within the method?  By just letting this mysterious object be
> added?

Why not add marker interfaces to Node addition... Example:

public interface ElementChild {}
public interface DocumentChild {}

public class Text extends Node implements ElementChild ... {

public class Element extends Node implements ElementChild, DocumentChild
... {
   public void addChild(ElementChild child);

Then your addChild method for specific container types (Element,
Document, etc...) can take classes that implement their particular
marker interfaces and chain off to a protected super.addChild method. 
You get compile-time type checking and code reuse.

Tom Bradford --- The dbXML Project --- http://www.dbxml.org/
We store your XML data a hell of a lot better than /dev/null

More information about the jdom-interest mailing list