Package org.gradle.api

# Interface NamedDomainObjectContainer<T>

Type Parameters:
  `T` - The type of objects in this container.

All Superinterfaces:
  `Collection<T>`, `Configurable<NamedDomainObjectContainer<T>>`, `DomainObjectCollection<T>`, `DomainObjectSet<T>`, `Iterable<T>`, `NamedDomainObjectCollection<T>`, `NamedDomainObjectSet<T>`, `Set<T>`

All Known Subinterfaces:
  `ArtifactTypeContainer`, `AuthenticationContainer`, `BuildTypeContainer`, `ConfigurationContainer`, `DistributionContainer`, `ExtensiblePolymorphicDomainObjectContainer<T>`, `FlavorContainer`, `IvyConfigurationContainer`, `MutableVersionCatalogContainer`, `NativeToolChainRegistry`, `PlatformContainer`, `PolymorphicDomainObjectContainer<T>`, `PublicationContainer`, `Repositories`, `SoftwareComponentContainer`, `SourceSetContainer`, `TaskContainer`

@ManagedType public interface NamedDomainObjectContainer<T> extends NamedDomainObjectSet<T>, Configurable<NamedDomainObjectContainer<T>>

A named domain object container is a specialization of `NamedDomainObjectSet` that adds the ability to create instances of the element type.

Implementations may use different strategies for creating new object instances.

Note that a container is an implementation of `SortedSet`, which means that the container is guaranteed to only contain elements with unique names within this container. Furthermore, items are ordered by their name.

See Also:
  - `Create an instance of this as a managed property (preferred).`
  - `Create an instance of this manually.`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `NamedDomainObjectContainer<T>`
  `configure(Closure configureClosure)`
  Allows the container to be configured, creating missing objects as they are referenced.
  `T`
  `create(String name)`
  Creates a new item with the given name, adding it to this container.
  `T`
  `create(String name, Closure configureClosure)`
  Creates a new item with the given name, adding it to this container, then configuring it with the given closure.
  `T`
  `create(String name, Action<? super T> configureAction)`
  Creates a new item with the given name, adding it to this container, then configuring it with the given action.
  `T`
  `maybeCreate(String name)`
  Looks for an item with the given name, creating and adding it to this container if it does not exist.
  `NamedDomainObjectProvider<T>`
  `register(String name)`
  Defines a new object, which will be created when it is required.
  `NamedDomainObjectProvider<T>`
  `register(String name, Action<? super T> configurationAction)`
  Defines a new object, which will be created and configured when it is required.

  ### Methods inherited from interface java.util.Collection

  `parallelStream, removeIf, stream, toArray`

  ### Methods inherited from interface org.gradle.api.DomainObjectCollection

  `addAllLater, addLater, all, all, areChangesAllowed, configureEach, disallowChanges, whenObjectAdded, whenObjectAdded, whenObjectRemoved, whenObjectRemoved, withType, withType`

  ### Methods inherited from interface java.lang.Iterable

  `forEach`

  ### Methods inherited from interface org.gradle.api.NamedDomainObjectCollection

  `add, addAll, addRule, addRule, addRule, findByName, getAsMap, getAt, getByName, getByName, getByName, getCollectionSchema, getNamer, getNames, getRules, named, named, named, named`

  ### Methods inherited from interface org.gradle.api.NamedDomainObjectSet

  `findAll, matching, matching, named, withType`

  ### Methods inherited from interface java.util.Set

  `add, addAll, clear, contains, containsAll, equals, hashCode, isEmpty, iterator, remove, removeAll, retainAll, size, spliterator, toArray, toArray`

- ## Method Details

  - ### create

    T create(String name) throws InvalidUserDataException
    Creates a new item with the given name, adding it to this container.

    This operation is eager, the element is realized before being returned.

    Parameters:
      `name` - The name to assign to the created object
    Returns:
      The created object. Never null.
    Throws:
      `InvalidUserDataException` - if an object with the given name already exists in this container.

  - ### maybeCreate

    T maybeCreate(String name)
    Looks for an item with the given name, creating and adding it to this container if it does not exist.

    This operation is eager, the element is realized before being returned.

    Parameters:
      `name` - The name to find or assign to the created object
    Returns:
      The found or created object. Never null.

  - ### create

    T create(String name, Closure configureClosure) throws InvalidUserDataException
    Creates a new item with the given name, adding it to this container, then configuring it with the given closure.

    This operation is eager, the element is realized before being returned.

    Parameters:
      `name` - The name to assign to the created object
      `configureClosure` - The closure to configure the created object with
    Returns:
      The created object. Never null.
    Throws:
      `InvalidUserDataException` - if an object with the given name already exists in this container.

  - ### create

    T create(String name, Action<? super T> configureAction) throws InvalidUserDataException
    Creates a new item with the given name, adding it to this container, then configuring it with the given action.

    This operation is eager, the element is realized before being returned.

    Parameters:
      `name` - The name to assign to the created object
      `configureAction` - The action to configure the created object with
    Returns:
      The created object. Never null.
    Throws:
      `InvalidUserDataException` - if an object with the given name already exists in this container.

  - ### configure

    NamedDomainObjectContainer<T> configure(Closure configureClosure)

    Allows the container to be configured, creating missing objects as they are referenced.

    TODO: example usage

    Specified by:
      `configure` in interface `Configurable<T>`
    Parameters:
      `configureClosure` - The closure to configure this container with
    Returns:
      This.

  - ### register

    NamedDomainObjectProvider<T> register(String name, Action<? super T> configurationAction) throws InvalidUserDataException
    Defines a new object, which will be created and configured when it is required. An object is 'required' when the object is located using query methods such as `NamedDomainObjectCollection.getByName(java.lang.String)` or when `Provider.get()` is called on the return value of this method.

    It is generally more efficient to use this method instead of `create(java.lang.String, org.gradle.api.Action)` or `create(java.lang.String)`, as those methods will eagerly create and configure the object, regardless of whether that object is required for the current build or not. This method, on the other hand, will defer creation and configuration until required.

    This operation is lazy, the returned element is NOT realized. A `lazy wrapper` is returned, allowing to continue to use it with other lazy APIs.

    Parameters:
      `name` - The name of the object.
      `configurationAction` - The action to run to configure the object. This action runs when the object is required.
    Returns:
      A `Provider` whose value will be the object, when queried.
    Throws:
      `InvalidUserDataException` - If a object with the given name already exists in this project.
    Since:
      4.10

  - ### register

    NamedDomainObjectProvider<T> register(String name) throws InvalidUserDataException
    Defines a new object, which will be created when it is required. A object is 'required' when the object is located using query methods such as `NamedDomainObjectCollection.getByName(java.lang.String)` or when `Provider.get()` is called on the return value of this method.

    It is generally more efficient to use this method instead of `create(java.lang.String)`, as that method will eagerly create the object, regardless of whether that object is required for the current build or not. This method, on the other hand, will defer creation until required.

    This operation is lazy, the returned element is NOT realized. A `lazy wrapper` is returned, allowing to continue to use it with other lazy APIs.

    Parameters:
      `name` - The name of the object.
    Returns:
      A `Provider` whose value will be the object, when queried.
    Throws:
      `InvalidUserDataException` - If a object with the given name already exists in this project.
    Since:
      4.10