You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2009/01/02 18:36:23 UTC

svn commit: r730796 - in /xerces/java/trunk/src/org/apache/xerces/stax: XMLEventFactoryImpl.java events/ElementImpl.java events/EndElementImpl.java events/StartElementImpl.java

Author: mrglavas
Date: Fri Jan  2 09:36:23 2009
New Revision: 730796

URL: http://svn.apache.org/viewvc?rev=730796&view=rev
Log:
Reducing object allocation and eliminating mutation methods.

Modified:
    xerces/java/trunk/src/org/apache/xerces/stax/XMLEventFactoryImpl.java
    xerces/java/trunk/src/org/apache/xerces/stax/events/ElementImpl.java
    xerces/java/trunk/src/org/apache/xerces/stax/events/EndElementImpl.java
    xerces/java/trunk/src/org/apache/xerces/stax/events/StartElementImpl.java

Modified: xerces/java/trunk/src/org/apache/xerces/stax/XMLEventFactoryImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/stax/XMLEventFactoryImpl.java?rev=730796&r1=730795&r2=730796&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/stax/XMLEventFactoryImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/stax/XMLEventFactoryImpl.java Fri Jan  2 09:36:23 2009
@@ -111,28 +111,11 @@
     
     private StartElement createStartElement(QName name, Iterator attributes,
             Iterator namespaces, NamespaceContext context) {
-        StartElementImpl start = new StartElementImpl(name, context, fLocation);
-        if (attributes != null) {
-            while (attributes.hasNext()) {
-                start.addAttribute((Attribute) attributes.next());
-            }
-        }
-        if (namespaces != null) {
-            while (namespaces.hasNext()) {
-                start.addNamespace((Namespace) namespaces.next());
-            }
-        }
-        return start;
+        return new StartElementImpl(name, attributes, namespaces, context, fLocation);
     }
 
     public EndElement createEndElement(QName name, Iterator namespaces) {
-        EndElementImpl end = new EndElementImpl(name, fLocation);
-        if (namespaces != null) {
-            while (namespaces.hasNext()) {
-                end.addNamespace((Namespace) namespaces.next());
-            }
-        }
-        return end;
+        return new EndElementImpl(name, namespaces, fLocation);
     }
 
     public EndElement createEndElement(String prefix, String namespaceUri,

Modified: xerces/java/trunk/src/org/apache/xerces/stax/events/ElementImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/stax/events/ElementImpl.java?rev=730796&r1=730795&r2=730796&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/stax/events/ElementImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/stax/events/ElementImpl.java Fri Jan  2 09:36:23 2009
@@ -18,6 +18,7 @@
 package org.apache.xerces.stax.events;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
 
@@ -43,14 +44,25 @@
     /**
      * Namespaces declared in the current scope.
      */
-    private final List fNamespaces = new ArrayList();
+    private final List fNamespaces;
     
     /**
      * Constructor.
      */
-    ElementImpl(final QName name, final boolean isStartElement, final Location location) {
+    ElementImpl(final QName name, final boolean isStartElement, Iterator namespaces, final Location location) {
         super(isStartElement ? START_ELEMENT : END_ELEMENT, location);
         fName = name;
+        if (namespaces != null && namespaces.hasNext()) {
+            fNamespaces = new ArrayList();
+            do {
+                Namespace ns = (Namespace) namespaces.next();
+                fNamespaces.add(ns);
+            }
+            while (namespaces.hasNext());
+        }
+        else {
+            fNamespaces = Collections.EMPTY_LIST;
+        }
     }
     
     /**
@@ -69,10 +81,6 @@
         return createImmutableIterator(fNamespaces.iterator());
     }
     
-    public final void addNamespace(final Namespace namespace) {
-        fNamespaces.add(namespace);
-    }
-    
     static Iterator createImmutableIterator(Iterator iter) {
         return new NoRemoveIterator(iter);
     }

Modified: xerces/java/trunk/src/org/apache/xerces/stax/events/EndElementImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/stax/events/EndElementImpl.java?rev=730796&r1=730795&r2=730796&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/stax/events/EndElementImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/stax/events/EndElementImpl.java Fri Jan  2 09:36:23 2009
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.io.Writer;
+import java.util.Iterator;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.Location;
@@ -37,8 +38,8 @@
     /**
      * @param location The location object for this event.
      */
-    public EndElementImpl(final QName name, final Location location) {
-        super(name, false, location);
+    public EndElementImpl(final QName name, final Iterator namespaces, final Location location) {
+        super(name, false, namespaces, location);
     }
 
     public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {

Modified: xerces/java/trunk/src/org/apache/xerces/stax/events/StartElementImpl.java
URL: http://svn.apache.org/viewvc/xerces/java/trunk/src/org/apache/xerces/stax/events/StartElementImpl.java?rev=730796&r1=730795&r2=730796&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/stax/events/StartElementImpl.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/stax/events/StartElementImpl.java Fri Jan  2 09:36:23 2009
@@ -19,6 +19,7 @@
 
 import java.io.IOException;
 import java.io.Writer;
+import java.util.Collections;
 import java.util.Comparator;
 import java.util.Iterator;
 import java.util.Map;
@@ -42,24 +43,37 @@
  * @version $Id$
  */
 public final class StartElementImpl extends ElementImpl implements StartElement {
-
-    private final Map fAttributes = new TreeMap(new Comparator(){
+    
+    private static final Comparator QNAME_COMPARATOR = new Comparator() {
         public int compare(Object o1, Object o2) {
-            if(o1.equals(o2)) {
+            if (o1.equals(o2)) {
                 return 0;
             }
-            QName name1 = (QName)o1;
-            QName name2 = (QName)o2;
+            QName name1 = (QName) o1;
+            QName name2 = (QName) o2;
             return name1.toString().compareTo(name2.toString());
-        }});
+        }};
+
+    private final Map fAttributes;
     private final NamespaceContext fNamespaceContext;
 
     /**
      * @param location
      * @param schemaType
      */
-    public StartElementImpl(final QName name, final NamespaceContext namespaceContext, final Location location) {
-        super(name, true, location);
+    public StartElementImpl(final QName name, final Iterator attributes, final Iterator namespaces, final NamespaceContext namespaceContext, final Location location) {
+        super(name, true, namespaces, location);
+        if (attributes != null && attributes.hasNext()) {
+            fAttributes = new TreeMap(QNAME_COMPARATOR);
+            do {
+                Attribute attr = (Attribute) attributes.next();
+                fAttributes.put(attr.getName(), attr);
+            }
+            while (attributes.hasNext());
+        }
+        else {
+            fAttributes = Collections.EMPTY_MAP;
+        }
         fNamespaceContext = (namespaceContext != null) ? namespaceContext : DefaultNamespaceContext.getInstance();
     }
 
@@ -90,15 +104,11 @@
     public String getNamespaceURI(final String prefix) {
         return fNamespaceContext.getNamespaceURI(prefix);
     }
-
-    public void addAttribute(final Attribute attribute) {
-        fAttributes.put(attribute.getName(), attribute);
-    }
     
     public void writeAsEncodedUnicode(Writer writer) throws XMLStreamException {
         try {
             // Write start tag.
-            writer.write("<");
+            writer.write('<');
             QName name = getName();
             String prefix = name.getPrefix();
             if (prefix != null && prefix.length() > 0) {



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org