Author: veithen
Date: Sun Oct 16 16:00:26 2016
New Revision: 1765169

URL: http://svn.apache.org/viewvc?rev=1765169&view=rev
Log:
Cache QName instances.

Added:
    
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCache.java
   (with props)
    
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCacheEntry.java
   (with props)
    
webservices/axiom/trunk/components/xml-utils/src/test/java/org/apache/axiom/util/xml/QNameCacheTest.java
   (with props)
Modified:
    webservices/axiom/trunk/aspects/core-aspects/pom.xml
    
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/stax/pull/StAXPivot.java
    
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomElementSupport.aj
    
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomNamedInformationItemSupport.aj

Modified: webservices/axiom/trunk/aspects/core-aspects/pom.xml
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/pom.xml?rev=1765169&r1=1765168&r2=1765169&view=diff
==============================================================================
--- webservices/axiom/trunk/aspects/core-aspects/pom.xml (original)
+++ webservices/axiom/trunk/aspects/core-aspects/pom.xml Sun Oct 16 16:00:26 
2016
@@ -42,6 +42,11 @@
             <artifactId>commons-logging</artifactId>
         </dependency>
         <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>xml-utils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>

Modified: 
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/stax/pull/StAXPivot.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/stax/pull/StAXPivot.java?rev=1765169&r1=1765168&r2=1765169&view=diff
==============================================================================
--- 
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/stax/pull/StAXPivot.java
 (original)
+++ 
webservices/axiom/trunk/aspects/core-aspects/src/main/java/org/apache/axiom/core/stream/stax/pull/StAXPivot.java
 Sun Oct 16 16:00:26 2016
@@ -36,6 +36,7 @@ import org.apache.axiom.core.stream.XmlH
 import org.apache.axiom.core.stream.XmlReader;
 import org.apache.axiom.core.stream.stax.StAXExceptionUtil;
 import org.apache.axiom.core.stream.util.CharacterDataAccumulator;
