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

Also see the following revisions:

    ------------------------------------------------------------------------
    r751894 | mbenson | 2009-03-09 15:48:07 -0700 (Mon, 09 Mar 2009) | 1 line
    
    add splitmap package whose original goal is to provide a more versatile TransformedMap implementation
    ------------------------------------------------------------------------


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:57:54 +00:00
parent ed32276f0d
commit 352f016239
3 changed files with 417 additions and 0 deletions

View File

@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.commons.collections.splitmap;
import junit.framework.TestCase;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* Entry point for tests.
*
* @since Commons Collections 5
* @TODO fix version
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Stephen Kestle
* @author Matt Benson
*/
@RunWith(Suite.class)
@SuiteClasses({
TestSplitMapUtils.class,
TestTransformedMap.class
})
public class TestAll extends TestCase {
}

View File

@ -0,0 +1,241 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.commons.collections.splitmap;
import java.util.HashMap;
import java.util.Map;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.BulkTest;
import org.apache.commons.collections.IterableMap;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.Put;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.Unmodifiable;
import org.apache.commons.collections.functors.NOPTransformer;
import org.apache.commons.collections.map.HashedMap;
/**
* Tests for {@link TransformedMap}
*
* @since Commons Collections 5
* @TODO fix version
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
public class TestSplitMapUtils extends BulkTest {
private Map<String, Integer> backingMap;
private TransformedMap<String, String, String, Integer> transformedMap;
private Transformer<String, Integer> stringToInt = new Transformer<String, Integer>() {
public Integer transform(String input) {
return Integer.valueOf(input);
}
};
public TestSplitMapUtils(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
backingMap = new HashMap<String, Integer>();
transformedMap = TransformedMap.decorate(backingMap, NOPTransformer.<String> getInstance(),
stringToInt);
for (int i = 0; i < 10; i++) {
transformedMap.put(String.valueOf(i), String.valueOf(i));
}
}
public static Test suite() {
return new TestSuite(TestSplitMapUtils.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestSplitMapUtils.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// -----------------------------------------------------------------------
public void testReadableMap() {
final IterableMap<String, Integer> map = SplitMapUtils.readableMap(transformedMap);
// basic
for (int i = 0; i < 10; i++) {
assertFalse(map.containsValue(String.valueOf(i)));
assertEquals(i, map.get(String.valueOf(i)).intValue());
}
// mapIterator
MapIterator<String, Integer> it = map.mapIterator();
while (it.hasNext()) {
String k = it.next();
assertEquals(k, it.getKey());
assertEquals(Integer.valueOf(k), it.getValue());
}
// unmodifiable
assertTrue(map instanceof Unmodifiable);
// check individual operations
int sz = map.size();
attemptPutOperation(new Runnable() {
public void run() {
map.clear();
}
});
assertEquals(sz, map.size());
attemptPutOperation(new Runnable() {
public void run() {
map.put("foo", 100);
}
});
final HashMap<String, Integer> m = new HashMap<String, Integer>();
m.put("foo", 100);
m.put("bar", 200);
m.put("baz", 300);
attemptPutOperation(new Runnable() {
public void run() {
map.putAll(m);
}
});
// equals, hashcode
IterableMap<String, Integer> other = SplitMapUtils.readableMap(transformedMap);
assertEquals(other, map);
assertEquals(other.hashCode(), map.hashCode());
// remove
for (int i = 0; i < 10; i++) {
assertEquals(i, map.remove(String.valueOf(i)).intValue());
assertEquals(--sz, map.size());
}
assertTrue(map.isEmpty());
assertSame(map, SplitMapUtils.readableMap(map));
}
public void testAlreadyReadableMap() {
HashedMap<String, Integer> hashedMap = new HashedMap<String, Integer>();
assertSame(hashedMap, SplitMapUtils.readableMap(hashedMap));
}
@SuppressWarnings("unchecked")
public void testWritableMap() {
final Map<String, String> map = SplitMapUtils.writableMap(transformedMap);
attemptGetOperation(new Runnable() {
public void run() {
map.get(null);
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.entrySet();
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.keySet();
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.values();
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.size();
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.isEmpty();
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.containsKey(null);
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.containsValue(null);
}
});
attemptGetOperation(new Runnable() {
public void run() {
map.remove(null);
}
});
// equals, hashcode
Map<String, String> other = SplitMapUtils.writableMap(transformedMap);
assertEquals(other, map);
assertEquals(other.hashCode(), map.hashCode());
// put
int sz = backingMap.size();
assertFalse(backingMap.containsKey("foo"));
map.put("new", "66");
assertEquals(++sz, backingMap.size());
// putall
Map<String, String> more = new HashMap<String, String>();
more.put("foo", "77");
more.put("bar", "88");
more.put("baz", "99");
map.putAll(more);
assertEquals(sz + more.size(), backingMap.size());
// clear
map.clear();
assertTrue(backingMap.isEmpty());
assertSame(map, SplitMapUtils.writableMap((Put<String, String>) map));
}
public void testAlreadyWritableMap() {
HashedMap<String, String> hashedMap = new HashedMap<String, String>();
assertSame(hashedMap, SplitMapUtils.writableMap(hashedMap));
}
private void attemptGetOperation(Runnable r) {
attemptMapOperation("Put exposed as writable Map must not allow Get operations", r);
}
private void attemptPutOperation(Runnable r) {
attemptMapOperation("Get exposed as writable Map must not allow Put operations", r);
}
private void attemptMapOperation(String s, Runnable r) {
try {
r.run();
fail(s);
} catch (UnsupportedOperationException e) {
}
}
}

View File

@ -0,0 +1,134 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.commons.collections.splitmap;
import java.math.BigInteger;
import java.util.HashMap;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.BulkTest;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.NOPTransformer;
/**
* Tests for {@link TransformedMap}
*
* @since Commons Collections 5
* @TODO fix version, add Serialization tests
* @version $Revision$ $Date$
*
* @author Stephen Colebourne
* @author Matt Benson
*/
public class TestTransformedMap extends BulkTest {
private Transformer<Integer, String> intToString = new Transformer<Integer, String>() {
public String transform(Integer input) {
return String.valueOf(input);
};
};
private Transformer<Object, Class<?>> objectToClass = new Transformer<Object, Class<?>>() {
public java.lang.Class<?> transform(Object input) {
return input == null ? null : input.getClass();
}
};
private Transformer<String, Integer> stringToInt = new Transformer<String, Integer>() {
public Integer transform(String input) {
return Integer.valueOf(input);
}
};
public TestTransformedMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestTransformedMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestTransformedMap.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
// -----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testTransformedMap() {
TransformedMap<Integer, String, Object, Class<?>> map = TransformedMap.decorate(
new HashMap<String, Class<?>>(), intToString, objectToClass);
Integer[] k = new Integer[] { 0, 1, 2, 3, 4, 5, 6 };
Object[] v = new Object[] { "", new Object(), new HashMap(), 0, BigInteger.TEN, null,
new Object[0] };
assertEquals(0, map.size());
for (int i = 0; i < k.length; i++) {
map.put(k[i], v[i]);
assertEquals(i + 1, map.size());
assertTrue(map.containsKey(intToString.transform(k[i])));
assertFalse(map.containsKey(k[i]));
assertTrue(map.containsValue(objectToClass.transform(v[i])));
assertTrue(objectToClass.transform(v[i]) != v[i] ^ map.containsValue(v[i]));
assertEquals(objectToClass.transform(v[i]), map.get(intToString.transform(k[i])));
}
int sz = map.size();
assertEquals(null, map.remove(k[0]));
assertEquals(sz, map.size());
assertEquals(objectToClass.transform(v[0]), map.remove(intToString.transform(k[0])));
assertEquals(--sz, map.size());
TransformedMap<String, String, String, Integer> map2 = TransformedMap.decorate(
new HashMap<String, Integer>(), NOPTransformer.<String> getInstance(), stringToInt);
assertEquals(0, map2.size());
for (int i = 0; i < 6; i++) {
map2.put(String.valueOf(i), String.valueOf(i));
assertEquals(i + 1, map2.size());
assertTrue(map2.containsValue(i));
assertFalse(map2.containsValue(String.valueOf(i)));
assertTrue(map2.containsKey(String.valueOf(i)));
assertEquals(i, map2.get(String.valueOf(i)).intValue());
}
int sz2 = map2.size();
assertEquals(Integer.valueOf(0), map2.remove("0"));
assertEquals(--sz2, map2.size());
}
// -----------------------------------------------------------------------
public void testMapIterator() {
TransformedMap<String, String, String, Integer> map = TransformedMap.decorate(
new HashMap<String, Integer>(), NOPTransformer.<String> getInstance(), stringToInt);
assertEquals(0, map.size());
for (int i = 0; i < 6; i++) {
map.put(String.valueOf(i), String.valueOf(i));
}
for (MapIterator<String, Integer> it = map.mapIterator(); it.hasNext();) {
String k = it.next();
assertEquals(k, it.getKey());
assertEquals(map.get(k), it.getValue());
}
}
}