[
Lists Home |
Date Index |
Thread Index
]
- From: Toby Speight <Toby.Speight@streapadair.freeserve.co.uk>
- To: "XML developers' list" <xml-dev@ic.ac.uk>
- Date: 13 Jan 2000 10:56:50 +0000
David> David Megginson <URL:mailto:david@megginson.com>
0> In article <m3r9fwtuv8.fsf@localhost.localdomain>, David wrote:
David> In principle (the principle of least surprise), it's very bad
David> behaviour for two objects to be == in C++ or equals() in Java
David> if any of their publicly-accessible fields differ. Think of
David> sets, for example.
You could follow the example of java.lang.String and define equals() to
compare all public data (as usual), and define an additional method
equalsIgnorePrefix() which doesn't. The problem with this is that
hashCode() has to be consistent with equals(), but sometimes you want
to use a hashCode() based on equalsIgnorePrefix(). I've solved this in
the past (for a case-insensitive String class) by making a wrapper that
defines equals() in terms of equalsIgnoreCase(), and a corresponding
hashCode(). I'm not sure I like this for SAX2 though, as we should be
making the common case - ignoring the prefix - the simplest.
public class QName {
public String getPrefix();
public String getUri();
public String getLocal();
public int hashCode(); // based on prefix, URI, local
public boolean hashCodeIgnorePrefix(Object o); // based on URI, local
public boolean equals(Object o); // based on prefix, URI, local
public boolean equalsIgnorePrefix(Object o); // based on URI, local
}
public class QNameIgnorePrefix {
private QName name;
public QNameIgnorePrefix(QName name) { this.name = name; }
public String getUri() { return name.getUri(); }
public String getLocal() { return name.getLocal(); }
public int hashCode() { return name.hashCodeIgnorePrefix(); }
public boolean equals(Object o); { return name.equalsIgnorePrefix(o); }
}
--
xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev@ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ or CD-ROM/ISBN 981-02-3594-1
Please note: New list subscriptions now closed in preparation for transfer to OASIS.
|