[jdom-interest] equality of Namespaces
Szegedi, Attila
szegedi at scriptum.hu
Mon Jul 30 01:06:25 PDT 2001
Namespace doesn't use semantic comparison; rather Namespace objects are
interned by the Namespace factory method Namespace.getNamespace(String
prefix, String uri), so the two bar elements actually share a single
Namespace object.
Attila.
> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of bob mcwhirter
> Sent: Saturday, July 28, 2001 2:42 PM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] equality of Namespaces
>
>
>
> Given a document:
>
> <foo>
> <bar xmlns:bar="http://bar.org/"/>
> <bar xmlns:bar="http://bar.org/"/>
> </foo>
>
> And an XPath of
>
> //namespace::*
>
> 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.
>
> Comments?
>
> -bob
>
> (fwiw, dom4j suffers from this too)
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com
More information about the jdom-interest
mailing list