Nothing earth shattering to see here: I answered this question for someone and wanted to share it.
How do I use a variable to name a map key when using ‘object’ notation instead of .put()?
Wrap the name of the key in parenthesis. It’ll become an expression and the result will be stored as the key:
Where it’s -really- handy
Imagine you have a “IProcessor” interface that does something (e.g. format, mangle, turn into pig-latin) to a given type of input. You’d like to build a factory that’ll serve up the appropriate processor for the type.
Why? Because we’re Java folks, and building factories is what we do. Inside it, we can form a registry that relates class types to ‘processors,’ using it as the core of the factory:
Further, because we’re smart Java folks we know we can use the above to configure the factory via Spring, in resources.groovy, instead of hard-coding this stuff into true source code.