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:
parent
378334a0a8
commit
8dcb34f116
41
java/external/src/javax/xml/namespace/QName.java
vendored
41
java/external/src/javax/xml/namespace/QName.java
vendored
@ -81,6 +81,11 @@ public class QName implements Serializable {
|
||||
* <p>prefix of this <code>QName</code>.</p>
|
||||
*/
|
||||
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
|
||||
@ -281,14 +286,16 @@ public class QName implements Serializable {
|
||||
* equal to this <code>QName</code> else <code>false</code>
|
||||
*/
|
||||
public final boolean equals(Object objectToTest) {
|
||||
if (objectToTest == null || !(objectToTest instanceof QName)) {
|
||||
return false;
|
||||
// Is this the same object?
|
||||
if (objectToTest == this) {
|
||||
return true;
|
||||
}
|
||||
|
||||
QName qName = (QName) objectToTest;
|
||||
|
||||
return namespaceURI.equals(qName.namespaceURI)
|
||||
&& localPart.equals(qName.localPart);
|
||||
// Is this a QName?
|
||||
if (objectToTest instanceof QName) {
|
||||
QName qName = (QName) objectToTest;
|
||||
return localPart.equals(qName.localPart) && namespaceURI.equals(qName.namespaceURI);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -331,11 +338,23 @@ public class QName implements Serializable {
|
||||
* @return <code>String</code> representation of this <code>QName</code>
|
||||
*/
|
||||
public String toString() {
|
||||
if (namespaceURI.equals(XMLConstants.NULL_NS_URI)) {
|
||||
return localPart;
|
||||
} else {
|
||||
return "{" + namespaceURI + "}" + localPart;
|
||||
String _qNameAsString = qNameAsString;
|
||||
if (_qNameAsString == null) {
|
||||
final int nsLength = namespaceURI.length();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user