From 8dcb34f1165c65a07eaa0d8222327c0399993f34 Mon Sep 17 00:00:00 2001 From: mrglavas Date: Sat, 4 Jun 2005 04:12:57 +0000 Subject: [PATCH] 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 --- .../src/javax/xml/namespace/QName.java | 41 ++++++++++++++----- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/java/external/src/javax/xml/namespace/QName.java b/java/external/src/javax/xml/namespace/QName.java index 5e87f4e..463c100 100644 --- a/java/external/src/javax/xml/namespace/QName.java +++ b/java/external/src/javax/xml/namespace/QName.java @@ -81,6 +81,11 @@ public class QName implements Serializable { *

prefix of this QName.

*/ private final String prefix; + + /** + *

String representation of this QName.

+ */ + private transient String qNameAsString; /** *

QName constructor specifying the Namespace URI @@ -281,14 +286,16 @@ public class QName implements Serializable { * equal to this QName else false */ 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 String representation of this QName */ 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; } /**