[jdom-interest] Namespace.hashcode patch

Bryan Thale thale at labs.mot.com
Thu May 31 09:21:17 PDT 2001

> Since use cases for comparing namespaces are so few I would hope we could
> just make the current api work.  I think we had an uneasy truce with the
> current api before the change to hashcode.  Aside from correctness
> arguments, did you have another problem with the hashcode implementation? I
> really don't see how we can have hashcode relate to the uri only and expect
> it to work in a List.

Well, I don't think the Collections classes will work properly if you
break the relationship between equals() and hashCode().  The hash code
is used to speed up searches so equals() is only invoked if hashCode()
returns the same value for the two objects.  If hashCode() includes the
prefix but equals() does not, then the search routine will miss
potential matches were a.equals(b) is satisfied but a.hashCode() ==
b.hashCode() is not.  I suspect that mismatch could cause things to fail
in very subtle and extremely hard to find ways.

Thinking some more about it last night, I think the addition of a
matches() or equalsIgnorePrefix() method to Namespace and returning
equals() and hashCode() to their original coverage of prefix and URI is
perhaps the way out of this situation.   Since Namespaces are global,
there is really a many to many relationship between prefixes and URIs,
so the current representation of namespaces as a collection of
prefix-URI pairings does actually represent what is going on.  The only
thing Namespace is missing is a way to test for XML equality.

What do you think about the attached patch?


Bryan Thale
Motorola Labs, Networking and Infrastructure Research
mailto:thale at labs.mot.com

-------------- next part --------------
Index: ./src/java/org/jdom/Namespace.java
RCS file: /home/cvspublic/jdom/src/java/org/jdom/Namespace.java,v
retrieving revision 1.28
diff -r1.28 Namespace.java
>     /**
>      * <p>
>      *  This tests for XML equality - Two <code>Namespaces</code>
>      *  match if and only if their URIs are byte-for-byte equals, the prefix,
>      *  if any, is not considered in the comparison.
>      * </p>
>      *
>      * @param ns <code>Namespace</code> to compare to this <code>Namespace</code>.
>      * @return <code>boolean</code> - whether the supplied <code>Namespace</code> 
>      *         represents the same XML namespace as this <code>Namespace</code>.
>      *         
>      * @see #equals(Object)
>      */
>     public boolean matches(Namespace ns) {
>         return uri.equals(ns.uri);
>     }
<      *  This tests for equality - Two <code>Namespaces</code>
<      *  are equal if and only if their URIs are byte-for-byte equals.
>      *  This tests for objective equality - Two <code>Namespaces</code>
>      *  are equal if and only if their prefixes and URIs are byte-for-byte 
>      *  equals.
>      *         
>      * @see #matches(Namespace)
<             return uri.equals(((Namespace)ob).uri);
>             return (prefix.equals(((Namespace)ob).prefix) 
>                    && uri.equals(((Namespace)ob).uri));
<      *  If two namespaces have the same URI, they are equal and have the same
<      *  hash code, even if they have different prefixes.
>      *  If two <code>Namespace</code> have the same URI and prefix, they are 
>      *  equal and have the same hash code.
>      * 
>      * @see java.lang.Object#hashCode()
<         return uri.hashCode();
>         return prefix.hashCode() + uri.hashCode();

The command completed successfully.

More information about the jdom-interest mailing list