You may need to associate a piece of information with a node without persisting it to disk as part of the model.
For example, you might want to store the result of evaluating a node with an interpreter, or a string to filter a large list of children in the editor.
You could reach for a static Map<SNode, MyInformation>
and it could work as long as you use a hash map with weak
keys, to avoid memory leaks. For example, a java.util.WeakHashMap
would do.
There is a better solution, however: each SNode
object manages a hash map of user objects which, as the name
suggests, can contain arbitrary objects added by the user. The map is accessed via two methods,
SNode#getUserObject(Object key)
and SNode#putUserObject(Object key, Object value)
. User objects free you from the
necessity of using weak hash maps, and may be slightly faster.
One caveat: although any Object
can serve as a key to the map, it is preferable to stick to plain strings. Since MPS
reloads classes as you recompile your code, a key object created before a reload will not equal the same key object
created after the reload.