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
Locates an object by name and casts it to the expected type T.
Locates an object by name and casts it to the expected type T then configures it.
Locates an object by name and casts it to the expected type.
Locates an object by name and casts it to the expected type then configures it.
Locates an object by name and type, without triggering its creation or configuration, failing if there is no such object.
Kotlin extension function taking kotlin.reflect.KClass for org.gradle.api.NamedDomainObjectCollection.named.
Configures an object by name and type, without triggering its creation or configuration, failing if there is no such object.
Allows a NamedDomainObjectCollection to be used as a property delegate.
Returns a collection containing the objects in this collection of the given type. The returned collection is live, so that when matching objects are later added to this collection, they are also visible in the filtered collection.
Returns a collection containing the objects in this collection of the given type. Equivalent to calling withType(type).all(configureAction)
.
Kotlin extension function taking kotlin.reflect.KClass for org.gradle.api.DomainObjectCollection.withType.
Kotlin extension function taking kotlin.reflect.KClass for org.gradle.api.NamedDomainObjectCollection.withType.