You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2021/07/25 17:01:13 UTC

[groovy] branch master updated: Tweak coercing GINQ result

This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 2941596  Tweak coercing GINQ result
2941596 is described below

commit 29415962ed358597cc45309b896dd081b51ddc04
Author: Daniel Sun <su...@apache.org>
AuthorDate: Mon Jul 26 01:00:50 2021 +0800

    Tweak coercing GINQ result
---
 .../collection/runtime/QueryableCollection.java    | 28 ++++++++-
 .../test/org/apache/groovy/ginq/GinqTest.groovy    | 73 ++++++++++++++++++++--
 2 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
index 2ec26d1..d5d2025 100644
--- a/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
+++ b/subprojects/groovy-ginq/src/main/groovy/org/apache/groovy/ginq/provider/collection/runtime/QueryableCollection.java
@@ -35,12 +35,17 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
+import java.util.Deque;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
+import java.util.Queue;
 import java.util.Set;
+import java.util.TreeSet;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutionException;
@@ -719,14 +724,35 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
         if (List.class == clazz || Collection.class == clazz || Iterable.class == clazz) {
             return toList();
         }
+        if (ArrayList.class == clazz) {
+            List<T> list = toList();
+            if (list instanceof ArrayList) {
+                return list;
+            }
+            return new ArrayList<>(list);
+        }
+        if (LinkedList.class == clazz || Deque.class == clazz || Queue.class == clazz) {
+            List<T> list = toList();
+            if (list instanceof LinkedList) {
+                return list;
+            }
+            return new LinkedList<>(list);
+        }
 
         if (clazz.isArray()) {
             return DefaultGroovyMethods.asType(toList(), clazz);
         }
 
-        if (Set.class == clazz) {
+        if (Set.class == clazz || LinkedHashSet.class == clazz) {
             return new LinkedHashSet<>(toList());
         }
+        if (HashSet.class == clazz) {
+            return new HashSet<>(toList());
+        }
+        if (TreeSet.class == clazz) {
+            return new TreeSet<>(toList());
+        }
+
 
         if (Stream.class == clazz) {
             return stream();
diff --git a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
index 04a38ed..e4a2471 100644
--- a/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
+++ b/subprojects/groovy-ginq/src/spec/test/org/apache/groovy/ginq/GinqTest.groovy
@@ -4180,7 +4180,7 @@ class GinqTest {
     void "testGinq - asType - 1"() {
         assertGinqScript '''
             def result = GQ {from n in [1] select n} as Collection
-            assert result instanceof List
+            assert result instanceof Collection
             assert 1 == result[0]
         '''
     }
@@ -4218,7 +4218,7 @@ class GinqTest {
     void "testGinq - asType - 5"() {
         assertGinqScript '''
             def result = GQ {from n in [1] select n} as Iterable
-            assert result instanceof List
+            assert result instanceof Iterable
             assert 1 == result[0]
         '''
     }
@@ -4262,6 +4262,69 @@ class GinqTest {
     }
 
     @Test
+    void "testGinq - asType - 10"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as ArrayList
+            assert result instanceof ArrayList
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
+    void "testGinq - asType - 11"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as LinkedList
+            assert result instanceof LinkedList
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
+    void "testGinq - asType - 12"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as HashSet
+            assert result instanceof HashSet
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
+    void "testGinq - asType - 13"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as TreeSet
+            assert result instanceof TreeSet
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
+    void "testGinq - asType - 14"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as LinkedHashSet
+            assert result instanceof LinkedHashSet
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
+    void "testGinq - asType - 15"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as Queue
+            assert result instanceof Queue
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
+    void "testGinq - asType - 16"() {
+        assertGinqScript '''
+            def result = GQ {from n in [1] select n} as Deque
+            assert result instanceof Deque
+            assert 1 == result[0]
+        '''
+    }
+
+    @Test
     @CompileDynamic
     void "testGinq - optimize - 1"() {
         def code = '''
@@ -6117,13 +6180,13 @@ class GinqTest {
         assertScript '''
 // tag::ginq_method_01[]
             @groovy.ginq.transform.GQ
-            def ginq(b, e) {
-                from n in [1, 2, 3, 4, 5, 6]
+            def ginq(list, b, e) {
+                from n in list
                 where b < n && n < e
                 select n
             }
             
-            assert [3, 4] == ginq(2, 5).toList()
+            assert [3, 4] == ginq([1, 2, 3, 4, 5, 6], 2, 5).toList()
 // end::ginq_method_01[]
         '''
     }