groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8271: Call iterators hasNext as late as possible where simpler checks can be done first (closes #578)
Date Tue, 01 Aug 2017 05:31:37 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X a983b9a35 -> f08026b34


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/f08026b3
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/f08026b3
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/f08026b3

Branch: refs/heads/GROOVY_2_5_X
Commit: f08026b34e711838b2378e6b706859c248f9d203
Parents: a983b9a
Author: Marty Neal <marty.neal@oracle.com>
Authored: Thu Jul 27 04:52:25 2017 -0700
Committer: paulk <paulk@asert.com.au>
Committed: Tue Aug 1 15:31:15 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/f08026b3/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 2581b46..a96c524 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/f08026b3/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() {}
     }


Mime
View raw message