You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2015/06/19 20:54:01 UTC

svn commit: r1686482 - in /webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core: AbstractAttributeIterator.java AttributeIterator.java AttributesByTypeIterator.java CoreAttributeSupport.aj CoreElementSupport.aj

Author: veithen
Date: Fri Jun 19 18:54:01 2015
New Revision: 1686482

URL: http://svn.apache.org/r1686482
Log:
- Fix an issue with the code that removes attributes.
- Simplify the class hierarchy for attribute iterators.
- Don't create an iterator if there are no attributes to return.

Added:
    webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AttributeIterator.java
      - copied, changed from r1686327, webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AbstractAttributeIterator.java
Removed:
    webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AbstractAttributeIterator.java
    webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AttributesByTypeIterator.java
Modified:
    webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreAttributeSupport.aj
    webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreElementSupport.aj

Copied: webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AttributeIterator.java (from r1686327, webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AbstractAttributeIterator.java)
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AttributeIterator.java?p2=webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AttributeIterator.java&p1=webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AbstractAttributeIterator.java&r1=1686327&r2=1686482&rev=1686482&view=diff
==============================================================================
--- webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AbstractAttributeIterator.java (original)
+++ webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/AttributeIterator.java Fri Jun 19 18:54:01 2015
@@ -18,37 +18,44 @@
  */
 package org.apache.axiom.core;
 
+import java.util.Collections;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
 
-abstract class AbstractAttributeIterator<T extends CoreAttribute,S> implements Iterator<S> {
-    private final CoreElement element;
+final class AttributeIterator<T extends CoreAttribute,S> implements Iterator<S> {
     private final Class<T> type;
     private final Mapper<T,S> mapper;
     private CoreAttribute currentAttribute;
     private CoreAttribute nextAttribute;
-    private boolean hasNextCalled;
+    private boolean nextAttributeSet;
     
-    AbstractAttributeIterator(CoreElement element, Class<T> type, Mapper<T,S> mapper) {
-        this.element = element;
+    private AttributeIterator(CoreAttribute firstAttribute, Class<T> type, Mapper<T,S> mapper) {
         this.type = type;
         this.mapper = mapper;
+        nextAttribute = firstAttribute;
+        nextAttributeSet = true;
+    }
+    
+    static <T extends CoreAttribute,S> Iterator<S> create(CoreElement element, Class<T> type, Mapper<T,S> mapper) {
+        CoreAttribute attribute = element.coreGetFirstAttribute();
+        while (attribute != null && !type.isInstance(attribute)) {
+            attribute = attribute.coreGetNextAttribute();
+        }
+        if (attribute == null) {
+            return Collections.<S>emptyList().iterator();
+        } else {
+            return new AttributeIterator<T,S>(attribute, type, mapper);
+        }
     }
     
-    protected abstract boolean matches(T attribute);
-
     public final boolean hasNext() {
-        if (!hasNextCalled) {
+        if (!nextAttributeSet) {
             CoreAttribute attribute = currentAttribute;
             do {
-                if (attribute == null) {
-                    attribute = element.coreGetFirstAttribute();
-                } else {
-                    attribute = attribute.coreGetNextAttribute();
-                }
-            } while (attribute != null && (!type.isInstance(attribute) || !matches(type.cast(attribute))));
+                attribute = attribute.coreGetNextAttribute();
+            } while (attribute != null && !type.isInstance(attribute));
             nextAttribute = attribute;
-            hasNextCalled = true;
+            nextAttributeSet = true;
         }
         return nextAttribute != null;
     }
@@ -58,7 +65,7 @@ abstract class AbstractAttributeIterator
             CoreAttribute attribute = nextAttribute;
             currentAttribute = attribute;
             nextAttribute = null;
-            hasNextCalled = false;
+            nextAttributeSet = false;
             return mapper.map(type.cast(attribute));
         } else {
             throw new NoSuchElementException();
@@ -69,6 +76,8 @@ abstract class AbstractAttributeIterator
         if (currentAttribute == null) {
             throw new IllegalStateException();
         } else {
+            // Ensure that the next attribute is known before we remove the current one.
+            hasNext();
             currentAttribute.coreRemove();
             currentAttribute = null;
         }

Modified: webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreAttributeSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreAttributeSupport.aj?rev=1686482&r1=1686481&r2=1686482&view=diff
==============================================================================
--- webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreAttributeSupport.aj (original)
+++ webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreAttributeSupport.aj Fri Jun 19 18:54:01 2015
@@ -114,6 +114,7 @@ public aspect CoreAttributeSupport {
             } else {
                 previousAttr.nextAttribute = nextAttribute;
             }
+            nextAttribute = null;
             return true;
         } else {
             if (newOwnerDocument) {

Modified: webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreElementSupport.aj
URL: http://svn.apache.org/viewvc/webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreElementSupport.aj?rev=1686482&r1=1686481&r2=1686482&view=diff
==============================================================================
--- webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreElementSupport.aj (original)
+++ webservices/axiom/branches/attrs-aspects/aspects/core-aspects/src/main/java/org/apache/axiom/core/CoreElementSupport.aj Fri Jun 19 18:54:01 2015
@@ -174,8 +174,7 @@ public aspect CoreElementSupport {
     }
 
     public final <T extends CoreAttribute,S> Iterator<S> CoreElement.coreGetAttributesByType(Class<T> type, Mapper<T,S> mapper) {
-        // TODO: if we know that there are no attributes, don't create a new iterator, but return a constant
-        return new AttributesByTypeIterator<T,S>(this, type, mapper);
+        return AttributeIterator.create(this, type, mapper);
     }
 
     public abstract String CoreElement.getImplicitNamespaceURI(String prefix);