[jdom-interest] equality of Namespaces

bob mcwhirter bob at werken.com
Sat Jul 28 05:42:02 PDT 2001

Given a document:

		<bar xmlns:bar="http://bar.org/"/>	
		<bar xmlns:bar="http://bar.org/"/>	

And an XPath of 


I beleive I should select a NodeSet of two Namespace
nodes, both of which have the same prefix and URI
(but, different parents).

Internally, maybe I'm using a HashSet to maintain
the NodeSet semantics of unordered/unique nodes.

With the currently implementation of Namespace, the
equals() and hashCode() which are used by HashSet
(and the underlying HashMap) result in one of the
Namespace objects not being included, since it's
semantically a duplicate.

Since JDOM uses identity comparison on pretty much
all other objects, why are we doing semantic comparison
with Namespaces?

I'd argue that Namespace should follow Element and
other identity-based equality comparisons.

Currently, they are compared as identical, due to the
fact we're losing information in their construction.
Once you lose the parentage information, they become
semantically equivelent.  If we were tracking parentages,
then even a semantic equivelent would disappear, since
equals() would appear more like:

    public boolean equals(Object ob) {
        if (ob == null) {
            return false;

        if (ob instanceof Namespace) {
            Namespace ns = (Namespace)ob;
            // Compare URIs
            if (ns.getURI().equals(uri) 
                && ns.getPrefix().equals(prefix)
                && ns.getParent().equals(parent)) {
                return true;
        return false;

This might not be a problem if we all lived in a 
java 1.4 world, where IdentityHashMap exists, but,
we don't.



(fwiw, dom4j suffers from this too)

More information about the jdom-interest mailing list