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:
parent
2fd0de757b
commit
da436314a9
|
@ -24,6 +24,7 @@ import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.commons.collections.Factory;
|
import org.apache.commons.collections.Factory;
|
||||||
|
import org.apache.commons.collections.IterableMap;
|
||||||
import org.apache.commons.collections.Transformer;
|
import org.apache.commons.collections.Transformer;
|
||||||
import org.apache.commons.collections.functors.ConstantTransformer;
|
import org.apache.commons.collections.functors.ConstantTransformer;
|
||||||
import org.apache.commons.collections.functors.FactoryTransformer;
|
import org.apache.commons.collections.functors.FactoryTransformer;
|
||||||
|
@ -63,15 +64,13 @@ import org.apache.commons.collections.functors.FactoryTransformer;
|
||||||
* @author Rafael U.C. Afonso
|
* @author Rafael U.C. Afonso
|
||||||
* @see LazyMap
|
* @see LazyMap
|
||||||
*/
|
*/
|
||||||
public class DefaultedMap
|
public class DefaultedMap<K, V> extends AbstractMapDecorator<K, V> implements Serializable {
|
||||||
extends AbstractMapDecorator
|
|
||||||
implements Map, Serializable {
|
|
||||||
|
|
||||||
/** Serialization version */
|
/** Serialization version */
|
||||||
private static final long serialVersionUID = 19698628745827L;
|
private static final long serialVersionUID = 19698628745827L;
|
||||||
|
|
||||||
/** The transformer to use if the map does not contain a key */
|
/** 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
|
* @param defaultValue the default value to return when the key is not found
|
||||||
* @throws IllegalArgumentException if map is null
|
* @throws IllegalArgumentException if map is null
|
||||||
*/
|
*/
|
||||||
public static Map decorate(Map map, Object defaultValue) {
|
public static <K, V> Map<K, V> decorate(Map<K, V> map, V defaultValue) {
|
||||||
if (defaultValue instanceof Transformer) {
|
return new DefaultedMap<K, V>(map, ConstantTransformer.getInstance(defaultValue));
|
||||||
defaultValue = ConstantTransformer.getInstance(defaultValue);
|
|
||||||
}
|
|
||||||
return new DefaultedMap(map, defaultValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -100,11 +96,11 @@ public class DefaultedMap
|
||||||
* @param factory the factory to use to create entries, must not be null
|
* @param factory the factory to use to create entries, must not be null
|
||||||
* @throws IllegalArgumentException if map or factory is 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) {
|
if (factory == null) {
|
||||||
throw new IllegalArgumentException("Factory must not be 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
|
* @param transformer the transformer to use as a factory to create entries, must not be null
|
||||||
* @throws IllegalArgumentException if map or factory is 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) {
|
if (transformer == null) {
|
||||||
throw new IllegalArgumentException("Transformer must not be 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
|
* @param defaultValue the default value to return when the key is not found
|
||||||
*/
|
*/
|
||||||
public DefaultedMap(Object defaultValue) {
|
public DefaultedMap(V defaultValue) {
|
||||||
super(new HashMap());
|
this(ConstantTransformer.getInstance(defaultValue));
|
||||||
if (defaultValue instanceof Transformer) {
|
|
||||||
defaultValue = ConstantTransformer.getInstance(defaultValue);
|
|
||||||
}
|
}
|
||||||
this.value = 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
|
* @param value the value to use
|
||||||
* @throws IllegalArgumentException if map or transformer is null
|
* @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);
|
super(map);
|
||||||
this.value = value;
|
this.value = defaultValueTransformer;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
@ -174,19 +176,18 @@ public class DefaultedMap
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||||
in.defaultReadObject();
|
in.defaultReadObject();
|
||||||
map = (Map) in.readObject();
|
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
|
// create value for key if key is not currently in the map
|
||||||
if (map.containsKey(key) == false) {
|
if (map.containsKey(key) == false) {
|
||||||
if (value instanceof Transformer) {
|
return value.transform((K) key);
|
||||||
return ((Transformer) value).transform(key);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
return map.get(key);
|
return map.get(key);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue