Hold a Transformer internally, as wrap Factory as Transformer

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131093 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-05-17 14:11:10 +00:00
parent f22de4bfaa
commit 17c05ab02e
2 changed files with 13 additions and 30 deletions

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/LazyMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/LazyMap.java,v 1.2 2003/05/17 14:11:10 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -61,6 +61,7 @@ import java.util.Map;
import org.apache.commons.collections.Factory; import org.apache.commons.collections.Factory;
import org.apache.commons.collections.Transformer; import org.apache.commons.collections.Transformer;
import org.apache.commons.collections.TransformerUtils;
/** /**
* <code>LazyMap</code> decorates another <code>Map</code> * <code>LazyMap</code> decorates another <code>Map</code>
@ -86,7 +87,7 @@ import org.apache.commons.collections.Transformer;
* instance is mapped to the "NOW" key in the map. * instance is mapped to the "NOW" key in the map.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ * @version $Revision: 1.2 $ $Date: 2003/05/17 14:11:10 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
@ -94,7 +95,7 @@ import org.apache.commons.collections.Transformer;
public class LazyMap extends AbstractMapDecorator implements Map { public class LazyMap extends AbstractMapDecorator implements Map {
/** The factory to use to construct elements */ /** The factory to use to construct elements */
protected final Object factory; protected final Transformer factory;
/** /**
* Factory method to create a lazily instantiated map. * Factory method to create a lazily instantiated map.
@ -130,7 +131,7 @@ public class LazyMap extends AbstractMapDecorator implements Map {
if (factory == null) { if (factory == null) {
throw new IllegalArgumentException("Factory must not be null"); throw new IllegalArgumentException("Factory must not be null");
} }
this.factory = factory; this.factory = TransformerUtils.asTransformer(factory);
} }
/** /**
@ -152,15 +153,9 @@ public class LazyMap extends AbstractMapDecorator implements Map {
public Object get(Object key) { public Object 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 (factory instanceof Factory) { Object value = factory.transform(key);
Object value = ((Factory) factory).create();
map.put(key, value); map.put(key, value);
return value; return value;
} else {
Object value = ((Transformer) factory).transform(key);
map.put(key, value);
return value;
}
} }
return map.get(key); return map.get(key);
} }

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/LazySortedMap.java,v 1.1 2003/05/09 16:42:35 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/decorators/Attic/LazySortedMap.java,v 1.2 2003/05/17 14:11:09 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -87,7 +87,7 @@ import org.apache.commons.collections.Transformer;
* instance is mapped to the "NOW" key in the map. * instance is mapped to the "NOW" key in the map.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/05/09 16:42:35 $ * @version $Revision: 1.2 $ $Date: 2003/05/17 14:11:09 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Paul Jack * @author Paul Jack
@ -162,29 +162,17 @@ public class LazySortedMap extends LazyMap implements SortedMap {
public SortedMap subMap(Object fromKey, Object toKey) { public SortedMap subMap(Object fromKey, Object toKey) {
SortedMap map = getSortedMap().subMap(fromKey, toKey); SortedMap map = getSortedMap().subMap(fromKey, toKey);
if (factory instanceof Factory) { return new LazySortedMap(map, factory);
return new LazySortedMap(map, (Factory) factory);
} else {
return new LazySortedMap(map, (Transformer) factory);
}
} }
public SortedMap headMap(Object toKey) { public SortedMap headMap(Object toKey) {
SortedMap map = getSortedMap().headMap(toKey); SortedMap map = getSortedMap().headMap(toKey);
if (factory instanceof Factory) { return new LazySortedMap(map, factory);
return new LazySortedMap(map, (Factory) factory);
} else {
return new LazySortedMap(map, (Transformer) factory);
}
} }
public SortedMap tailMap(Object fromKey) { public SortedMap tailMap(Object fromKey) {
SortedMap map = getSortedMap().tailMap(fromKey); SortedMap map = getSortedMap().tailMap(fromKey);
if (factory instanceof Factory) { return new LazySortedMap(map, factory);
return new LazySortedMap(map, (Factory) factory);
} else {
return new LazySortedMap(map, (Transformer) factory);
}
} }
} }