Skip to content

Commit

Permalink
GroovyCollection test implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
yukoba committed Aug 1, 2014
1 parent e7536da commit 745e42a
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/groovy/util/GroovyCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package groovy.util;

import java.util.Collection;

/**
* This is an interface for a Groovy custom collection.
* <p/>
* You must also implement a public default constructor with no arguments.
* A public default constructor with no arguments must create size 0 collection.
*
* @author Yu Kobayashi
*/
public interface GroovyCollection<T> extends Collection<T> {
/**
* Creates a new similar collection.
*
* @return a new size 0 collection
*/
GroovyCollection<T> createSimilar();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import groovy.transform.stc.MapEntryOrKeyValue;
import groovy.transform.stc.SimpleType;
import groovy.util.ClosureComparator;
import groovy.util.GroovyCollection;
import groovy.util.GroovyCollections;
import groovy.util.MapEntry;
import groovy.util.OrderBy;
Expand Down Expand Up @@ -9060,6 +9061,11 @@ public static <T> T asType(Collection col, Class<T> clazz) {
if (col.getClass() == clazz) {
return (T) col;
}
if (GroovyCollection.class.isAssignableFrom(clazz)) {
GroovyCollection result = (GroovyCollection) InvokerHelper.invokeConstructorOf(clazz, null);
result.addAll(col);
return (T) result;
}
if (clazz == List.class) {
return (T) asList((Iterable) col);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import groovy.lang.EmptyRange;
import groovy.lang.IntRange;
import groovy.lang.Range;
import groovy.util.GroovyCollection;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;

import java.io.Closeable;
Expand Down Expand Up @@ -147,6 +148,9 @@ protected static <T> Collection<T> createSimilarCollection(Collection<T> collect
}

protected static <T> Collection<T> createSimilarCollection(Collection<T> orig, int newCapacity) {
if (orig instanceof GroovyCollection) {
return ((GroovyCollection<T>) orig).createSimilar();
}
if (orig instanceof Set) {
return createSimilarSet((Set<T>) orig);
}
Expand Down
131 changes: 131 additions & 0 deletions src/test/groovy/util/GroovyCollectionTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package groovy.util

/**
* @author Yu Kobayashi
*/
class GroovyCollectionTest extends GroovyTestCase {
void testAsType() {
def c1 = [1, 2, 3] as SizeOneCollection<Integer>
assert 1 == c1.size()
assert 3 == c1[0]
}

void testPlus() {
def c1 = [1, 2, 3] as SizeOneCollection<Integer>
def c2 = [4, 5, 6] as SizeOneCollection<Integer>
def c3 = c1 + c2
assert c3 instanceof SizeOneCollection
assert 1 == c3.size()
assert 6 == c3[0]
}

void testCollect() {
def c1 = [1, 2, 3] as SizeOneCollection<Integer>
def c2 = c1.collect { it + 1 }
assert 1 == c2.size()
assert 4 == c2[0]
}

void testEach() {
def c1 = [1, 2, 3] as SizeOneCollection<Integer>
c1.each { assert 3 == it }
c1 = [] as SizeOneCollection<Integer>
c1.each { assert false }
}

static class SizeOneCollection<E> implements GroovyCollection<E> {
E e

public SizeOneCollection<E> createSimilar() {
new SizeOneCollection<E>()
}

int size() {
e == null ? 0 : 1
}

boolean isEmpty() {
e == null
}

boolean contains(Object o) {
e == o
}

Iterator iterator() {
e == null ? [].iterator() : [e].iterator()
}

Object[] toArray() {
e == null ? [] as Object[] : [e] as Object[]
}

def <T> T[] toArray(T[] a) {
if (e == null) {
return a
} else {
if (a.length >= 1) {
a[0] = (T) e
return a
} else {
(T[]) ([e] as Object[])
}
}
}

boolean add(Object o) {
e = (E) o
true
}

boolean remove(Object o) {
if (e == o) {
e = null
true
} else {
false
}
}

boolean addAll(Collection c) {
c.size() > 0 ? add(c.last()) : false
}

void clear() {
e = null
}

boolean retainAll(Collection c) {
if (c.contains(e)) {
false
} else {
e = null
true
}
}

boolean removeAll(Collection c) {
c.each { remove(it) }
}

boolean containsAll(Collection c) {
return e != null && c.contains(e)
}
}
}

0 comments on commit 745e42a

Please sign in to comment.