[jdom-interest] Attribute comparison

Jason Hunter jhunter at collab.net
Tue Dec 5 02:33:30 PST 2000


> I've noticed that the Attribute.equals() method of Attribute (and
> other JDOM elements) only checks for reference equality. This means
> that the result of :
> 
> (att1 == att2) and (att1.equals(att2)) is the same.
> 
> Is there any reason for this. (I remember several months ago there was
> discussion in the list about that, but I can't really find it)

Yep, it's desirable behavior esp when you consider how Element.equals()
should behave.  Trying to do a full equivalency check on Element is a
losing proposition.  You'd have to recurse down the entire tree
comparing everything!  It's far better for the default .equals() to do
== and to let users implement a Comparable if they want to compare just
name/namespace or something.

It leads then that Attribute should behave similarly to Element, and
it'll be especially important if/when we add attribute.getParent() and
attributes become something that can't generally be equivalent without
being ==.

Also == helps for types like Comment because it allow you to remove a
comment in a list with getMixedContent().remove(comment), and you can't
accidentally remove a Comment that appears earlier with the same text.

-jh-



More information about the jdom-interest mailing list