Turns out we want to highlight the difference between normal getting and
getting that adds the default value to the map. I opted for fetch() which at least means something close to get() and can be documented as also inserting the default value. I also considered obtain() and acquire() but those both felt like they had something to do with locks. apply() seemed too functional. wangle() seemed like a strong contender for a while. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5965 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -102,14 +102,17 @@ public class DefaultMap<K, V> extends ForwardingMap<K, V>
|
|||||||
_creator = creator;
|
_creator = creator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override // from Map
|
/**
|
||||||
public V get (Object key)
|
* 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)
|
||||||
{
|
{
|
||||||
@SuppressWarnings("unchecked") K tkey = (K)key;
|
V value = get(key);
|
||||||
V value = super.get(key);
|
|
||||||
// null is a valid value, so we check containsKey() before creating a default
|
// null is a valid value, so we check containsKey() before creating a default
|
||||||
if (value == null && !containsKey(key)) {
|
if (value == null && !containsKey(key)) {
|
||||||
put(tkey, value = _creator.create(tkey));
|
put(key, value = _creator.create(key));
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user