Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 26601 invoked from network); 15 Sep 2009 05:56:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 15 Sep 2009 05:56:31 -0000 Received: (qmail 22523 invoked by uid 500); 15 Sep 2009 05:56:29 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 22413 invoked by uid 500); 15 Sep 2009 05:56:29 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 22283 invoked by uid 99); 15 Sep 2009 05:56:29 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Sep 2009 05:56:29 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Sep 2009 05:56:25 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id E234F23889D1; Tue, 15 Sep 2009 05:56:01 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r815075 - /commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/DefaultedMap.java Date: Tue, 15 Sep 2009 05:56:01 -0000 To: commits@commons.apache.org From: bayard@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20090915055601.E234F23889D1@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: bayard Date: Tue Sep 15 05:56:01 2009 New Revision: 815075 URL: http://svn.apache.org/viewvc?rev=815075&view=rev Log: 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 ------------------------------------------------------------------------ Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/DefaultedMap.java Modified: commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/DefaultedMap.java URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/DefaultedMap.java?rev=815075&r1=815074&r2=815075&view=diff ============================================================================== --- commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/DefaultedMap.java (original) +++ commons/proper/collections/trunk/src/java/org/apache/commons/collections/map/DefaultedMap.java Tue Sep 15 05:56:01 2009 @@ -24,6 +24,7 @@ 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 @@ * @author Rafael U.C. Afonso * @see LazyMap */ -public class DefaultedMap - extends AbstractMapDecorator - implements Map, Serializable { +public class DefaultedMap extends AbstractMapDecorator 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 value; //----------------------------------------------------------------------- /** @@ -83,11 +82,8 @@ * @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 Map decorate(Map map, V defaultValue) { + return new DefaultedMap(map, ConstantTransformer.getInstance(defaultValue)); } /** @@ -100,11 +96,11 @@ * @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 IterableMap decorate(Map map, Factory factory) { if (factory == null) { throw new IllegalArgumentException("Factory must not be null"); } - return new DefaultedMap(map, FactoryTransformer.getInstance(factory)); + return new DefaultedMap(map, FactoryTransformer.getInstance(factory)); } /** @@ -118,11 +114,11 @@ * @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 Map decorate(Map map, Transformer transformer) { if (transformer == null) { throw new IllegalArgumentException("Transformer must not be null"); } - return new DefaultedMap(map, transformer); + return new DefaultedMap(map, transformer); } //----------------------------------------------------------------------- @@ -135,12 +131,18 @@ * * @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 DefaultedMap that decorates + * a HashMap. + *

+ * @param defaultValueTransformer transformer to use to generate missing values. + */ + public DefaultedMap(Transformer defaultValueTransformer) { + this(new HashMap(), defaultValueTransformer); } /** @@ -150,9 +152,9 @@ * @param value the value to use * @throws IllegalArgumentException if map or transformer is null */ - protected DefaultedMap(Map map, Object value) { + protected DefaultedMap(Map map, Transformer defaultValueTransformer) { super(map); - this.value = value; + this.value = defaultValueTransformer; } //----------------------------------------------------------------------- @@ -174,19 +176,18 @@ * @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); }