|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | METHOD | DETAIL: FIELD | METHOD |
org.gradle.api.NamedDomainObjectCollection java.lang.Iterable org.gradle.api.DomainObjectCollection
public interface NamedDomainObjectCollection extends DomainObjectCollection
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"
- The type of domain objects in this collection.Method Summary | |
---|---|
boolean
|
add(Object e)
Adds an object to the collection, if there is no existing object in the collection with the same name. |
boolean
|
addAll(Collection c)
Adds any of the given objects to the collection that do not have the same name as any existing element. |
Rule
|
addRule(Rule rule)
Adds a rule to this collection. |
Rule
|
addRule(String description, Closure ruleAction)
Adds a rule to this collection. |
Object
|
findByName(String name)
Locates an object by name, returning null if there is no such object. |
SortedMap
|
getAsMap()
|
Object
|
getAt(String name)
Locates an object by name, failing if there is no such task. |
Object
|
getByName(String name)
Locates an object by name, failing if there is no such object. |
Object
|
getByName(String name, Closure configureClosure)
Locates an object by name, failing if there is no such object. |
Namer
|
getNamer()
An object that represents the naming strategy used to name objects of this collection. |
SortedSet
|
getNames()
|
List
|
getRules()
Returns the rules used by this collection. |
NamedDomainObjectCollection
|
matching(Spec spec)
{@inheritDoc} |
NamedDomainObjectCollection
|
matching(Closure spec)
{@inheritDoc} |
NamedDomainObjectCollection
|
withType(Class type)
{@inheritDoc} |
Methods inherited from interface DomainObjectCollection | |
---|---|
all, all, findAll, matching, matching, whenObjectAdded, whenObjectAdded, whenObjectRemoved, whenObjectRemoved, withType, withType, withType |
Methods inherited from interface Collection | |
---|---|
add, equals, hashCode, clear, contains, isEmpty, size, toArray, toArray, addAll, iterator, remove, containsAll, removeAll, retainAll |
Method Detail |
---|
public boolean add(Object e)
e
- the item to add to the collection
public boolean addAll(Collection c)
c
- the items to add to the collection
public Rule addRule(Rule rule)
rule
- The rule to add.
public Rule addRule(String description, Closure ruleAction)
description
- The description of the rule.ruleAction
- The closure to execute to apply the rule.
public Object findByName(String name)
name
- The object name
public SortedMap getAsMap()
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).
public Object getAt(String name)
name
- The object name
public Object getByName(String name)
name
- The object name
public Object getByName(String name, Closure configureClosure)
name
- The object nameconfigureClosure
- The closure to use to configure the object.
public Namer getNamer()
public SortedSet getNames()
Returns the names of the objects in this collection as a Set of Strings.
The set of names is in natural ordering.
public List getRules()
public NamedDomainObjectCollection matching(Spec spec)
public NamedDomainObjectCollection matching(Closure spec)
public NamedDomainObjectCollection withType(Class type)
Gradle API 1.12