You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/04/11 13:24:32 UTC

[groovy] branch master updated: Trivial refactoring: Replace with enhanced 'for' loop

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new af16422  Trivial refactoring: Replace with enhanced 'for' loop
af16422 is described below

commit af164227c8f7f006445beb049c3d314179318601
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Apr 11 21:24:19 2020 +0800

    Trivial refactoring: Replace with enhanced 'for' loop
---
 .../src/main/java/groovy/xml/DOMBuilder.java          |  5 ++---
 .../java/groovy/xml/slurpersupport/Attributes.java    |  5 ++---
 .../java/groovy/xml/slurpersupport/GPathResult.java   | 10 ++++------
 .../java/groovy/xml/slurpersupport/NodeChildren.java  | 19 +++++++------------
 4 files changed, 15 insertions(+), 24 deletions(-)

diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java b/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java
index 2823cb5..05626b6 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/DOMBuilder.java
@@ -33,7 +33,6 @@ import javax.xml.parsers.ParserConfigurationException;
 import java.io.IOException;
 import java.io.Reader;
 import java.io.StringReader;
-import java.util.Iterator;
 import java.util.Map;
 
 /**
@@ -190,8 +189,8 @@ public class DOMBuilder extends BuilderSupport {
 
     protected Object createNode(Object name, Map attributes) {
         Element element = (Element) createNode(name);
-        for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
-            Map.Entry entry = (Map.Entry) iter.next();
+        for (Object o : attributes.entrySet()) {
+            Map.Entry entry = (Map.Entry) o;
             String attrName = entry.getKey().toString();
             Object value = entry.getValue();
             if ("xmlns".equals(attrName)) {
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/Attributes.java b/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/Attributes.java
index 182e2ce..e21550d 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/Attributes.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/Attributes.java
@@ -107,9 +107,8 @@ public class Attributes extends NodeChildren {
 
     public String text() {
         final StringBuilder sb = new StringBuilder();
-        final Iterator iter = iterator();
-        while (iter.hasNext()) {
-            sb.append(iter.next());
+        for (Object o : this) {
+            sb.append(o);
         }
         return sb.toString();
     }
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/GPathResult.java b/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/GPathResult.java
index d733797..da1b3d6 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/GPathResult.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/GPathResult.java
@@ -147,10 +147,9 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
     public void setProperty(final String property, final Object newValue) {
         if (property.startsWith("@")) {
             if (newValue instanceof String || newValue instanceof GString) {
-                final Iterator iter = iterator();
 
-                while (iter.hasNext()) {
-                    final NodeChild child = (NodeChild) iter.next();
+                for (Object o : this) {
+                    final NodeChild child = (NodeChild) o;
 
                     child.attributes().put(property.substring(1), newValue);
                 }
@@ -572,9 +571,8 @@ public abstract class GPathResult extends GroovyObjectSupport implements Writabl
                         List nextLevel = new ArrayList();
                         for (Object child : children) {
                             GPathResult next = (GPathResult) child;
-                            Iterator iterator = next.iterator();
-                            while (iterator.hasNext()) {
-                                nextLevel.add(iterator.next());
+                            for (Object o : next) {
+                                nextLevel.add(o);
                             }
                         }
                         this.iter = nextLevel.iterator();
diff --git a/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/NodeChildren.java b/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/NodeChildren.java
index ba760dc..ce22c18 100644
--- a/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/NodeChildren.java
+++ b/subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/NodeChildren.java
@@ -171,9 +171,7 @@ public class NodeChildren extends GPathResult {
     }
 
     public GPathResult find(final Closure closure) {
-        final Iterator iter = iterator();
-        while (iter.hasNext()) {
-            final Object node = iter.next();
+        for (Object node : this) {
             if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{node}))) {
                 return (GPathResult) node;
             }
@@ -209,25 +207,22 @@ public class NodeChildren extends GPathResult {
     }
 
     protected void replaceNode(final Closure newValue) {
-        final Iterator iter = iterator();
-        while (iter.hasNext()) {
-            final NodeChild result = (NodeChild) iter.next();
+        for (Object o : this) {
+            final NodeChild result = (NodeChild) o;
             result.replaceNode(newValue);
         }
     }
 
     protected void replaceBody(final Object newValue) {
-        final Iterator iter = iterator();
-        while (iter.hasNext()) {
-            final NodeChild result = (NodeChild) iter.next();
+        for (Object o : this) {
+            final NodeChild result = (NodeChild) o;
             result.replaceBody(newValue);
         }
     }
 
     protected void appendNode(final Object newValue) {
-        final Iterator iter = iterator();
-        while (iter.hasNext()) {
-            final NodeChild result = (NodeChild) iter.next();
+        for (Object o : this) {
+            final NodeChild result = (NodeChild) o;
             result.appendNode(newValue);
         }
     }