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

MethodDescription
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, without triggering its creation or configuration, failing if there is no such object.

named(name, type)

Locates a object by name and type, without triggering its creation or configuration, failing if there is no such object.

named(name, type, configurationAction)

Locates a object by name and type, without triggering its creation or configuration, 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, without triggering its creation or configuration, 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. This method will NOT cause any pending objects in this container to be realized.

Script blocks

No script blocks

Method details

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.

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.

Locates a object by name, without triggering its creation or configuration, failing if there is no such object.

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

Locates a object by name and type, without triggering its creation or configuration, failing if there is no such object.

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

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

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

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

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 will NOT cause any pending objects in this container to be realized.