You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2017/08/01 05:30:40 UTC

groovy git commit: GROOVY-8271: Call iterators hasNext as late as possible where simpler checks can be done first (closes #578)

Repository: groovy
Updated Branches:
  refs/heads/master 13e4a522c -> f35abadad


GROOVY-8271: Call iterators hasNext as late as possible where simpler checks can be done first (closes #578)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/f35abada
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f35abada
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f35abada

Branch: refs/heads/master
Commit: f35abadad0c1d4af3425ba8c341c67a3224131fc
Parents: 13e4a52
Author: Marty Neal <ma...@oracle.com>
Authored: Thu Jul 27 04:52:25 2017 -0700
Committer: paulk <pa...@asert.com.au>
Committed: Tue Aug 1 15:29:10 2017 +1000

----------------------------------------------------------------------
 .../codehaus/groovy/runtime/DefaultGroovyMethods.java  |  4 ++--
 .../groovy/runtime/DefaultGroovyMethodsTest.groovy     | 13 +++++++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/f35abada/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
index 03f061c..dd0ae2c 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -9836,7 +9836,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
         }
 
         public boolean hasNext() {
-            return delegate.hasNext() && num > 0;
+            return num > 0 && delegate.hasNext();
         }
 
         public E next() {
@@ -9911,7 +9911,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport {
      * @since 2.4.0
      */
     public static <T> Collection<T> takeRight(Iterable<T> self, int num) {
-        if (!self.iterator().hasNext() || num <= 0) {
+        if (num <= 0 || !self.iterator().hasNext()) {
             return self instanceof Collection ? createSimilarCollection((Collection<T>) self, 0) : new ArrayList<T>();
         }
         Collection<T> selfCol = self instanceof Collection ? (Collection<T>) self : toList(self);

http://git-wip-us.apache.org/repos/asf/groovy/blob/f35abada/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
index ebea8fd..85cd65b 100644
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
@@ -255,6 +255,19 @@ public class DefaultGroovyMethodsTest extends GroovyTestCase {
         assertEquals(1, iterableAsType[0])
     }
 
+    // GROOVY-8271
+    public void testTake() {
+        int hasNextCount = 0
+        int nextCount = 0
+        def iterator = [
+            hasNext: { -> hasNextCount++; true },
+            next: { -> nextCount++ }
+        ] as Iterator<Integer>
+        iterator.take(3).toList()
+        assertEquals(3, hasNextCount)
+        assertEquals(3, nextCount)
+    }
+
     private static class MyList extends ArrayList {
         public MyList() {}
     }