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

Also see the following revisions:

    ------------------------------------------------------------------------
    r751858 | mbenson | 2009-03-09 14:45:53 -0700 (Mon, 09 Mar 2009) | 1 line
    
    generics
    ------------------------------------------------------------------------
    r740150 | mbenson | 2009-02-02 15:24:00 -0800 (Mon, 02 Feb 2009) | 1 line
    
    make all [collections] maps implement IterableMap
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815133 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:57:39 +00:00
parent 68b5806258
commit 6f76f2b205
1 changed files with 47 additions and 29 deletions

View File

@ -23,6 +23,8 @@ 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.IterableMap;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.TransformerUtils; import org.apache.commons.collections.TransformerUtils;
import org.apache.commons.collections.collection.TestTransformedCollection; import org.apache.commons.collections.collection.TestTransformedCollection;
@ -35,8 +37,8 @@ import org.apache.commons.collections.collection.TestTransformedCollection;
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
public class TestTransformedMap extends AbstractTestMap { public class TestTransformedMap<K, V> extends AbstractTestIterableMap<K, V> {
public TestTransformedMap(String testName) { public TestTransformedMap(String testName) {
super(testName); super(testName);
} }
@ -51,32 +53,38 @@ public class TestTransformedMap extends AbstractTestMap {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
public Map makeEmptyMap() { public IterableMap<K, V> makeObject() {
return TransformedMap.decorate(new HashMap(), TransformerUtils.nopTransformer(), TransformerUtils.nopTransformer()); return TransformedMap.decorate(new HashMap<K, V>(), TransformerUtils.<K> nopTransformer(),
TransformerUtils.<V> nopTransformer());
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testTransformedMap() { public void testTransformedMap() {
Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"}; Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "6" };
Map map = TransformedMap.decorate(new HashMap(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER, null); Map<K, V> map = TransformedMap
.decorate(
new HashMap<K, V>(),
(Transformer<? super K, ? extends K>) TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER,
null);
assertEquals(0, map.size()); assertEquals(0, map.size());
for (int i = 0; i < els.length; i++) { for (int i = 0; i < els.length; i++) {
map.put(els[i], els[i]); map.put((K) els[i], (V) els[i]);
assertEquals(i + 1, map.size()); assertEquals(i + 1, map.size());
assertEquals(true, map.containsKey(new Integer((String) els[i]))); assertEquals(true, map.containsKey(new Integer((String) els[i])));
assertEquals(false, map.containsKey(els[i])); assertEquals(false, map.containsKey(els[i]));
assertEquals(true, map.containsValue(els[i])); assertEquals(true, map.containsValue(els[i]));
assertEquals(els[i], map.get(new Integer((String) els[i]))); assertEquals(els[i], map.get(new Integer((String) els[i])));
} }
assertEquals(null, map.remove(els[0])); assertEquals(null, map.remove(els[0]));
assertEquals(els[0], map.remove(new Integer((String) els[0]))); assertEquals(els[0], map.remove(new Integer((String) els[0])));
map = TransformedMap.decorate(new HashMap(), null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); map = TransformedMap.decorate(new HashMap(), null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
assertEquals(0, map.size()); assertEquals(0, map.size());
for (int i = 0; i < els.length; i++) { for (int i = 0; i < els.length; i++) {
map.put(els[i], els[i]); map.put((K) els[i], (V) els[i]);
assertEquals(i + 1, map.size()); assertEquals(i + 1, map.size());
assertEquals(true, map.containsValue(new Integer((String) els[i]))); assertEquals(true, map.containsValue(new Integer((String) els[i])));
assertEquals(false, map.containsValue(els[i])); assertEquals(false, map.containsValue(els[i]));
@ -85,13 +93,13 @@ public class TestTransformedMap extends AbstractTestMap {
} }
assertEquals(new Integer((String) els[0]), map.remove(els[0])); assertEquals(new Integer((String) els[0]), map.remove(els[0]));
Set entrySet = map.entrySet(); Set<Map.Entry<K, V>> entrySet = map.entrySet();
Map.Entry[] array = (Map.Entry[]) entrySet.toArray(new Map.Entry[0]); Map.Entry<K, V>[] array = entrySet.toArray(new Map.Entry[0]);
array[0].setValue("66"); array[0].setValue((V) "66");
assertEquals(new Integer(66), array[0].getValue()); assertEquals(new Integer(66), array[0].getValue());
assertEquals(new Integer(66), map.get(array[0].getKey())); assertEquals(new Integer(66), map.get(array[0].getKey()));
Map.Entry entry = (Map.Entry) entrySet.iterator().next(); Map.Entry entry = (Map.Entry) entrySet.iterator().next();
entry.setValue("88"); entry.setValue("88");
assertEquals(new Integer(88), entry.getValue()); assertEquals(new Integer(88), entry.getValue());
@ -99,33 +107,43 @@ public class TestTransformedMap extends AbstractTestMap {
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testFactory_Decorate() { public void testFactory_Decorate() {
Map base = new HashMap(); Map<K, V> base = new HashMap<K, V>();
base.put("A", "1"); base.put((K) "A", (V) "1");
base.put("B", "2"); base.put((K) "B", (V) "2");
base.put("C", "3"); base.put((K) "C", (V) "3");
Map trans = TransformedMap.decorate(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); Map<K, V> trans = TransformedMap
.decorate(
base,
null,
(Transformer<? super V, ? extends V>) TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
assertEquals(3, trans.size()); assertEquals(3, trans.size());
assertEquals("1", trans.get("A")); assertEquals("1", trans.get("A"));
assertEquals("2", trans.get("B")); assertEquals("2", trans.get("B"));
assertEquals("3", trans.get("C")); assertEquals("3", trans.get("C"));
trans.put("D", "4"); trans.put((K) "D", (V) "4");
assertEquals(new Integer(4), trans.get("D")); assertEquals(new Integer(4), trans.get("D"));
} }
@SuppressWarnings("unchecked")
public void testFactory_decorateTransform() { public void testFactory_decorateTransform() {
Map base = new HashMap(); Map<K, V> base = new HashMap<K, V>();
base.put("A", "1"); base.put((K) "A", (V) "1");
base.put("B", "2"); base.put((K) "B", (V) "2");
base.put("C", "3"); base.put((K) "C", (V) "3");
Map trans = TransformedMap.decorateTransform(base, null, TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); Map<K, V> trans = TransformedMap
.decorateTransform(
base,
null,
(Transformer<? super V, ? extends V>) TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER);
assertEquals(3, trans.size()); assertEquals(3, trans.size());
assertEquals(new Integer(1), trans.get("A")); assertEquals(new Integer(1), trans.get("A"));
assertEquals(new Integer(2), trans.get("B")); assertEquals(new Integer(2), trans.get("B"));
assertEquals(new Integer(3), trans.get("C")); assertEquals(new Integer(3), trans.get("C"));
trans.put("D", "4"); trans.put((K) "D", (V) "4");
assertEquals(new Integer(4), trans.get("D")); assertEquals(new Integer(4), trans.get("D"));
} }