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
3 changed files
with
172 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,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 <T> GroovyCollection<T> createSimilar(GroovyCollection<T> from)</pre></li> | ||
* </ol> | ||
* The return size of createSimilar() must be 0. | ||
*/ | ||
public interface GroovyCollection<T> extends Collection<T> { | ||
} |
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,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) | ||
} | ||
} | ||
} |