# NamedDomainObjectCollection

API Documentation: | `NamedDomainObjectCollection`

Known Subtypes: |
`NamedDomainObjectSet`

A `NamedDomainObjectCollection` represents a collection of domain 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 `NamedDomainObjectCollection.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"

### Properties

No properties

### Methods

Method | Description
`getAt(name)` | Locates an object by name, failing if there is no such task. This method is identical to `NamedDomainObjectCollection.getByName()`. You can call this method in your build script by using the groovy `[]` operator.
`getByName(name)` | Locates an object by name, failing if there is no such object.
`getByName(name, configureClosure)` | Locates an object by name, failing if there is no such object. The given configure closure is executed against the object before it is returned from this method. The object is passed to the closure as its delegate.

### Script blocks

No script blocks

### Method details

#### `T` `getAt`(`String` name)

Locates an object by name, failing if there is no such task. This method is identical to `NamedDomainObjectCollection.getByName()`. You can call this method in your build script by using the groovy `[]` operator.

#### `T` `getByName`(`String` name)

Locates an object by name, failing if there is no such object.

#### `T` `getByName`(`String` name, `Closure` configureClosure)

Locates an object by name, failing if there is no such object. The given configure closure is executed against the object before it is returned from this method. The object is passed to the closure as its delegate.