From 9b946dfbd596e17d8e4826aab113f87047b90462 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Sat, 12 Sep 2009 01:13:19 +0000 Subject: [PATCH] DefaultMap extracted from Yohoho, simpl- and and MDB-ified. For Charlie's inspection and approval and Matt's eventual use. Note that we can't put this in samskivert as it depends on Google Collections. Perhaps we should create an ooo-utils library for things like this (and possibly move Narya's Serialization replacement therein since it is pretty general purpose; Nate is using it for Riposte, for example). git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5952 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/java/com/threerings/util/DefaultMap.java | 122 +++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/java/com/threerings/util/DefaultMap.java diff --git a/src/java/com/threerings/util/DefaultMap.java b/src/java/com/threerings/util/DefaultMap.java new file mode 100644 index 000000000..830be63c6 --- /dev/null +++ b/src/java/com/threerings/util/DefaultMap.java @@ -0,0 +1,122 @@ +// +// $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 keys with no value when a value is fetched via the {@link #get} method. 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 extends ForwardingMap +{ + /** Used to create default values. */ + public interface Creator + { + /** 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 DefaultMap newInstanceHashMap (Class clazz) + { + return newInstanceMap(Maps.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 DefaultMap newInstanceMap (Map delegate, Class clazz) + { + final Constructor ctor; + try { + ctor = clazz.getConstructor(); + } catch (NoSuchMethodException nsme) { + throw new IllegalArgumentException(clazz + " must have a no-args constructor."); + } + return newMap(delegate, new Creator() { + 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 DefaultMap newHashMap (Creator creator) + { + return newMap(Maps.newHashMap(), creator); + } + + /** + * Creates a default map backed by the supplied map using the supplied default creator. + */ + public static DefaultMap newMap (Map delegate, Creator creator) + { + return new DefaultMap(delegate, creator); + } + + @Override // from Map + public V get (Object key) + { + @SuppressWarnings("unchecked") K tkey = (K)key; + V value = super.get(key); + // null is a valid value, so we check containsKey() before creating a default + if (value == null && !containsKey(key)) { + put(tkey, value = _creator.create(tkey)); + } + return value; + } + + protected DefaultMap (Map delegate, Creator creator) + { + _delegate = delegate; + _creator = creator; + } + + @Override // from ForwardingMap + protected Map delegate() + { + return _delegate; + } + + protected final Map _delegate; + protected final Creator _creator; +}