Minor performance improvements:

Improving the performance of toString() by caching the result.

Improving the performance of equals() by first checking if the QName is the
same object and then by checking whether the local names are equal before
looking at the namespace URI. Since it's more likely that the namespaces
will be equal, we can return false faster when the local names are different.


git-svn-id: https://svn.apache.org/repos/asf/xml/commons/trunk@226225 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
mrglavas 2005-06-04 04:12:57 +00:00
parent 378334a0a8
commit 8dcb34f116

View File

@ -82,6 +82,11 @@ public class QName implements Serializable {
*/ */
private final String prefix; private final String prefix;
/**
* <p><code>String</code> representation of this <code>QName</code>.</p>
*/
private transient String qNameAsString;
/** /**
* <p><code>QName</code> constructor specifying the Namespace URI * <p><code>QName</code> constructor specifying the Namespace URI
* and local part.</p> * and local part.</p>
@ -281,14 +286,16 @@ public class QName implements Serializable {
* equal to this <code>QName</code> else <code>false</code> * equal to this <code>QName</code> else <code>false</code>
*/ */
public final boolean equals(Object objectToTest) { public final boolean equals(Object objectToTest) {
if (objectToTest == null || !(objectToTest instanceof QName)) { // Is this the same object?
return false; if (objectToTest == this) {
return true;
} }
// Is this a QName?
QName qName = (QName) objectToTest; if (objectToTest instanceof QName) {
QName qName = (QName) objectToTest;
return namespaceURI.equals(qName.namespaceURI) return localPart.equals(qName.localPart) && namespaceURI.equals(qName.namespaceURI);
&& localPart.equals(qName.localPart); }
return false;
} }
/** /**
@ -331,11 +338,23 @@ public class QName implements Serializable {
* @return <code>String</code> representation of this <code>QName</code> * @return <code>String</code> representation of this <code>QName</code>
*/ */
public String toString() { public String toString() {
if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) { String _qNameAsString = qNameAsString;
return localPart; if (_qNameAsString == null) {
} else { final int nsLength = namespaceURI.length();
return "{" + namespaceURI + "}" + localPart; if (nsLength == 0) {
_qNameAsString = localPart;
}
else {
StringBuffer buffer = new StringBuffer(nsLength + localPart.length() + 2);
buffer.append('{');
buffer.append(namespaceURI);
buffer.append('}');
buffer.append(localPart);
_qNameAsString = buffer.toString();
}
qNameAsString = _qNameAsString;
} }
return _qNameAsString;
} }
/** /**