Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.

Also see the following revisions:

    ------------------------------------------------------------------------
    r471580 | scolebourne | 2006-11-05 16:22:53 -0800 (Sun, 05 Nov 2006) | 1 line
    
    Generify CompositeCollection
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815137 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:57:45 +00:00
parent 9e8badc3be
commit 1e7b7fc64c
1 changed files with 73 additions and 64 deletions

View File

@ -16,15 +16,16 @@
*/ */
package org.apache.commons.collections.set; package org.apache.commons.collections.set;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.collections.collection.CompositeCollection; import org.apache.commons.collections.collection.CompositeCollection;
import java.util.Set;
import java.util.HashSet;
import java.util.Collection;
/** /**
* Extension of {@link AbstractTestSet} for exercising the * Extension of {@link AbstractTestSet} for exercising the
* {@link CompositeSet} implementation. * {@link CompositeSet} implementation.
@ -36,7 +37,7 @@ import java.util.Collection;
* @author Phil Steitz * @author Phil Steitz
*/ */
public class TestCompositeSet extends AbstractTestSet { public class TestCompositeSet<E> extends AbstractTestSet<E> {
public TestCompositeSet(String name) { public TestCompositeSet(String name) {
super(name); super(name);
} }
@ -45,36 +46,40 @@ public class TestCompositeSet extends AbstractTestSet {
return new TestSuite(TestCompositeSet.class); return new TestSuite(TestCompositeSet.class);
} }
public Set makeEmptySet() { public CompositeSet<E> makeObject() {
final HashSet contained = new HashSet(); final HashSet<E> contained = new HashSet<E>();
CompositeSet set = new CompositeSet(contained); CompositeSet<E> set = new CompositeSet<E>(contained);
set.setMutator( new EmptySetMutator(contained) ); set.setMutator( new EmptySetMutator(contained) );
return set; return set;
} }
public Set buildOne() { @SuppressWarnings("unchecked")
HashSet set = new HashSet(); public Set<E> buildOne() {
set.add("1"); HashSet<E> set = new HashSet<E>();
set.add("2"); set.add((E) "1");
set.add((E) "2");
return set; return set;
} }
public Set buildTwo() { @SuppressWarnings("unchecked")
HashSet set = new HashSet(); public Set<E> buildTwo() {
set.add("3"); HashSet<E> set = new HashSet<E>();
set.add("4"); set.add((E) "3");
set.add((E) "4");
return set; return set;
} }
@SuppressWarnings("unchecked")
public void testContains() { public void testContains() {
CompositeSet set = new CompositeSet(new Set[]{buildOne(), buildTwo()}); CompositeSet<E> set = new CompositeSet<E>(new Set[]{ buildOne(), buildTwo() });
assertTrue(set.contains("1")); assertTrue(set.contains("1"));
} }
@SuppressWarnings("unchecked")
public void testRemoveUnderlying() { public void testRemoveUnderlying() {
Set one = buildOne(); Set<E> one = buildOne();
Set two = buildTwo(); Set<E> two = buildTwo();
CompositeSet set = new CompositeSet(new Set[]{one, two}); CompositeSet<E> set = new CompositeSet<E>(new Set[] { one, two });
one.remove("1"); one.remove("1");
assertFalse(set.contains("1")); assertFalse(set.contains("1"));
@ -82,10 +87,11 @@ public class TestCompositeSet extends AbstractTestSet {
assertFalse(set.contains("3")); assertFalse(set.contains("3"));
} }
@SuppressWarnings("unchecked")
public void testRemoveComposited() { public void testRemoveComposited() {
Set one = buildOne(); Set<E> one = buildOne();
Set two = buildTwo(); Set<E> two = buildTwo();
CompositeSet set = new CompositeSet(new Set[]{one, two}); CompositeSet<E> set = new CompositeSet<E>(new Set[] { one, two });
set.remove("1"); set.remove("1");
assertFalse(one.contains("1")); assertFalse(one.contains("1"));
@ -93,33 +99,35 @@ public class TestCompositeSet extends AbstractTestSet {
assertFalse(one.contains("3")); assertFalse(one.contains("3"));
} }
@SuppressWarnings("unchecked")
public void testFailedCollisionResolution() { public void testFailedCollisionResolution() {
Set one = buildOne(); Set<E> one = buildOne();
Set two = buildTwo(); Set<E> two = buildTwo();
CompositeSet set = new CompositeSet(new Set[]{one, two}); CompositeSet<E> set = new CompositeSet<E>(new Set[] { one, two });
set.setMutator(new CompositeSet.SetMutator() { set.setMutator(new CompositeSet.SetMutator<E>() {
public void resolveCollision(CompositeSet comp, Set existing, public void resolveCollision(CompositeSet<E> comp, Set<E> existing,
Set added, Collection intersects) { Set<E> added, Collection<E> intersects) {
//noop
} }
public boolean add(CompositeCollection composite, public boolean add(CompositeCollection<E> composite,
Collection[] collections, Object obj) { List<Collection<E>> collections, E obj) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean addAll(CompositeCollection composite, public boolean addAll(CompositeCollection<E> composite,
Collection[] collections, Collection coll) { List<Collection<E>> collections, Collection<? extends E> coll) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
public boolean remove(CompositeCollection composite, public boolean remove(CompositeCollection<E> composite,
Collection[] collections, Object obj) { List<Collection<E>> collections, Object obj) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
}); });
HashSet three = new HashSet(); HashSet<E> three = new HashSet<E>();
three.add("1"); three.add((E) "1");
try { try {
set.addComposited(three); set.addComposited(three);
fail("IllegalArgumentException should have been thrown"); fail("IllegalArgumentException should have been thrown");
@ -129,21 +137,22 @@ public class TestCompositeSet extends AbstractTestSet {
} }
} }
@SuppressWarnings("unchecked")
public void testAddComposited() { public void testAddComposited() {
Set one = buildOne(); Set<E> one = buildOne();
Set two = buildTwo(); Set<E> two = buildTwo();
CompositeSet set = new CompositeSet(); CompositeSet<E> set = new CompositeSet<E>();
set.addComposited(one, two); set.addComposited(one, two);
CompositeSet set2 = new CompositeSet(buildOne()); CompositeSet<E> set2 = new CompositeSet<E>(buildOne());
set2.addComposited(buildTwo()); set2.addComposited(buildTwo());
assertTrue(set.equals(set2)); assertTrue(set.equals(set2));
HashSet set3 = new HashSet(); HashSet<E> set3 = new HashSet<E>();
set3.add("1"); set3.add((E) "1");
set3.add("2"); set3.add((E) "2");
set3.add("3"); set3.add((E) "3");
HashSet set4 = new HashSet(); HashSet<E> set4 = new HashSet<E>();
set4.add("4"); set4.add((E) "4");
CompositeSet set5 = new CompositeSet(set3); CompositeSet<E> set5 = new CompositeSet<E>(set3);
set5.addComposited(set4); set5.addComposited(set4);
assertTrue(set.equals(set5)); assertTrue(set.equals(set5));
try { try {