Named Domain Object Collection
A NamedDomainObjectCollection represents a collection of objects that have an inherent, constant, name.
Objects to be added to a named domain object collection must implement equals() in such a way that no two objects with different names are considered equal. That is, all equality tests must consider the name as an equality key. Behavior is undefined if two objects with different names are considered equal by their equals() implementation.
All implementations must guarantee that all elements in the collection are uniquely named. That is, an attempt to add an object with a name equal to the name of any existing object in the collection will fail. Implementations may choose to simply return false from add(T) or to throw an exception.
Objects in the collection are accessible as read-only properties, using the name of the object as the property name. For example (assuming the 'name' property provides the object name):
books.add(new Book(name: "gradle", title: null))
books.gradle.title = "Gradle in Action"
A dynamic method is added for each object which takes a configuration closure. This is equivalent to calling getByName. For example:
books.add(new Book(name: "gradle", title: null))
books.gradle {
title = "Gradle in Action"
}
You can also use the [] operator to access the objects of a collection by name. For example:
books.add(new Book(name: "gradle", title: null))
books['gradle'].title = "Gradle in Action"
Rule objects can be attached to the collection in order to respond to requests for objects by name where no object with name exists in the collection. This mechanism can be used to create objects on demand. For example:
books.addRule('create any') { books.add(new Book(name: "gradle", title: null)) }
books.gradle.name == "gradle"
Parameters
The type of objects in this collection.
Inheritors
Functions
Action to be executed when an object is added to this collection.Action to be executed when an object is removed from this collection.