Interface NamedDomainObjectCollection<T>
- Type Parameters:
T
- The type of objects in this collection.
- All Superinterfaces:
Collection<T>
,DomainObjectCollection<T>
,Iterable<T>
- All Known Subinterfaces:
ArtifactRepositoryContainer
,ArtifactTransformReports
,ArtifactTypeContainer
,AuthenticationContainer
,BuildDashboardReports
,BuildTypeContainer
,CheckstyleReports
,CodeNarcReports
,ConfigurationContainer
,ConfigurationReports
,DependencyReportContainer
,DistributionContainer
,ExtensiblePolymorphicDomainObjectContainer<T>
,FlavorContainer
,IvyConfigurationContainer
,JacocoReportsContainer
,MutableVersionCatalogContainer
,NamedDomainObjectContainer<T>
,NamedDomainObjectList<T>
,NamedDomainObjectSet<T>
,NativeToolChainRegistry
,PlatformContainer
,PmdReports
,PolymorphicDomainObjectContainer<T>
,PrebuiltLibraries
,PublicationContainer
,ReportContainer<T>
,Repositories
,RepositoryHandler
,SoftwareComponentContainer
,SourceSetContainer
,TaskCollection<T>
,TaskContainer
,TestTaskReports
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(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"
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds an object to the collection, if there is no existing object in the collection with the same name.boolean
addAll
(Collection<? extends T> c) Adds any of the given objects to the collection that do not have the same name as any existing element.Adds a rule to this collection.Adds a rule to this collection.Adds a rule to this collection.findByName
(String name) Locates an object by name, returning null if there is no such object.getAsMap()
Returns the objects in this collection, as a map from object name to object instance.Locates an object by name, failing if there is no such object.Locates an object by name, failing if there is no such object.Locates an object by name, failing if there is no such object.Locates an object by name, failing if there is no such object.Provides access to the schema of all created or registered named domain objects in this collection.getNamer()
An object that represents the naming strategy used to name objects of this collection.getNames()
Returns the names of the objects in this collection as a Set of Strings.getRules()
Returns the rules used by this collection.Returns a collection which contains the objects in this collection which meet the given closure specification.Returns a collection which contains the objects in this collection which meet the given specification.Locates a object by name, failing if there is no such object.<S extends T>
NamedDomainObjectProvider<S>Locates a object by name and type, failing if there is no such object.<S extends T>
NamedDomainObjectProvider<S>Locates a object by name and type, failing if there is no such object.Locates a object by name, failing if there is no such object.default NamedDomainObjectCollection<T>
Returns a collection containing the objects with names matching the provided filter.<S extends T>
NamedDomainObjectCollection<S>Returns a collection containing the objects in this collection of the given type.Methods inherited from interface java.util.Collection
clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
Methods inherited from interface org.gradle.api.DomainObjectCollection
addAllLater, addLater, all, all, configureEach, findAll, whenObjectAdded, whenObjectAdded, whenObjectRemoved, whenObjectRemoved, withType, withType
-
Method Details
-
add
Adds an object to the collection, if there is no existing object in the collection with the same name.- Specified by:
add
in interfaceCollection<T>
- Parameters:
e
- the item to add to the collection- Returns:
true
if the item was added, or false if an item with the same name already exists.
-
addAll
Adds any of the given objects to the collection that do not have the same name as any existing element.- Specified by:
addAll
in interfaceCollection<T>
- Parameters:
c
- the items to add to the collection- Returns:
true
if any item was added, or false if all items have non unique names within this collection.
-
getNamer
An object that represents the naming strategy used to name objects of this collection.- Returns:
- Object representing the naming strategy.
-
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).
This operation is eager and will cause all elements of the collection to be realized.
- Returns:
- The objects. Returns an empty map if this collection is empty.
-
getNames
Returns the names of the objects in this collection as a Set of Strings.The set of names is in natural ordering.
This operation is lazy and pending elements of this collection will not be realized.
- Returns:
- The names. Returns an empty set if this collection is empty.
-
findByName
Locates an object by name, returning null if there is no such object.This operation is eager and will cause the returned element, if any, to be realized.
- Parameters:
name
- The object name- Returns:
- The object with the given name, or null if there is no such object in this collection.
-
getByName
Locates an object by name, failing if there is no such object.This operation is eager and will cause the returned element to be realized.
- Parameters:
name
- The object name- Returns:
- The object with the given name. Never returns null.
- Throws:
UnknownDomainObjectException
- when there is no such object in this collection.
-
getByName
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.This operation is eager and will cause the returned element to be realized.
- Parameters:
name
- The object nameconfigureClosure
- The closure to use to configure the object.- Returns:
- The object with the given name, after the configure closure has been applied to it. Never returns null.
- Throws:
UnknownDomainObjectException
- when there is no such object in this collection.
-
getByName
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.This operation is eager and will cause the returned element to be realized.
- Parameters:
name
- The object nameconfigureAction
- The action to use to configure the object.- Returns:
- The object with the given name, after the configure action has been applied to it. Never returns null.
- Throws:
UnknownDomainObjectException
- when there is no such object in this collection.- Since:
- 3.1
-
getAt
Locates an object by name, failing if there is no such object. This method is identical togetByName(String)
. You can call this method in your build script by using the groovy[]
operator.This operation is eager and will cause the returned element to be realized.
- Parameters:
name
- The object name- Returns:
- The object with the given name. Never returns null.
- Throws:
UnknownDomainObjectException
- when there is no such object in this collection.
-
addRule
Adds a rule to this collection. The given rule is invoked when an unknown object is requested by name.- Parameters:
rule
- The rule to add.- Returns:
- The added rule.
-
addRule
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.- Parameters:
description
- The description of the rule.ruleAction
- The closure to execute to apply the rule.- Returns:
- The added rule.
-
addRule
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.- Parameters:
description
- The description of the rule.ruleAction
- The action to execute to apply the rule.- Returns:
- The added rule.
- Since:
- 3.3
-
getRules
Returns the rules used by this collection.- Returns:
- The rules, in the order they will be applied.
-
withType
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.This method is an intermediate operation. It does not change the realized/unrealized state of the elements in the collection.
- Specified by:
withType
in interfaceDomainObjectCollection<T>
- Parameters:
type
- The type of objects to find.- Returns:
- The matching objects. Returns an empty collection if there are no such objects in this collection.
-
named
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 is an intermediate operation. It does not change the realized/unrealized state of the elements in the collection.
- Parameters:
nameFilter
- The specification to test names against.- Returns:
- The collection of objects with names satisfying the filter. Returns an empty collection if there are no such objects in this collection.
- Since:
- 8.6
-
matching
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.This method is an intermediate eager operation. The application of the filter forces the realization of the collection elements.
- Specified by:
matching
in interfaceDomainObjectCollection<T>
- Parameters:
spec
- The specification to use.- Returns:
- The collection of matching objects. Returns an empty collection if there are no such objects in this collection.
-
matching
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.This method is an intermediate eager operation. The application of the filter forces the realization of the collection elements.
- Specified by:
matching
in interfaceDomainObjectCollection<T>
- Parameters:
spec
- The specification to use. The closure gets a collection element as an argument.- Returns:
- The collection of matching objects. Returns an empty collection if there are no such objects in this collection.
-
named
Locates a object by name, failing if there is no such object.This method is lazy and will not cause the returned element to be realized.
- Parameters:
name
- The object's name- Returns:
- A
Provider
that will return the object when queried. The object may be created and configured at this point, if not already. - Throws:
UnknownDomainObjectException
- If a object with the given name is not defined.- Since:
- 4.10
-
named
NamedDomainObjectProvider<T> named(String name, Action<? super T> configurationAction) throws UnknownDomainObjectException Locates a object by name, failing if there is no such object. The given configure action is executed against the object before it is returned from the provider.This method is lazy and will not cause the returned element to be realized.
- Parameters:
name
- The object's name- Returns:
- A
Provider
that will return the object when queried. The object may be created and configured at this point, if not already. - Throws:
UnknownDomainObjectException
- If an object with the given name is not defined.- Since:
- 5.0
-
named
<S extends T> NamedDomainObjectProvider<S> named(String name, Class<S> type) throws UnknownDomainObjectException Locates a object by name and type, failing if there is no such object.This method is lazy and will not cause the returned element to be realized.
- Parameters:
name
- The object's nametype
- The object's type- Returns:
- A
Provider
that will return the object when queried. The object may be created and configured at this point, if not already. - Throws:
UnknownDomainObjectException
- If an object with the given name is not defined.- Since:
- 5.0
-
named
<S extends T> NamedDomainObjectProvider<S> named(String name, Class<S> type, Action<? super S> configurationAction) throws UnknownDomainObjectException Locates a object by name and type, failing if there is no such object. The given configure action is executed against the object before it is returned from the provider.This method is lazy and will not cause the returned element to be realized.
- Parameters:
name
- The object's nametype
- The object's typeconfigurationAction
- The action to use to configure the object.- Returns:
- A
Provider
that will return the object when queried. The object may be created and configured at this point, if not already. - Throws:
UnknownDomainObjectException
- If an object with the given name is not defined.- Since:
- 5.0
-
getCollectionSchema
Provides access to the schema of all created or registered named domain objects in this collection.- Since:
- 4.10
-