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 2020/12/01 04:08:00 UTC
[groovy] branch master updated: Tweak casting types for GINQ
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 e1edb01 Tweak casting types for GINQ
e1edb01 is described below
commit e1edb01cf251cab500b26b173f36f93ab9462afc
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue Dec 1 12:07:20 2020 +0800
Tweak casting types for GINQ
---
.../collection/runtime/QueryableCollection.java | 28 +++++++
.../test/org/apache/groovy/ginq/GinqTest.groovy | 87 +++++++++++++++++++++-
2 files changed, 114 insertions(+), 1 deletion(-)
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 792ef3c..18fc04d 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
@@ -20,15 +20,19 @@ package org.apache.groovy.ginq.provider.collection.runtime;
import groovy.lang.Tuple2;
import groovy.transform.Internal;
+import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
+import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
@@ -354,6 +358,30 @@ class QueryableCollection<T> implements Queryable<T>, Serializable {
this.sourceIterable = this.sourceStream.collect(Collectors.toList());
}
+ public Object asType(Class<?> clazz) {
+ if (List.class == clazz || Collection.class == clazz || Iterable.class == clazz) {
+ return toList();
+ }
+
+ if (clazz.isArray()) {
+ return DefaultGroovyMethods.asType(toList(), clazz);
+ }
+
+ if (Set.class == clazz) {
+ return new LinkedHashSet<>(toList());
+ }
+
+ if (Stream.class == clazz) {
+ return stream();
+ }
+
+ if (Iterator.class == clazz) {
+ return iterator();
+ }
+
+ return DefaultGroovyMethods.asType(this, clazz);
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
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 8711d41..f1d805e 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
@@ -3287,11 +3287,96 @@ class GinqTest {
}
@Test
- void "testGinq - GQL - 8"() {
+ void "testGinq - GQL - 1"() {
assertScript '''
// tag::ginq_tips_06[]
assert [4, 16, 36, 64, 100] == GQL {from n in 1..<11 where n % 2 == 0 select n ** 2}
// end::ginq_tips_06[]
'''
}
+
+ @Test
+ void "testGinq - GQL - 2"() {
+ assertScript '''
+ def result = GQL {from n in [1] select n}
+ assert result instanceof List
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 1"() {
+ assertScript '''
+ def result = GQ {from n in [1] select n} as Collection
+ assert result instanceof List
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 2"() {
+ assertScript '''
+ def result = GQ {from n in [1] select n} as int[]
+ assert result instanceof int[]
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 3"() {
+ assertScript '''
+ def result = GQ {from n in [1] select n} as Set
+ assert result instanceof Set
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 4"() {
+ assertScript '''
+ import java.util.stream.Stream
+
+ def result = GQ {from n in [1] select n} as Stream
+ assert result instanceof Stream
+ assert 1 == result.findFirst().get()
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 5"() {
+ assertScript '''
+ def result = GQ {from n in [1] select n} as Iterable
+ assert result instanceof List
+ assert 1 == result[0]
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 6"() {
+ assertScript '''
+ def result = GQ {from n in [1] select n} as Iterator
+ assert result instanceof Iterator
+ assert 1 == result.next()
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 7"() {
+ assertScript '''
+ import org.apache.groovy.ginq.provider.collection.runtime.Queryable
+
+ def original = GQ {from n in [1] select n}
+ def result = original as Queryable
+ assert original === result
+ '''
+ }
+
+ @Test
+ void "testGinq - asType - 8"() {
+ assertScript '''
+ def original = GQ {from n in [1] select n}
+ def result = original as Object
+ assert original === result
+ '''
+ }
}