api / org.gradle.api / NamedDomainObjectCollection

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" 

Parameters

- The type of domain objects in this collection.

Functions

add

abstract fun add(element: T): Boolean

Adds an object to the collection, if there is no existing object in the collection with the same name.

addAll

abstract fun addAll(elements: Collection<T>): Boolean

Adds any of the given objects to the collection that do not have the same name as any existing element.

addRule

abstract fun addRule(rule: Rule): Rule

Adds a rule to this collection. The given rule is invoked when an unknown object is requested by name.

abstract fun addRule(description: String, ruleAction: Closure<Any>): Rule

Adds a rule to this collection. The given closure is executed when an unknown object is requested by name. The requested name is passed to the closure as a parameter.

abstract fun addRule(description: String, ruleAction: Action<String>): Rule

Adds a rule to this collection. The given action is executed when an unknown object is requested by name. The requested name is passed to the action.

findByName

abstract fun findByName(name: String): T

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

getAsMap

abstract fun getAsMap(): SortedMap<String, T>

Returns the objects in this collection, as a map from object name to object instance.

The map is ordered by the natural ordering of the object names (i.e. keys).

getAt

abstract fun getAt(name: String): T

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

getByName

abstract fun getByName(name: String): T

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

abstract fun getByName(name: String, configureClosure: Closure<Any>): T

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.

abstract fun getByName(name: String, configureAction: Action<in T>): T

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.

getNamer

abstract fun getNamer(): Namer<T>

An object that represents the naming strategy used to name objects of this collection.

getNames

abstract fun getNames(): SortedSet<String>

Returns the names of the objects in this collection as a Set of Strings.

The set of names is in natural ordering.

getRules

abstract fun getRules(): MutableList<Rule>

Returns the rules used by this collection.

matching

abstract fun matching(spec: Spec<in T>): NamedDomainObjectCollection<T>
abstract fun matching(spec: Closure<Any>): NamedDomainObjectCollection<T>

{@inheritDoc}

withType

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

{@inheritDoc}

Inherited 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.

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>, 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 Properties

getting

val <T : Any, U : NamedDomainObjectCollection<in T>> U.getting: U

Idiomatic way of referring to an existing element in a collection via a delegate property.

Extension Functions

get

operator fun <T : Any> NamedDomainObjectCollection<T>.get(name: String): T

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

getByName

fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String): T

Locates an object by name and casts it to the expected type T.

fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, type: KClass<T>): T

Locates an object by name and casts it to the expected type.

fun <T : Any> NamedDomainObjectCollection<out Any>.getByName(name: String, configure: T.() -> Unit): T

Locates an object by name and casts it to the expected type T then configures it.

getValue

operator fun <T : Any, U : T> NamedDomainObjectCollection<T>.getValue(thisRef: Any?, property: KProperty<*>): U

Allows a NamedDomainObjectCollection to be used as a property delegate.

getting

fun <T : Any, U : NamedDomainObjectCollection<T>> U.getting(configuration: T.() -> Unit): NamedDomainObjectCollectionDelegateProvider<T>

Idiomatic way of referring and configuring an existing element in a collection via a delegate property.

Inheritors

NamedDomainObjectList

interface NamedDomainObjectList<T : Any> : NamedDomainObjectCollection<T>, MutableList<T>

A specialisation of org.gradle.api.NamedDomainObjectCollection that also implements java.util.List.

All object equality is determined in terms of object names. That is, calling remove() with an object that is NOT equal to an existing object in terms of equals, but IS in terms of name equality will result in the existing collection item with the equal name being removed.

NamedDomainObjectSet

interface NamedDomainObjectSet<T : Any> : NamedDomainObjectCollection<T>, MutableSet<T>

A specialisation of NamedDomainObjectCollection that also implements Set and orders objects by their inherent name.

All object equality is determined in terms of object names. That is, calling remove() with an object that is NOT equal to an existing object in terms of equals, but IS in terms of name equality will result in the existing collection item with the equal name being removed.