Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r571381 | skestle | 2007-08-30 22:13:56 -0700 (Thu, 30 Aug 2007) | 1 line Generified LazyMap ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815078 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
027b776f7e
commit
7701366040
|
@ -62,15 +62,13 @@ import org.apache.commons.collections.functors.FactoryTransformer;
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @author Paul Jack
|
* @author Paul Jack
|
||||||
*/
|
*/
|
||||||
public class LazyMap
|
public class LazyMap<K, V> extends AbstractMapDecorator<K, V> implements Map<K, V>, Serializable {
|
||||||
extends AbstractMapDecorator
|
|
||||||
implements Map, Serializable {
|
|
||||||
|
|
||||||
/** Serialization version */
|
/** Serialization version */
|
||||||
private static final long serialVersionUID = 7990956402564206740L;
|
private static final long serialVersionUID = 7990956402564206740L;
|
||||||
|
|
||||||
/** The factory to use to construct elements */
|
/** The factory to use to construct elements */
|
||||||
protected final Transformer factory;
|
protected final Transformer<? super K, ? extends V> factory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method to create a lazily instantiated map.
|
* Factory method to create a lazily instantiated map.
|
||||||
|
@ -78,9 +76,11 @@ public class LazyMap
|
||||||
* @param map the map to decorate, must not be null
|
* @param map the map to decorate, must not be null
|
||||||
* @param factory the factory to use, must not be null
|
* @param factory the factory to use, must not be null
|
||||||
* @throws IllegalArgumentException if map or factory is null
|
* @throws IllegalArgumentException if map or factory is null
|
||||||
|
* @deprecated use {@link #getLazyMap(Map, Factory)} instead.
|
||||||
*/
|
*/
|
||||||
public static Map decorate(Map map, Factory factory) {
|
@Deprecated
|
||||||
return new LazyMap(map, factory);
|
public static <K,V> Map<K,V> decorate(Map<K,V> map, Factory<? extends V> factory) {
|
||||||
|
return getLazyMap(map, factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -90,8 +90,32 @@ public class LazyMap
|
||||||
* @param factory the factory to use, must not be null
|
* @param factory the factory to use, 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 factory) {
|
public static <K, V> LazyMap<K, V> getLazyMap(Map<K, V> map, Factory< ? extends V> factory) {
|
||||||
return new LazyMap(map, factory);
|
return new LazyMap<K,V>(map, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory method to create a lazily instantiated 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 use {@link #getLazyMap(Map, Transformer)} instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public static <K,V> Map<K,V> decorate(Map<K,V> map, Transformer<? super K, ? extends V> factory) {
|
||||||
|
return getLazyMap(map, factory);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory method to create a lazily instantiated 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 <V, K> LazyMap<K, V> getLazyMap(Map<K, V> map, Transformer<? super K, ? extends V> factory) {
|
||||||
|
return new LazyMap<K,V>(map, factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
|
@ -102,7 +126,7 @@ public class LazyMap
|
||||||
* @param factory the factory to use, must not be null
|
* @param factory the factory to use, must not be null
|
||||||
* @throws IllegalArgumentException if map or factory is null
|
* @throws IllegalArgumentException if map or factory is null
|
||||||
*/
|
*/
|
||||||
protected LazyMap(Map map, Factory factory) {
|
protected LazyMap(Map<K,V> map, Factory<? extends V> factory) {
|
||||||
super(map);
|
super(map);
|
||||||
if (factory == null) {
|
if (factory == null) {
|
||||||
throw new IllegalArgumentException("Factory must not be null");
|
throw new IllegalArgumentException("Factory must not be null");
|
||||||
|
@ -117,7 +141,7 @@ public class LazyMap
|
||||||
* @param factory the factory to use, must not be null
|
* @param factory the factory to use, must not be null
|
||||||
* @throws IllegalArgumentException if map or factory is null
|
* @throws IllegalArgumentException if map or factory is null
|
||||||
*/
|
*/
|
||||||
protected LazyMap(Map map, Transformer factory) {
|
protected LazyMap(Map<K,V> map, Transformer<? super K, ? extends V> factory) {
|
||||||
super(map);
|
super(map);
|
||||||
if (factory == null) {
|
if (factory == null) {
|
||||||
throw new IllegalArgumentException("Factory must not be null");
|
throw new IllegalArgumentException("Factory must not be null");
|
||||||
|
@ -146,22 +170,36 @@ public class LazyMap
|
||||||
* @throws ClassNotFoundException
|
* @throws ClassNotFoundException
|
||||||
* @since Commons Collections 3.1
|
* @since Commons Collections 3.1
|
||||||
*/
|
*/
|
||||||
|
@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) {
|
@Override
|
||||||
|
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) {
|
||||||
Object value = factory.transform(key);
|
K castKey = cast(key);
|
||||||
map.put(key, value);
|
V value = factory.transform(castKey);
|
||||||
|
map.put(castKey, value);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
return map.get(key);
|
return map.get(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method just to cast {@link Object}s to K where necessary. This is done to ensure that the SuppressWarnings does not
|
||||||
|
* cover other stuff that it shouldn't
|
||||||
|
* @param key .
|
||||||
|
* @return the cast key.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
private K cast(Object key) {
|
||||||
|
return (K) key;
|
||||||
|
}
|
||||||
|
|
||||||
// no need to wrap keySet, entrySet or values as they are views of
|
// no need to wrap keySet, entrySet or values as they are views of
|
||||||
// existing map entries - you can't do a map-style get on them.
|
// existing map entries - you can't do a map-style get on them.
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue