Generified LazySortedMap to fix build errors

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/branches/collections_jdk5_branch@572176 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Kestle 2007-09-02 22:04:41 +00:00
parent aa66de500c
commit a8f01f53c1
3 changed files with 86 additions and 65 deletions

View File

@ -58,9 +58,9 @@ import org.apache.commons.collections.Transformer;
* @author Stephen Colebourne
* @author Paul Jack
*/
public class LazySortedMap
extends LazyMap<Object, Object>
implements SortedMap<Object, Object> {
public class LazySortedMap<K,V>
extends LazyMap<K,V>
implements SortedMap<K,V> {
/** Serialization version */
private static final long serialVersionUID = 2715322183617658933L;
@ -71,9 +71,11 @@ public class LazySortedMap
* @param map the map to decorate, must not be null
* @param factory the factory to use, must not be null
* @throws IllegalArgumentException if map or factory is null
* @deprecated
*/
public static SortedMap decorate(SortedMap map, Factory factory) {
return new LazySortedMap(map, factory);
@Deprecated
public static <K,V> SortedMap<K,V> decorate(SortedMap<K,V> map, Factory<? extends V> factory) {
return getLazySortedMap(map, factory);
}
/**
@ -83,10 +85,34 @@ public class LazySortedMap
* @param factory the factory to use, must not be null
* @throws IllegalArgumentException if map or factory is null
*/
public static SortedMap decorate(SortedMap map, Transformer factory) {
return new LazySortedMap(map, factory);
public static <K, V> SortedMap<K, V> getLazySortedMap(SortedMap<K, V> map, Factory<? extends V> factory) {
return new LazySortedMap<K,V>(map, factory);
}
/**
* Factory method to create a lazily instantiated sorted map.
*
* @param map the map to decorate, must not be null
* @param factory the factory to use, must not be null
* @throws IllegalArgumentException if map or factory is null
* @deprecated
*/
@Deprecated
public static <K,V> SortedMap<K,V> decorate(SortedMap<K,V> map, Transformer<? super K, ? extends V> factory) {
return getLazySortedMap(map, factory);
}
/**
* Factory method to create a lazily instantiated sorted map.
*
* @param map the map to decorate, must not be null
* @param factory the factory to use, must not be null
* @throws IllegalArgumentException if map or factory is null
*/
public static <K, V> SortedMap<K, V> getLazySortedMap(SortedMap<K, V> map, Transformer<? super K, ? extends V> factory) {
return new LazySortedMap<K,V>(map, factory);
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies).
@ -95,7 +121,7 @@ public class LazySortedMap
* @param factory the factory to use, must not be null
* @throws IllegalArgumentException if map or factory is null
*/
protected LazySortedMap(SortedMap map, Factory factory) {
protected LazySortedMap(SortedMap<K,V> map, Factory<? extends V> factory) {
super(map, factory);
}
@ -106,7 +132,7 @@ public class LazySortedMap
* @param factory the factory to use, must not be null
* @throws IllegalArgumentException if map or factory is null
*/
protected LazySortedMap(SortedMap map, Transformer factory) {
protected LazySortedMap(SortedMap<K,V> map, Transformer<? super K, ? extends V> factory) {
super(map, factory);
}
@ -116,36 +142,36 @@ public class LazySortedMap
*
* @return the decorated map
*/
protected SortedMap getSortedMap() {
return (SortedMap) map;
protected SortedMap<K,V> getSortedMap() {
return (SortedMap<K,V>) map;
}
//-----------------------------------------------------------------------
public Object firstKey() {
public K firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
public K lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
public Comparator<? super K> comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object fromKey, Object toKey) {
SortedMap map = getSortedMap().subMap(fromKey, toKey);
return new LazySortedMap(map, factory);
public SortedMap<K,V> subMap(K fromKey, K toKey) {
SortedMap<K,V> map = getSortedMap().subMap(fromKey, toKey);
return new LazySortedMap<K,V>(map, factory);
}
public SortedMap headMap(Object toKey) {
SortedMap map = getSortedMap().headMap(toKey);
return new LazySortedMap(map, factory);
public SortedMap<K,V> headMap(K toKey) {
SortedMap<K,V> map = getSortedMap().headMap(toKey);
return new LazySortedMap<K,V>(map, factory);
}
public SortedMap tailMap(Object fromKey) {
SortedMap map = getSortedMap().tailMap(fromKey);
return new LazySortedMap(map, factory);
public SortedMap<K,V> tailMap(K fromKey) {
SortedMap<K,V> map = getSortedMap().tailMap(fromKey);
return new LazySortedMap<K,V>(map, factory);
}
}

View File

@ -38,7 +38,6 @@ import org.junit.Test;
public class TestLazyMap extends AbstractTestMap {
private static final Factory<Integer> oneFactory = FactoryUtils.constantFactory(1);
private static final Factory<Object> nullFactory = FactoryUtils.nullFactory();
public TestLazyMap(String testName) {
super(testName);

View File

@ -16,18 +16,18 @@
*/
package org.apache.commons.collections.map;
import static org.apache.commons.collections.map.LazySortedMap.getLazySortedMap;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.TransformerUtils;
import org.junit.Test;
/**
* Extension of {@link TestLazyMap} for exercising the
@ -40,59 +40,54 @@ import org.apache.commons.collections.TransformerUtils;
*/
public class TestLazySortedMap extends AbstractTestSortedMap {
protected static final Factory oneFactory = FactoryUtils.constantFactory("One");
protected static final Factory nullFactory = FactoryUtils.nullFactory();
private static final Factory<Integer> oneFactory = FactoryUtils.constantFactory(1);
public TestLazySortedMap(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestLazySortedMap.class);
}
public static void main(String args[]) {
String[] testCaseName = { TestLazySortedMap.class.getName()};
junit.textui.TestRunner.main(testCaseName);
}
//-----------------------------------------------------------------------
protected SortedMap decorateMap(SortedMap map, Factory factory) {
return LazySortedMap.decorate(map, factory);
@Override
public <K,V> Map<K,V> makeEmptyMap() {
return getLazySortedMap(new TreeMap<K,V>(), FactoryUtils.<V>nullFactory());
}
public Map makeEmptyMap() {
return decorateMap(new TreeMap(), nullFactory);
private <K,V> SortedMap<K,V> makeTestSortedMap(Factory<V> factory) {
return getLazySortedMap(new TreeMap<K,V>(), factory);
}
protected SortedMap makeTestSortedMap(Factory factory) {
return decorateMap(new TreeMap(), factory);
}
public boolean isSubMapViewsSerializable() {
// TreeMap sub map views have a bug in deserialization.
@Override
public boolean isSubMapViewsSerializable() {
// TODO TreeMap sub map views have a bug in deserialization.
return false;
}
public boolean isAllowNullKey() {
@Override
public boolean isAllowNullKey() {
return false;
}
// from TestLazyMap
//-----------------------------------------------------------------------
@Override
public void testMapGet() {
Map map = makeTestSortedMap(oneFactory);
//TODO eliminate need for this via superclass - see svn history.
}
@Test
public void mapGet() {
Map<Integer, Number> map = getLazySortedMap(new TreeMap<Integer,Number>(), oneFactory);
assertEquals(0, map.size());
String s1 = (String) map.get("Five");
assertEquals("One", s1);
Number i1 = map.get(5);
assertEquals(1, i1);
assertEquals(1, map.size());
String s2 = (String) map.get(new String(new char[] {'F','i','v','e'}));
assertEquals("One", s2);
assertEquals(1, map.size());
assertSame(s1, s2);
map = makeTestSortedMap(nullFactory);
Object o = map.get("Five");
map = getLazySortedMap(new TreeMap<Integer,Number>(), FactoryUtils.<Number>nullFactory());
Number o = map.get(5);
assertEquals(null,o);
assertEquals(1, map.size());
@ -100,10 +95,10 @@ public class TestLazySortedMap extends AbstractTestSortedMap {
//-----------------------------------------------------------------------
public void testSortOrder() {
SortedMap map = makeTestSortedMap(oneFactory);
map.put("A", "a");
SortedMap<String, Number> map = getLazySortedMap(new TreeMap<String,Number>(), oneFactory);
map.put("A", 5);
map.get("B"); // Entry with value "One" created
map.put("C", "c");
map.put("C", 8);
assertEquals("First key should be A", map.firstKey(), "A");
assertEquals("Last key should be C", map.lastKey(), "C");
assertEquals("First key in tail map should be B",
@ -113,30 +108,31 @@ public class TestLazySortedMap extends AbstractTestSortedMap {
assertEquals("Last key in submap should be B",
map.subMap("A","C").lastKey(), "B");
Comparator c = map.comparator();
Comparator<?> c = map.comparator();
assertTrue("natural order, so comparator should be null",
c == null);
}
public void testTransformerDecorate() {
Transformer transformer = TransformerUtils.asTransformer(oneFactory);
SortedMap map = LazySortedMap.decorate(new TreeMap(), transformer);
Transformer<Object, Integer> transformer = TransformerUtils.asTransformer(oneFactory);
SortedMap<Integer, Number> map = getLazySortedMap(new TreeMap<Integer, Number>(), transformer);
assertTrue(map instanceof LazySortedMap);
try {
map = LazySortedMap.decorate(new TreeMap(), (Transformer) null);
map = getLazySortedMap(new TreeMap<Integer, Number>(), (Transformer<Integer, Number>) null);
fail("Expecting IllegalArgumentException for null transformer");
} catch (IllegalArgumentException e) {
// expected
}
try {
map = LazySortedMap.decorate(null, transformer);
map = getLazySortedMap((SortedMap<Integer,Number>) null, transformer);
fail("Expecting IllegalArgumentException for null map");
} catch (IllegalArgumentException e) {
// expected
}
}
public String getCompatibilityVersion() {
@Override
public String getCompatibilityVersion() {
return "3.1";
}