+import org.apache.axiom.util.xml.QNameCache;
 
 public final class StAXPivot implements InternalXMLStreamReader, XmlHandler {
     private class NamespaceContextImpl implements NamespaceContext {
@@ -657,8 +658,7 @@ public final class StAXPivot implements
     @Override
     public QName getAttributeName(int index) {
         if (eventType == START_ELEMENT) {
-            // TODO: optimize this
-            return new QName(attributeStack[5*index], 
attributeStack[5*index+1], attributeStack[5*index+2]);
+            return QNameCache.getQName(attributeStack[5*index], 
attributeStack[5*index+1], attributeStack[5*index+2]);
         } else {
             throw new IllegalStateException();
         }
@@ -869,8 +869,7 @@ public final class StAXPivot implements
         switch (eventType) {
             case START_ELEMENT:
             case END_ELEMENT:
-                // TODO: optimize this
-                return new QName(elementStack[3*depth], 
elementStack[3*depth+1], elementStack[3*depth+2]);
+                return QNameCache.getQName(elementStack[3*depth], 
elementStack[3*depth+1], elementStack[3*depth+2]);
             default:
                 throw new IllegalStateException();
         }

Modified: 
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomElementSupport.aj
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomElementSupport.aj?rev=1765169&r1=1765168&r2=1765169&view=diff
==============================================================================
--- 
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomElementSupport.aj
 (original)
+++ 
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomElementSupport.aj
 Sun Oct 16 16:00:26 2016
@@ -67,6 +67,7 @@ import org.apache.axiom.om.impl.intf.Seq
 import org.apache.axiom.util.namespace.MapBasedNamespaceContext;
 import org.apache.axiom.util.stax.XMLStreamIOException;
 import org.apache.axiom.util.stax.XMLStreamReaderUtils;
+import org.apache.axiom.util.xml.QNameCache;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -138,11 +139,11 @@ public aspect AxiomElementSupport {
         int idx = qname.indexOf(':');
         if (idx == -1) {
             OMNamespace ns = getDefaultNamespace();
-            return ns == null ? new QName(qname) : new 
QName(ns.getNamespaceURI(), qname, "");
+            return QNameCache.getQName(ns == null ? "" : ns.getNamespaceURI(), 
qname);
         } else {
             String prefix = qname.substring(0, idx);
             OMNamespace ns = findNamespace(null, prefix);
-            return ns == null ? null : new QName(ns.getNamespaceURI(), 
qname.substring(idx+1), prefix);
+            return ns == null ? null : 
QNameCache.getQName(ns.getNamespaceURI(), qname.substring(idx+1), prefix);
         }
     }
 

Modified: 
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomNamedInformationItemSupport.aj
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomNamedInformationItemSupport.aj?rev=1765169&r1=1765168&r2=1765169&view=diff
==============================================================================
--- 
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomNamedInformationItemSupport.aj
 (original)
+++ 
webservices/axiom/trunk/aspects/om-aspects/src/main/java/org/apache/axiom/om/impl/mixin/AxiomNamedInformationItemSupport.aj
 Sun Oct 16 16:00:26 2016
@@ -28,6 +28,7 @@ import org.apache.axiom.om.impl.common.b
 import org.apache.axiom.om.impl.intf.AxiomElement;
 import org.apache.axiom.om.impl.intf.AxiomNamedInformationItem;
 import org.apache.axiom.om.impl.intf.AxiomSourcedElement;
+import org.apache.axiom.util.xml.QNameCache;
 
 public aspect AxiomNamedInformationItemSupport {
     /**
@@ -47,7 +48,6 @@ public aspect AxiomNamedInformationItemS
     private OMNamespace AxiomNamedInformationItem.namespace;
     
     private String AxiomNamedInformationItem.localName;
-    private QName AxiomNamedInformationItem.qName;
     
     public final void AxiomNamedInformationItem.initName(String namespaceURI, 
String localName, String prefix, Object namespaceHelper) {
         this.localName = localName;
@@ -61,7 +61,6 @@ public aspect AxiomNamedInformationItemS
      */
     public final void 
AxiomNamedInformationItem.internalSetNamespace(OMNamespace namespace) {
         this.namespace = namespace;
-        qName = null;
     }
 
     public final String AxiomNamedInformationItem.internalGetLocalName() {
@@ -85,7 +84,6 @@ public aspect AxiomNamedInformationItemS
     public final void AxiomNamedInformationItem.setLocalName(String localName) 
{
         beforeSetLocalName();
         this.localName = localName;
-        qName = null;
     }
 
     public QName AxiomNamedInformationItem.getQName() {
@@ -93,16 +91,10 @@ public aspect AxiomNamedInformationItemS
     }
     
     public final QName AxiomNamedInformationItem.defaultGetQName() {
-        if (qName != null) {
-            return qName;
-        }
-
-        if (namespace != null) {
-            qName = new QName(namespace.getNamespaceURI(), localName, 
namespace.getPrefix());
-        } else {
-            qName = new QName(localName);
-        }
-        return qName;
+        return QNameCache.getQName(
+                namespace == null ? "" : namespace.getNamespaceURI(),
+                localName,
+                namespace == null ? "" : namespace.getPrefix());
     }
     
     public final boolean AxiomNamedInformationItem.hasName(QName name) {
@@ -128,8 +120,6 @@ public aspect AxiomNamedInformationItemS
     public final void AxiomNamedInformationItem.coreSetName(String 
namespaceURI, String localName, String prefix) {
         this.localName = localName;
         namespace = namespaceURI.length() == 0 && prefix.length() == 0 ? null 
: new OMNamespaceImpl(namespaceURI, prefix);
-        // TODO: need unit test to assert this
-        qName = null;
     }
 
     public final void AxiomNamedInformationItem.initName(CoreNamedNode other) {
@@ -137,11 +127,9 @@ public aspect AxiomNamedInformationItemS
         if (o instanceof AxiomSourcedElement && 
((AxiomElement)this).isExpanded()) {
             localName = o.coreGetLocalName();
             namespace = o.getNamespace();
-            qName = o.getQName();
         } else {
             localName = o.localName;
             namespace = o.namespace;
-            qName = o.qName;
         }
     }
     

Added: 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCache.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCache.java?rev=1765169&view=auto
==============================================================================
--- 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCache.java
 (added)
+++ 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCache.java
 Sun Oct 16 16:00:26 2016
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.util.xml;
+
+import javax.xml.namespace.QName;
+
+public final class QNameCache {
+    private static final QNameCacheEntry[] cache;
+    
+    static {
+        cache = new QNameCacheEntry[1024];
+        for (int i=0; i<cache.length; i++) {
+            cache[i] = new QNameCacheEntry();
+        }
+    }
+
+    private QNameCache() {}
+
+    public static QName getQName(String namespaceURI, String localPart, String 
prefix) {
+        int index = (namespaceURI.hashCode() ^ localPart.hashCode() ^ 
prefix.hashCode()) & (cache.length-1);
+        QNameCacheEntry entry = cache[index];
+        QName qname = entry.get();
+        if (qname == null
+                || !qname.getNamespaceURI().equals(namespaceURI)
+                || !qname.getLocalPart().equals(localPart)
+                || !qname.getPrefix().equals(prefix)) {
+            qname = new QName(namespaceURI, localPart, prefix);
+            entry.set(qname);
+        }
+        return qname;
+    }
+
+    public static QName getQName(String namespaceURI, String localPart) {
+        return getQName(namespaceURI, localPart, "");
+    }
+}

Propchange: 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCache.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCacheEntry.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCacheEntry.java?rev=1765169&view=auto
==============================================================================
--- 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCacheEntry.java
 (added)
+++ 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCacheEntry.java
 Sun Oct 16 16:00:26 2016
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.util.xml;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.xml.namespace.QName;
+
+@SuppressWarnings("serial")
+final class QNameCacheEntry extends AtomicReference<QName> {
+    
+}
\ No newline at end of file

Propchange: 
webservices/axiom/trunk/components/xml-utils/src/main/java/org/apache/axiom/util/xml/QNameCacheEntry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
webservices/axiom/trunk/components/xml-utils/src/test/java/org/apache/axiom/util/xml/QNameCacheTest.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/components/xml-utils/src/test/java/org/apache/axiom/util/xml/QNameCacheTest.java?rev=1765169&view=auto
==============================================================================
--- 
webservices/axiom/trunk/components/xml-utils/src/test/java/org/apache/axiom/util/xml/QNameCacheTest.java
 (added)
+++ 
webservices/axiom/trunk/components/xml-utils/src/test/java/org/apache/axiom/util/xml/QNameCacheTest.java
 Sun Oct 16 16:00:26 2016
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.axiom.util.xml;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import javax.xml.namespace.QName;
+
+import org.junit.Test;
+
+public class QNameCacheTest {
+    @Test
+    public void testGetQName() {
+        QName qname = QNameCache.getQName("urn:ns1", "somename", "ns1");
+        assertThat(qname.getNamespaceURI()).isEqualTo("urn:ns1");
+        assertThat(qname.getLocalPart()).isEqualTo("somename");
+        assertThat(qname.getPrefix()).isEqualTo("ns1");
+    }
+
+    @Test
+    public void testCached() {
+        QName[] qnames = new QName[2];
+        for (int i=0; i<2; i++) {
+            qnames[i] = QNameCache.getQName("urn:test", "test", "p");
+        }
+        assertThat(qnames[1]).isSameAs(qnames[0]);
+    }
+
+    @Test
+    public void testPrefixIsRelevant() {
+        QName qname1 = QNameCache.getQName("urn:ns2", "foo", "p");
+        QName qname2 = QNameCache.getQName("urn:ns2", "foo", "");
+        assertThat(qname2).isNotSameAs(qname1);
+    }
+}

Propchange: 
webservices/axiom/trunk/components/xml-utils/src/test/java/org/apache/axiom/util/xml/QNameCacheTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to