Re: Seeking a smarter idiom




"Eric Sosman" <esosman@xxxxxxxxxxxxxxxxxxx> wrote in message
news:NvidnWO0rsgCSbzYnZ2dnUVZ_o6dnZ2d@xxxxxxxxxxxxxx

Here's something I find myself doing a lot:

Map<KeyType,MutableType> map = ...;
...
KeyType key = ...;
MutableType value = map.get(key);
if (value == null) {
value = new MutableType();
map.put(key, value);
}
value.update(...info...);

<snip problem details>

I use this form alot for data structures that build themselves as data
arrive and when you can't really know the keys in advance. The issue is that
when you have the map-specific insertion information, you can't invoke the
value-type constructor. But if you could extend Map just a little...

Map.Entry mapEntry = map.getOrCreateEntry (key);
if (mapEntry.getValue () == null) {
mapEntry.setValue (new MutableType ())
}
mapEntry.getValue().update (...info...);

Map.Entry is an interface so it could contain map-specific insertion
information and wouldn't have to be a real node in the map structure.

Matt Humphrey matth@xxxxxxxxxxxxxx http://www.iviz.com/


.