//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.util;

import java.lang.reflect.Constructor;

import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.ForwardingMap;
import com.google.common.collect.Maps;

/**
 * Provides a map implementation that automatically creates, and inserts into the map, a default
 * value for any key fetched via the {@link #get} method which has no value. Note that this map
 * must assume that all keys supplied to {@link #get} are valid instances of the type specified by
 * K as those keys will be used in a subsequent call to {@link #put}. Thus you must not call {@link
 * #get} with a key of invalid type or you will cause your map to become unsound.
 */
public class DefaultMap<K, V> extends ForwardingMap<K, V>
{
    /** Used to create default values. */
    public interface Creator<K, V>
    {
        /** Creates a new default value for the specified key. */
        V create (K key);
    }

    /**
     * Creates a default map backed by a {@link HashMap} that creates instances of the supplied
     * class (using its no-args constructor) as defaults.
     */
    public static <K, V> DefaultMap<K, V> newInstanceHashMap (Class<V> clazz)
    {
        return newInstanceMap(Maps.<K, V>newHashMap(), clazz);
    }

    /**
     * Creates a default map backed by the supplied map that creates instances of the supplied
     * class (using its no-args constructor) as defaults.
     */
    public static <K, V> DefaultMap<K, V> newInstanceMap (Map<K, V> delegate, Class<V> clazz)
    {
        final Constructor<V> ctor;
        try {
            ctor = clazz.getConstructor();
        } catch (NoSuchMethodException nsme) {
            throw new IllegalArgumentException(clazz + " must have a no-args constructor.");
        }
        return newMap(delegate, new Creator<K, V>() {
            public V create (K key) {
                try {
                    return ctor.newInstance();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    /**
     * Creates a default map backed by a {@link HashMap} using the supplied default creator.
     */
    public static <K, V> DefaultMap<K, V> newHashMap (Creator<K, V> creator)
    {
        return newMap(Maps.<K, V>newHashMap(), creator);
    }

    /**
     * Creates a default map backed by the supplied map using the supplied default creator.
     */
    public static <K, V> DefaultMap<K, V> newMap (Map<K, V> delegate, Creator<K, V> creator)
    {
        return new DefaultMap<K, V>(delegate, creator);
    }

    /**
     * Creates a default map backed by the supplied map using the supplied default creator.
     */
    public DefaultMap (Map<K, V> delegate, Creator<K, V> creator)
    {
        _delegate = delegate;
        _creator = creator;
    }

    /**
     * Looks up the supplied key in the map, returning the value to which it is mapped. If the key
     * has no mapping, a default value will be obtained for the requested key and placed into the
     * map. The current and subsequent lookups will return that value.
     */
    public V fetch (K key)
    {
        V value = get(key);
        // null is a valid value, so we check containsKey() before creating a default
        if (value == null && !containsKey(key)) {
            put(key, value = _creator.create(key));
        }
        return value;
    }

    @Override // from ForwardingMap
    protected Map<K, V> delegate()
    {
        return _delegate;
    }

    protected final Map<K, V> _delegate;
    protected final Creator<K, V> _creator;
}

@@ -1243,17 +1243,21 @@
 any 
-key fetch
+value retriev
 ed v
@@ -1271,19 +1271,21 @@
 %7B@link #
-g
+f
 et
+ch
 %7D method
@@ -1302,313 +1302,26 @@
  no 
-value. Note that this map%0A * must assume that all keys supplied to %7B@link #get%7D are valid instances of the type specified by%0A * K as those keys will be used in a subsequent call to %7B@link #put%7D. Thus you must not call %7B@link%0A * #get%7D with a key of invalid type or you will cause your map to become unsound
+entry for that key
 .%0A *
@@ -2149,32 +2149,361 @@
 V%3E clazz)%0A    %7B%0A
+        Creator%3CK, V%3E creator = newInstanceCreator(clazz);%0A        return newMap(delegate, creator);%0A    %7D%0A%0A%0A    /**%0A     * Returns a Creator that makes instances of the supplied class (using its no-args%0A     * constructor) as default values.%0A     */%0A    public static %3CK, V%3E Creator%3CK, V%3E newInstanceCreator (Class%3CV%3E clazz) %7B%0A%0A
         final Co
@@ -2742,33 +2742,16 @@
   return
- newMap(delegate,
  new Cre
@@ -2987,32 +2987,32 @@
 %7D%0A            %7D%0A
+
         %7D);%0A    
@@ -3004,17 +3004,16 @@
        %7D
-)
 ;%0A    %7D%0A

Fix the docs now that fetch has shown up and break out a factory for instance Creators separate from newInstanceMap.