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:33:02 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 460fa7ab6 -> f033336a9


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

Branch: refs/heads/GROOVY_2_4_X
Commit: f033336a9d2f4f2682a15cbd14fd04342c082ea5
Parents: 460fa7a
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:45 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/f033336a/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 f192585..515f100 100644
--- a/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
+++ b/src/main/org/codehaus/groovy/runtime/DefaultGroovyMethods.java
@@ -10625,6 +10625,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/f033336a/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 e92a337..8665644 100644
--- a/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
+++ b/src/test/org/codehaus/groovy/runtime/DefaultGroovyMethodsTest.groovy
@@ -195,7 +195,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