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:32:25 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 5552bf3d2 -> c3b6a1212


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

Branch: refs/heads/GROOVY_2_5_X
Commit: c3b6a12125a7d5736faaf70229e80b2210a1d5a1
Parents: 5552bf3
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 15:32:10 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/c3b6a121/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 a8b6868..744c5da 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/c3b6a121/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