Skip to content

Commit

Permalink
GroovyCollection test implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
yukoba committed Jul 31, 2014
1 parent e7536da commit 45402ab
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/main/groovy/util/GroovyCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 a marker interface for a custom mutable collection.
* <p/>
* You must implement these two methods.
* <ol>
* <li>A public default constructor with no arguments</li>
* <li><pre>public static &lt;T&gt; GroovyCollection&lt;T&gt; createSimilar(GroovyCollection&lt;T&gt; from)</pre></li>
* </ol>
* The return size of createSimilar() must be 0.
*/
public interface GroovyCollection<T> extends Collection<T> {
}
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 @@ -146,7 +147,11 @@ protected static <T> Collection<T> createSimilarCollection(Collection<T> collect
return createSimilarCollection(collection, collection.size());
}

@SuppressWarnings("unchecked")
protected static <T> Collection<T> createSimilarCollection(Collection<T> orig, int newCapacity) {
if (orig instanceof GroovyCollection) {
return (Collection<T>) InvokerHelper.invokeStaticMethod(orig.getClass(), "createSimilar", orig);
}
if (orig instanceof Set) {
return createSimilarSet((Set<T>) orig);
}
Expand Down
135 changes: 135 additions & 0 deletions src/test/groovy/util/GroovyCollectionTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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

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 }
}

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

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

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

@Override
boolean isEmpty() {
e == null
}

@Override
boolean contains(Object o) {
e == 0
}

@Override
Iterator iterator() {
[e].iterator()
}

@Override
Object[] toArray() {
[e] as Object[]
}

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

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

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

@Override
boolean addAll(Collection c) {
add(c.last())
}

@Override
void clear() {
e = null
}

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

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

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

0 comments on commit 45402ab

Please sign in to comment.