diff --git a/src/java/com/threerings/util/DefaultMap.java b/src/java/com/threerings/util/DefaultMap.java index 141d2f2e2..3d39e3335 100644 --- a/src/java/com/threerings/util/DefaultMap.java +++ b/src/java/com/threerings/util/DefaultMap.java @@ -31,10 +31,7 @@ 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. + * value for any value retrieved via the {@link #fetch} method which has no entry for that key. */ public class DefaultMap extends ForwardingMap { @@ -60,13 +57,24 @@ public class DefaultMap extends ForwardingMap */ public static DefaultMap newInstanceMap (Map delegate, Class clazz) { + Creator creator = newInstanceCreator(clazz); + return newMap(delegate, creator); + } + + + /** + * Returns a Creator that makes instances of the supplied class (using its no-args + * constructor) as default values. + */ + public static Creator newInstanceCreator (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() { + return new Creator() { public V create (K key) { try { return ctor.newInstance(); @@ -74,7 +82,7 @@ public class DefaultMap extends ForwardingMap throw new RuntimeException(e); } } - }); + }; } /**