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

Also see the following revisions:

    ------------------------------------------------------------------------
    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@815075 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-09-15 05:56:01 +00:00
parent 2fd0de757b
commit da436314a9
1 changed files with 27 additions and 26 deletions

View File

@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.Factory;
import org.apache.commons.collections.IterableMap;
import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.functors.ConstantTransformer;
import org.apache.commons.collections.functors.FactoryTransformer;
@ -63,15 +64,13 @@ import org.apache.commons.collections.functors.FactoryTransformer;
* @author Rafael U.C. Afonso
* @see LazyMap
*/
public class DefaultedMap
extends AbstractMapDecorator
implements Map, Serializable {
public class DefaultedMap<K, V> extends AbstractMapDecorator<K, V> implements Serializable {
/** Serialization version */
private static final long serialVersionUID = 19698628745827L;
/** The transformer to use if the map does not contain a key */
protected final Object value;
private final Transformer<? super K, ? extends V> value;
//-----------------------------------------------------------------------
/**
@ -83,11 +82,8 @@ public class DefaultedMap
* @param defaultValue the default value to return when the key is not found
* @throws IllegalArgumentException if map is null
*/
public static Map decorate(Map map, Object defaultValue) {
if (defaultValue instanceof Transformer) {
defaultValue = ConstantTransformer.getInstance(defaultValue);
}
return new DefaultedMap(map, defaultValue);
public static <K, V> Map<K, V> decorate(Map<K, V> map, V defaultValue) {
return new DefaultedMap<K, V>(map, ConstantTransformer.getInstance(defaultValue));
}
/**
@ -100,11 +96,11 @@ public class DefaultedMap
* @param factory the factory to use to create entries, must not be null
* @throws IllegalArgumentException if map or factory is null
*/
public static Map decorate(Map map, Factory factory) {
public static <K, V> IterableMap<K, V> decorate(Map<K, V> map, Factory<? extends V> factory) {
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
return new DefaultedMap(map, FactoryTransformer.getInstance(factory));
return new DefaultedMap<K, V>(map, FactoryTransformer.getInstance(factory));
}
/**
@ -118,11 +114,11 @@ public class DefaultedMap
* @param transformer the transformer to use as a factory to create entries, must not be null
* @throws IllegalArgumentException if map or factory is null
*/
public static Map decorate(Map map, Transformer transformer) {
public static <K, V> Map<K, V> decorate(Map<K, V> map, Transformer<? super K, ? extends V> transformer) {
if (transformer == null) {
throw new IllegalArgumentException("Transformer must not be null");
}
return new DefaultedMap(map, transformer);
return new DefaultedMap<K, V>(map, transformer);
}
//-----------------------------------------------------------------------
@ -135,12 +131,18 @@ public class DefaultedMap
*
* @param defaultValue the default value to return when the key is not found
*/
public DefaultedMap(Object defaultValue) {
super(new HashMap());
if (defaultValue instanceof Transformer) {
defaultValue = ConstantTransformer.getInstance(defaultValue);
}
this.value = defaultValue;
public DefaultedMap(V defaultValue) {
this(ConstantTransformer.getInstance(defaultValue));
}
/**
* Constructs a new empty <code>DefaultedMap</code> that decorates
* a <code>HashMap</code>.
* <p>
* @param defaultValueTransformer transformer to use to generate missing values.
*/
public DefaultedMap(Transformer<? super K, ? extends V> defaultValueTransformer) {
this(new HashMap<K, V>(), defaultValueTransformer);
}
/**
@ -150,9 +152,9 @@ public class DefaultedMap
* @param value the value to use
* @throws IllegalArgumentException if map or transformer is null
*/
protected DefaultedMap(Map map, Object value) {
protected DefaultedMap(Map<K, V> map, Transformer<? super K, ? extends V> defaultValueTransformer) {
super(map);
this.value = value;
this.value = defaultValueTransformer;
}
//-----------------------------------------------------------------------
@ -174,19 +176,18 @@ public class DefaultedMap
* @throws IOException
* @throws ClassNotFoundException
*/
@SuppressWarnings("unchecked")
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
map = (Map) in.readObject();
}
//-----------------------------------------------------------------------
public Object get(Object key) {
@SuppressWarnings("unchecked")
public V get(Object key) {
// create value for key if key is not currently in the map
if (map.containsKey(key) == false) {
if (value instanceof Transformer) {
return ((Transformer) value).transform(key);
}
return value;
return value.transform((K) key);
}
return map.get(key);
}