class ObjectExtensions extends Object
Extensions to the base Object class.
| Constructor and description |
|---|
ObjectExtensions() |
| Type Params | Return Type | Name and description |
|---|---|---|
<T> |
static T |
getOrCreate(Object self, String key, Closure<T> create)Retrieves a value from any object that supports the subscript operators (square bracket notation, implemented by the getAt/putAt
methods). |
Retrieves a value from any object that supports the subscript operators
(square bracket notation, implemented by the getAt/putAt
methods). If there is no value for the given key, then the create
closure is executed and its return value is set on the object for the key,
and returned to the calling code.
assert !myObject['someProperty']
def result = map.getOrCreate('someProperty') { ->
return 13
}
assert result == 13
assert myObject['someProperty'] == 13
key.