Repository: groovy Updated Branches: refs/heads/GROOVY_2_6_X fe20e3741 -> c52c1c0b0
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/c52c1c0b Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c52c1c0b Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c52c1c0b Branch: refs/heads/GROOVY_2_6_X Commit: c52c1c0b02f3bf6d096e92ac6569f6c4b073c104 Parents: fe20e37 Author: Sargis Harutyunyan <[email protected]> Authored: Sun May 21 23:35:16 2017 +0400 Committer: paulk <[email protected]> Committed: Tue Jun 13 15:07:49 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/c52c1c0b/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 98041c7..43dbd39 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/c52c1c0b/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() + } + } }
