api / org.gradle.api / DomainObjectCollection

DomainObjectCollection

interface DomainObjectCollection<T : Any> : MutableCollection<T>

A DomainObjectCollection is a specialised Collection that adds the ability to receive modification notifications and use live filtered sub collections.

The filtered collections returned by the filtering methods, such as #matching(Closure), return collections that are live. That is, they reflect changes made to the source collection that they were created from. This is true for filtered collections made from filtered collections etc.

You can also add actions which are executed as elements are added to the collection. Actions added to filtered collections will be fired if an addition/removal occurs for the source collection that matches the filter.

Parameters

- The type of domain objects in this collection.

Functions

addLater

abstract fun addLater(provider: Provider<out T>): Unit

Adds an element to this collection, given a Provider that will provide the element when required. Note: this method currently has a placeholder name and will almost certainly be renamed.

all

abstract fun all(action: Action<in T>): Unit

Executes the given action against all objects in this collection, and any objects subsequently added to this collection.

abstract fun all(action: Closure<Any>): Unit

Executes the given closure against all objects in this collection, and any objects subsequently added to this collection. The object is passed to the closure as the closure delegate. Alternatively, it is also passed as a parameter.

configureEach

abstract fun configureEach(action: Action<in T>): Unit

Configures each element in this collection using the given action, as each element is required. Actions are run in the order added.

findAll

abstract fun findAll(spec: Closure<Any>): MutableCollection<T>

Returns a collection which contains the objects in this collection which meet the given closure specification.

matching

abstract fun matching(spec: Spec<in T>): DomainObjectCollection<T>

Returns a collection which contains the objects in this collection which meet the given specification. The returned collection is live, so that when matching objects are added to this collection, they are also visible in the filtered collection.

abstract fun matching(spec: Closure<Any>): DomainObjectCollection<T>

Returns a collection which contains the objects in this collection which meet the given closure specification. The returned collection is live, so that when matching objects are added to this collection, they are also visible in the filtered collection.

whenObjectAdded

abstract fun whenObjectAdded(action: Action<in T>): Action<in T>

Adds an Action to be executed when an object is added to this collection.

abstract fun whenObjectAdded(action: Closure<Any>): Unit

Adds a closure to be called when an object is added to this collection. The newly added object is passed to the closure as the parameter.

whenObjectRemoved

abstract fun whenObjectRemoved(action: Action<in T>): Action<in T>

Adds an Action to be executed when an object is removed from this collection.

abstract fun whenObjectRemoved(action: Closure<Any>): Unit

Adds a closure to be called when an object is removed from this collection. The removed object is passed to the closure as the parameter.

withType

abstract fun <S : T> withType(type: Class<S>): DomainObjectCollection<S>

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.

abstract fun <S : T> withType(type: Class<S>, configureAction: Action<in S>): DomainObjectCollection<S>

Returns a collection containing the objects in this collection of the given type. Equivalent to calling withType(type).all(configureAction)

abstract fun <S : T> withType(type: Class<S>, configureClosure: Closure<Any>): DomainObjectCollection<S>

Returns a collection containing the objects in this collection of the given type. Equivalent to calling withType(type).all(configureClosure).

Extension Functions

withType

fun <S : Any> DomainObjectCollection<in S>.withType(configuration: S.() -> Unit): DomainObjectCollection<S>

Returns a collection containing the objects in this collection of the given type. Equivalent to calling {@code withType(type).all(configureAction)}

fun <S : Any> DomainObjectCollection<in S>.withType(): DomainObjectCollection<S>

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.

Inheritors

DomainObjectSet

interface DomainObjectSet<T : Any> : DomainObjectCollection<T>, MutableSet<T>

A DomainObjectSet is a specialisation of DomainObjectCollection that guarantees Set semantics.

NamedDomainObjectCollection

interface NamedDomainObjectCollection<T : Any> : DomainObjectCollection<T>

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 #getByName(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"