T
- The type of domain objects in this collection.Collection<T>
, DomainObjectCollection<T>
, Iterable<T>
ArtifactRepositoryContainer
, ArtifactTypeContainer
, AuthenticationContainer
, BuildDashboardReports
, BuildTypeContainer
, CheckstyleReports
, CodeNarcReports
, ConfigurationContainer
, DependencyReportContainer
, DistributionContainer
, ExtensiblePolymorphicDomainObjectContainer<T>
, FindBugsReports
, FlavorContainer
, IvyConfigurationContainer
, JacocoReportsContainer
, JDependReports
, NamedDomainObjectContainer<T>
, NamedDomainObjectList<T>
, NamedDomainObjectSet<T>
, NativeToolChainRegistry
, PlatformContainer
, PlayDistributionContainer
, PmdReports
, PolymorphicDomainObjectContainer<T>
, PrebuiltLibraries
, PublicationContainer
, ReportContainer<T>
, Repositories
, RepositoryHandler
, SoftwareComponentContainer
, SourceSetContainer
, TaskCollection<T>
, TaskContainer
, TestTaskReports
public interface NamedDomainObjectCollection<T> extends 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"
Modifier and Type | Method | Description |
---|---|---|
boolean |
add(T e) |
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.
|
Rule |
addRule(String description,
Closure ruleAction) |
Adds a rule to this collection.
|
Rule |
addRule(String description,
Action<String> ruleAction) |
Adds a rule to this collection.
|
Rule |
addRule(Rule rule) |
Adds a rule to this collection.
|
T |
findByName(String name) |
Locates an object by name, returning null if there is no such object.
|
SortedMap<String,T> |
getAsMap() |
Returns the objects in this collection, as a map from object name to object instance.
|
T |
getAt(String name) |
Locates an object by name, failing if there is no such task.
|
T |
getByName(String name) |
Locates an object by name, failing if there is no such object.
|
T |
getByName(String name,
Closure configureClosure) |
Locates an object by name, failing if there is no such object.
|
T |
getByName(String name,
Action<? super T> configureAction) |
Locates an object by name, failing if there is no such object.
|
NamedDomainObjectCollectionSchema |
getCollectionSchema() |
Provides access to the schema of all created or registered named domain objects in this collection.
|
Namer<T> |
getNamer() |
An object that represents the naming strategy used to name objects of this collection.
|
SortedSet<String> |
getNames() |
Returns the names of the objects in this collection as a Set of Strings.
|
List<Rule> |
getRules() |
Returns the rules used by this collection.
|
NamedDomainObjectCollection<T> |
matching(Closure spec) |
Returns a collection which contains the objects in this collection which meet the given closure specification.
|
NamedDomainObjectCollection<T> |
matching(Spec<? super T> spec) |
Returns a collection which contains the objects in this collection which meet the given specification.
|
NamedDomainObjectProvider<T> |
named(String name) |
Locates a object by name, without triggering its creation or configuration, failing if there is no such object.
|
<S extends T> |
named(String name,
Class<S> type) |
Locates a object by name and type, without triggering its creation or configuration, failing if there is no such object.
|
<S extends T> |
named(String name,
Class<S> type,
Action<? super S> configurationAction) |
Locates a object by name and type, without triggering its creation or configuration, failing if there is no such object.
|
NamedDomainObjectProvider<T> |
named(String name,
Action<? super T> configurationAction) |
Locates a object by name, without triggering its creation or configuration, failing if there is no such object.
|
<S extends T> |
withType(Class<S> type) |
Returns a collection containing the objects in this collection of the given type.
|
clear, contains, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray, toArray
addAllLater, addLater, all, all, configureEach, findAll, whenObjectAdded, whenObjectAdded, whenObjectRemoved, whenObjectRemoved, withType, withType
boolean add(T e)
add
in interface Collection<T>
e
- the item to add to the collectiontrue
if the item was added, or false if an item with the same name already exists.boolean addAll(Collection<? extends T> c)
addAll
in interface Collection<T>
c
- the items to add to the collectiontrue
if any item was added, or false if all items have non unique names within this collection.Namer<T> getNamer()
SortedMap<String,T> 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).
SortedSet<String> getNames()
Returns the names of the objects in this collection as a Set of Strings.
The set of names is in natural ordering.
@Nullable T findByName(String name)
name
- The object nameT getByName(String name) throws UnknownDomainObjectException
name
- The object nameUnknownDomainObjectException
- when there is no such object in this collection.T getByName(String name, Closure configureClosure) throws UnknownDomainObjectException
name
- The object nameconfigureClosure
- The closure to use to configure the object.UnknownDomainObjectException
- when there is no such object in this collection.T getByName(String name, Action<? super T> configureAction) throws UnknownDomainObjectException
name
- The object nameconfigureAction
- The action to use to configure the object.UnknownDomainObjectException
- when there is no such object in this collection.T getAt(String name) throws UnknownDomainObjectException
getByName(String)
. You can call this method in your build script by using the groovy []
operator.name
- The object nameUnknownDomainObjectException
- when there is no such object in this collection.Rule addRule(Rule rule)
rule
- The rule to add.Rule addRule(String description, Closure ruleAction)
description
- The description of the rule.ruleAction
- The closure to execute to apply the rule.Rule addRule(String description, Action<String> ruleAction)
description
- The description of the rule.ruleAction
- The action to execute to apply the rule.List<Rule> getRules()
<S extends T> NamedDomainObjectCollection<S> withType(Class<S> type)
withType
in interface DomainObjectCollection<T>
type
- The type of objects to find.NamedDomainObjectCollection<T> matching(Spec<? super T> spec)
matching
in interface DomainObjectCollection<T>
spec
- The specification to use.NamedDomainObjectCollection<T> matching(Closure spec)
matching
in interface DomainObjectCollection<T>
spec
- The specification to use. The closure gets a collection element as an argument.NamedDomainObjectProvider<T> named(String name) throws UnknownDomainObjectException
name
- The object's nameProvider
that will return the object when queried. The object may be created and configured at this point, if not already.UnknownDomainObjectException
- If a object with the given name is not defined.NamedDomainObjectProvider<T> named(String name, Action<? super T> configurationAction) throws UnknownDomainObjectException
name
- The object's nameProvider
that will return the object when queried. The object may be created and configured at this point, if not already.UnknownDomainObjectException
- If a object with the given name is not defined.<S extends T> NamedDomainObjectProvider<S> named(String name, Class<S> type) throws UnknownDomainObjectException
name
- The object's nametype
- The object's typeProvider
that will return the object when queried. The object may be created and configured at this point, if not already.UnknownDomainObjectException
- If a object with the given name is not defined.<S extends T> NamedDomainObjectProvider<S> named(String name, Class<S> type, Action<? super S> configurationAction) throws UnknownDomainObjectException
name
- The object's nametype
- The object's typeconfigurationAction
- The action to use to configure the object.Provider
that will return the object when queried. The object may be created and configured at this point, if not already.UnknownDomainObjectException
- If a object with the given name is not defined.@Internal NamedDomainObjectCollectionSchema getCollectionSchema()