# NamedDomainObjectCollection

API Documentation: | `NamedDomainObjectCollection`

Known Subtypes: |
`NamedDomainObjectSet`

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 `NamedDomainObjectCollection.getByName(java.lang.String, groovy.lang.Closure)`. 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
`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.
`getByName(name, configureAction)` | Locates an object by name, failing if there is no such object. The given configure action is executed against the object before it is returned from this method.
`named(name)` | Locates a object by name, failing if there is no such object.
`named(name, type)` | Locates a object by name and type, failing if there is no such object.
`named(name, type, configurationAction)` | Locates a object by name and type, failing if there is no such object. The given configure action is executed against the object before it is returned from the provider.
`named(name, configurationAction)` | Locates a object by name, failing if there is no such object. The given configure action is executed against the object before it is returned from the provider.
`named(nameFilter)` | Incubating Returns a collection containing the objects with names matching the provided filter. The returned collection is live, so that when matching objects are added to this collection, they are also visible in the filtered collection.

### Script blocks

No script blocks

### Method details

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

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

This operation is eager and will cause the returned element to be realized.

#### `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.

This operation is eager and will cause the returned element to be realized.

#### `T` `getByName`(`String` name, ``Action`<? super `T`>` configureAction)

Locates an object by name, failing if there is no such object. The given configure action is executed against the object before it is returned from this method.

This operation is eager and will cause the returned element to be realized.

#### ``NamedDomainObjectProvider`<`T`>` `named`(`String` name)

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

This method is lazy and will not cause the returned element to be realized.

#### ``NamedDomainObjectProvider`<`S`>` `named`(`String` name, ``Class`<`S`>` type)

Locates a object by name and type, failing if there is no such object.

This method is lazy and will not cause the returned element to be realized.

#### ``NamedDomainObjectProvider`<`S`>` `named`(`String` name, ``Class`<`S`>` type, ``Action`<? super `S`>` configurationAction)

Locates a object by name and type, failing if there is no such object. The given configure action is executed against the object before it is returned from the provider.

This method is lazy and will not cause the returned element to be realized.

#### ``NamedDomainObjectProvider`<`T`>` `named`(`String` name, ``Action`<? super `T`>` configurationAction)

Locates a object by name, failing if there is no such object. The given configure action is executed against the object before it is returned from the provider.

This method is lazy and will not cause the returned element to be realized.

#### ``NamedDomainObjectCollection`<`T`>` `named`(``Spec`<`String`>` nameFilter)

Note: This method is incubating and may change in a future version of Gradle.

Returns a collection containing the objects with names matching the provided filter. The returned collection is live, so that when matching objects are added to this collection, they are also visible in the filtered collection.

This method is an intermediate operation. It does not change the realized/unrealized state of the elements in the collection.