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-5936: remove superfluous toList calls for DGM unique(Iterator, ...) methods
Date Thu, 04 Aug 2016 05:37:16 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 4db71e697 -> 286b36bbf


GROOVY-5936: remove superfluous toList calls for DGM unique(Iterator, ...) methods


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

Branch: refs/heads/master
Commit: 286b36bbfe2d6e18d201095fc76b57bb4d2b9694
Parents: 4db71e6
Author: paulk <paulk@asert.com.au>
Authored: Thu Aug 4 15:37:07 2016 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Thu Aug 4 15:37:07 2016 +1000

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java    | 47 +++++++++++++++-----
 1 file changed, 35 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/286b36bb/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 7e89c44..a971c62 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -1017,7 +1017,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
      * @since 1.5.5
      */
     public static <T> Iterator<T> unique(Iterator<T> self) {
-        return toList((Iterable<T>) unique(toList(self))).listIterator();
+        return uniqueItems(new IteratorIterableAdapter<T>(self)).listIterator();
     }
 
     /**
@@ -1068,6 +1068,15 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
      * @since 1.8.1
      */
     public static <T> Collection<T> unique(Collection<T> self, boolean
mutate) {
+        List<T> answer = uniqueItems(self);
+        if (mutate) {
+            self.clear();
+            self.addAll(answer);
+        }
+        return mutate ? self : answer ;
+    }
+
+    private static <T> List<T> uniqueItems(Iterable<T> self) {
         List<T> answer = new ArrayList<T>();
         for (T t : self) {
             boolean duplicated = false;
@@ -1080,11 +1089,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
             if (!duplicated)
                 answer.add(t);
         }
-        if (mutate) {
-            self.clear();
-            self.addAll(answer);
-        }
-        return mutate ? self : answer ;
+        return answer;
     }
 
     /**
@@ -1275,7 +1280,20 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
      * @since 1.5.5
      */
     public static <T> Iterator<T> unique(Iterator<T> self, Comparator<T>
comparator) {
-        return toList((Iterable<T>) unique(toList(self), false, comparator)).listIterator();
+        return uniqueItems(new IteratorIterableAdapter<T>(self), comparator).listIterator();
+    }
+
+    private static class IteratorIterableAdapter<T> implements Iterable<T> {
+        private final Iterator<T> self;
+
+        private IteratorIterableAdapter(Iterator<T> self) {
+            this.self = self;
+        }
+
+        @Override
+        public Iterator<T> iterator() {
+            return self;
+        }
     }
 
     /**
@@ -1428,6 +1446,15 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
      * @since 1.8.1
      */
     public static <T> Collection<T> unique(Collection<T> self, boolean
mutate, Comparator<T> comparator) {
+        List<T> answer = uniqueItems(self, comparator);
+        if (mutate) {
+            self.clear();
+            self.addAll(answer);
+        }
+        return mutate ? self : answer;
+    }
+
+    private static <T> List<T> uniqueItems(Iterable<T> self, Comparator<T>
comparator) {
         List<T> answer = new ArrayList<T>();
         for (T t : self) {
             boolean duplicated = false;
@@ -1440,11 +1467,7 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
             if (!duplicated)
                 answer.add(t);
         }
-        if (mutate) {
-            self.clear();
-            self.addAll(answer);
-        }
-        return mutate ? self : answer;
+        return answer;
     }
 
     /**


Mime
View raw message