groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [groovy] 11/18: Minor refactoring: 'while' loop can be replaced with 'foreach'
Date Sun, 03 Nov 2019 15:42:27 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 05431cfeb66b820c15408e2dd2f486aab27156ed
Author: Daniel Sun <sunlan@apache.org>
AuthorDate: Sun Nov 3 22:50:59 2019 +0800

    Minor refactoring: 'while' loop can be replaced with 'foreach'
    
    (cherry picked from commit 7ad8e563ad119efa1a68f5ae23cb533a96219a31)
---
 .../groovy/antlr/java/Java2GroovyProcessor.java      |  5 +----
 .../groovy/antlr/treewalker/CompositeVisitor.java    | 20 ++++++++++++--------
 .../antlr/treewalker/FlatNodeListTraversal.java      |  8 +++-----
 .../org/codehaus/groovy/control/ErrorCollector.java  | 12 +++++-------
 .../java/org/codehaus/groovy/control/Janitor.java    |  6 ++----
 5 files changed, 23 insertions(+), 28 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/antlr/java/Java2GroovyProcessor.java b/src/main/java/org/codehaus/groovy/antlr/java/Java2GroovyProcessor.java
index 689df7e..3f21086 100644
--- a/src/main/java/org/codehaus/groovy/antlr/java/Java2GroovyProcessor.java
+++ b/src/main/java/org/codehaus/groovy/antlr/java/Java2GroovyProcessor.java
@@ -38,16 +38,13 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.PrintStream;
 import java.io.StringReader;
-import java.util.Iterator;
 import java.util.List;
 
 @Deprecated
 public class Java2GroovyProcessor {
 
     public static void processFiles(List<String> fileNames) throws Exception {
-        Iterator i = fileNames.iterator();
-        while (i.hasNext()) {
-            String filename = (String) i.next();
+        for (String filename : fileNames) {
             File f = new File(filename);
             String text = ResourceGroovyMethods.getText(f);
             System.out.println(convert(filename, text, true, true));
diff --git a/src/main/java/org/codehaus/groovy/antlr/treewalker/CompositeVisitor.java b/src/main/java/org/codehaus/groovy/antlr/treewalker/CompositeVisitor.java
index c89b69d..87e242f 100644
--- a/src/main/java/org/codehaus/groovy/antlr/treewalker/CompositeVisitor.java
+++ b/src/main/java/org/codehaus/groovy/antlr/treewalker/CompositeVisitor.java
@@ -60,8 +60,9 @@ public class CompositeVisitor implements Visitor{
     }
 
     public void setUp() {
-        Iterator itr = visitors.iterator();
-        while (itr.hasNext()) {((Visitor)itr.next()).setUp();}
+        for (Object visitor : visitors) {
+            ((Visitor) visitor).setUp();
+        }
     }
 
     public void visitAbstract(GroovySourceAST t, int visit) {
@@ -1160,18 +1161,21 @@ public class CompositeVisitor implements Visitor{
     }
 
     public void tearDown() {
-        Iterator itr = backToFrontVisitors.iterator();
-        while (itr.hasNext()) {((Visitor)itr.next()).tearDown();}
+        for (Object backToFrontVisitor : backToFrontVisitors) {
+            ((Visitor) backToFrontVisitor).tearDown();
+        }
     }
 
     public void push(GroovySourceAST t) {
-        Iterator itr = visitors.iterator();
-        while (itr.hasNext()) {((Visitor)itr.next()).push(t);}
+        for (Object visitor : visitors) {
+            ((Visitor) visitor).push(t);
+        }
     }
     public GroovySourceAST pop() {
         GroovySourceAST lastNodePopped = null;
-        Iterator itr = backToFrontVisitors.iterator();
-        while (itr.hasNext()) {lastNodePopped = (GroovySourceAST) ((Visitor)itr.next()).pop();}
+        for (Object backToFrontVisitor : backToFrontVisitors) {
+            lastNodePopped = (GroovySourceAST) ((Visitor) backToFrontVisitor).pop();
+        }
         return lastNodePopped;
     }
 }
diff --git a/src/main/java/org/codehaus/groovy/antlr/treewalker/FlatNodeListTraversal.java
b/src/main/java/org/codehaus/groovy/antlr/treewalker/FlatNodeListTraversal.java
index d64fa20..14cc8cb 100644
--- a/src/main/java/org/codehaus/groovy/antlr/treewalker/FlatNodeListTraversal.java
+++ b/src/main/java/org/codehaus/groovy/antlr/treewalker/FlatNodeListTraversal.java
@@ -22,7 +22,6 @@ import antlr.collections.AST;
 import org.codehaus.groovy.antlr.AntlrASTProcessor;
 import org.codehaus.groovy.antlr.GroovySourceAST;
 
-import java.util.Iterator;
 import java.util.List;
 
 /**
@@ -44,10 +43,9 @@ public class FlatNodeListTraversal extends TraversalHelper {
         List listOfAllNodesInThisAST = collector.getNodes();
         
         // process each node in turn
-        setUp(node);        
-        Iterator itr = listOfAllNodesInThisAST.iterator();
-        while (itr.hasNext()) {
-            GroovySourceAST currentNode = (GroovySourceAST) itr.next();
+        setUp(node);
+        for (Object o : listOfAllNodesInThisAST) {
+            GroovySourceAST currentNode = (GroovySourceAST) o;
             accept(currentNode);
         }
         tearDown(node);
diff --git a/src/main/java/org/codehaus/groovy/control/ErrorCollector.java b/src/main/java/org/codehaus/groovy/control/ErrorCollector.java
index 67de50a..232d3df 100644
--- a/src/main/java/org/codehaus/groovy/control/ErrorCollector.java
+++ b/src/main/java/org/codehaus/groovy/control/ErrorCollector.java
@@ -28,7 +28,6 @@ import org.codehaus.groovy.syntax.SyntaxException;
 
 import java.io.PrintWriter;
 import java.io.Serializable;
-import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 
@@ -318,15 +317,14 @@ public class ErrorCollector implements Serializable {
 
     private void write(PrintWriter writer, Janitor janitor, List messages, String txt) {
         if (messages==null || messages.isEmpty()) return;
-        Iterator iterator = messages.iterator();
-        while (iterator.hasNext()) {
-            Message message = (Message) iterator.next();
+        for (Object o : messages) {
+            Message message = (Message) o;
             message.write(writer, janitor);
-            
-            if (configuration.getDebug() && (message instanceof SyntaxErrorMessage)){
+
+            if (configuration.getDebug() && (message instanceof SyntaxErrorMessage))
{
                 SyntaxErrorMessage sem = (SyntaxErrorMessage) message;
                 sem.getCause().printStackTrace(writer);
-            } 
+            }
             writer.println();
         }
 
diff --git a/src/main/java/org/codehaus/groovy/control/Janitor.java b/src/main/java/org/codehaus/groovy/control/Janitor.java
index 862bc09..46885ed 100644
--- a/src/main/java/org/codehaus/groovy/control/Janitor.java
+++ b/src/main/java/org/codehaus/groovy/control/Janitor.java
@@ -19,7 +19,6 @@
 package org.codehaus.groovy.control;
 
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.Set;
 
 /**
@@ -34,9 +33,8 @@ public class Janitor implements HasCleanup {
     }
 
     public void cleanup() {
-        Iterator iterator = pending.iterator();
-        while (iterator.hasNext()) {
-            HasCleanup object = (HasCleanup) iterator.next();
+        for (Object o : pending) {
+            HasCleanup object = (HasCleanup) o;
 
             try {
                 object.cleanup();


Mime
View raw message