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-7654: Add DefaultGroovyMethods.asType(Iterable, Class) and delegate to Collection variant only if dealing with a Collection (clses #546)
Date Tue, 13 Jun 2017 05:07:20 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 6b2051ea5 -> 24f32c381


GROOVY-7654: Add DefaultGroovyMethods.asType(Iterable, Class) and delegate to Collection variant
only if dealing with a Collection (clses #546)


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

Branch: refs/heads/master
Commit: 24f32c381f954f6fba280c1b29983bffc42b004b
Parents: 6b2051e
Author: Sargis Harutyunyan <sargis.harutyunyan@webbfontaine.com>
Authored: Sun May 21 23:35:16 2017 +0400
Committer: paulk <paulk@asert.com.au>
Committed: Tue Jun 13 14:38:25 2017 +1000

----------------------------------------------------------------------
 .../groovy/runtime/DefaultGroovyMethods.java    | 18 +++++++++++++
 .../runtime/DefaultGroovyMethodsTest.groovy     | 27 ++++++++++++++++++++
 2 files changed, 45 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/24f32c38/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 76b9fa6..36c2453 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -10996,6 +10996,24 @@ public class DefaultGroovyMethods extends DefaultGroovyMethodsSupport
{
     }
 
     /**
+     * Converts the given iterable to another type.
+     *
+     * @param iterable a Iterable
+     * @param clazz    the desired class
+     * @return the object resulting from this type conversion
+     * @see #asType(Collection, Class)
+     * @since 2.4.12
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T asType(Iterable iterable, Class<T> clazz) {
+        if (Collection.class.isAssignableFrom(clazz)) {
+            return asType((Collection) toList(iterable), clazz);
+        }
+
+        return asType((Object) iterable, clazz);
+    }
+
+    /**
      * Converts the given collection to another type. A default concrete
      * type is used for List, Set, or SortedSet. If the given type has
      * a constructor taking a collection, that is used. Otherwise, the

http://git-wip-us.apache.org/repos/asf/groovy/blob/24f32c38/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 087b301..ebea8fd 100644
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
@@ -236,7 +236,34 @@ public class DefaultGroovyMethodsTest extends GroovyTestCase {
         assertEquals(3, list.get(2));
     }
 
+    // GROOVY-7654
+    public void testIterableAsList() {
+        def list = [1, 2, 3]
+        def iterable = new IterableWrapper(delegate: list)
+
+        def iterableAsIterable = iterable as Iterable
+        assertTrue(iterableAsIterable.is(iterable))
+
+        def iterableAsIterableWrapper = iterable as IterableWrapper
+        assertTrue(iterableAsIterableWrapper.is(iterable))
+
+        def iterableAsList = iterable.asList()
+        def iterableAsType = iterable as List
+
+        assertEquals(iterableAsList, iterableAsType)
+        assertEquals(1, iterableAsList[0])
+        assertEquals(1, iterableAsType[0])
+    }
+
     private static class MyList extends ArrayList {
         public MyList() {}
     }
+
+    private static class IterableWrapper implements Iterable {
+        Iterable delegate
+
+        Iterator iterator() {
+            delegate.iterator()
+        }
+    }
 }


Mime
View raw message