forked from groovy/groovy-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GroovyCollection test implementation.
- Loading branch information
Showing
4 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |