Overview | Package | Class | Deprecated | Index | Help
| Groovy Documentation
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | METHOD | DETAIL: FIELD | METHOD

## org.gradle.api
[Java] Interface NamedDomainObjectCollection

    java.lang.Iterable
      org.gradle.api.DomainObjectCollection
          org.gradle.api.NamedDomainObjectCollection

All Superinterfaces:
  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"

Parameters:
  `` - The type of domain objects in this collection.

Method Summary
`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(java.util.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(java.lang.String description, groovy.lang.Closure ruleAction)` Adds a rule to this collection.
`T` | `findByName(java.lang.String name)` Locates an object by name, returning null if there is no such object.
`java.util.SortedMap` | `getAsMap()` Returns the objects in this collection, as a map from object name to object instance.
`T` | `getAt(java.lang.String name)` Locates an object by name, failing if there is no such task.
`T` | `getByName(java.lang.String name)` Locates an object by name, failing if there is no such object.
`T` | `getByName(java.lang.String name, groovy.lang.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.
`java.util.List` | `getRules()` Returns the rules used by this collection.
`NamedDomainObjectCollection` | `matching(Spec spec)` {@inheritDoc}
`NamedDomainObjectCollection` | `matching(groovy.lang.Closure spec)` {@inheritDoc}
`NamedDomainObjectCollection` | `withType(java.lang.Class type)` {@inheritDoc}

Methods inherited from interface DomainObjectCollection
all, all, allObjects, allObjects, findAll, findAll, getAll, matching, matching, whenObjectAdded, whenObjectAdded, whenObjectRemoved, whenObjectRemoved, withType, withType, withType

Methods inherited from interface java.util.Collection
java.util.Collection#add(java.lang.Object), java.util.Collection#equals(java.lang.Object), java.util.Collection#hashCode(), java.util.Collection#clear(), java.util.Collection#size(), java.util.Collection#isEmpty(), java.util.Collection#contains(java.lang.Object), java.util.Collection#addAll(java.util.Collection), java.util.Collection#toArray(), java.util.Collection#toArray([Ljava.lang.Object;), java.util.Collection#iterator(), java.util.Collection#remove(java.lang.Object), java.util.Collection#removeAll(java.util.Collection), java.util.Collection#containsAll(java.util.Collection), java.util.Collection#retainAll(java.util.Collection)

Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()

Method Detail

### add

    public boolean add(T e)

  Adds an object to the collection, if there is no existing object in the collection with the same name.
  Parameters:
    `e` - the item to add to the collection
  Returns:
    `true` if the item was added, or {@code} false if an item with the same name already exists.

### addAll

    public boolean addAll(java.util.Collection c)

  Adds any of the given objects to the collection that do not have the same name as any existing element.
  Parameters:
    `c` - the items to add to the collection
  Returns:
    `true` if any item was added, or {@code} false if all items have non unique names within this collection.

### addRule

    public Rule addRule(Rule rule)

  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

    public Rule addRule(java.lang.String description, groovy.lang.Closure ruleAction)

  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.

### findByName

    public T findByName(java.lang.String name)

  Locates an object by name, returning null if there is no such object.
  Parameters:
    `name` - The object name
  Returns:
    The object with the given name, or null if there is no such object in this collection.

### getAsMap

    public java.util.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).

  Returns:
    The objects. Returns an empty map if this collection is empty.

### getAt

    public T getAt(java.lang.String name)

  Locates an object by name, failing if there is no such task. This method is identical to getByName(String). You can call this method in your build script by using the groovy `[]` operator.
  throws:
    UnknownDomainObjectException when there is no such object in this collection.
  Parameters:
    `name` - The object name
  Returns:
    The object with the given name. Never returns null.

### getByName

    public T getByName(java.lang.String name)

  Locates an object by name, failing if there is no such object.
  throws:
    UnknownDomainObjectException when there is no such object in this collection.
  Parameters:
    `name` - The object name
  Returns:
    The object with the given name. Never returns null.

### getByName

    public T getByName(java.lang.String name, groovy.lang.Closure configureClosure)

  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 it's delegate.
  throws:
    UnknownDomainObjectException when there is no such object in this collection.
  Parameters:
    `name` - The object name
    `configureClosure` - 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.

### getNamer

    public Namer getNamer()

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

### getRules

    public java.util.List getRules()

  Returns the rules used by this collection.
  Returns:
    The rules, in the order they will be applied.

### matching

    public NamedDomainObjectCollection matching(Spec spec)

  {@inheritDoc}

### matching

    public NamedDomainObjectCollection matching(groovy.lang.Closure spec)

  {@inheritDoc}

### withType

    public NamedDomainObjectCollection withType(java.lang.Class type)

  {@inheritDoc}

Groovy Documentation