DomainObjectCollection (Gradle API 9.7.0)
Description: declaration: package: org.gradle.api, interface: DomainObjectCollection

# Interface DomainObjectCollection<T>

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

All Superinterfaces:
  `Collection<T>, Iterable<T>`

All Known Subinterfaces:
  `ArtifactRepositoryContainer, ArtifactTransformReports, ArtifactTypeContainer, AuthenticationContainer, BinaryTasksCollection, BuildDashboardReports, BuildTypeContainer, CheckstyleReports, CodeNarcReports, ConfigurationContainer, ConfigurationReports, DependencyConstraintSet, DependencyReportContainer, DependencySet, DistributionContainer, DomainObjectSet<T>, ExtensiblePolymorphicDomainObjectContainer<T>, FlavorContainer, IvyArtifactSet, IvyConfigurationContainer, JacocoReportsContainer, MavenArtifactSet, MutableVersionCatalogContainer, NamedDomainObjectCollection<T>, NamedDomainObjectContainer<T>, NamedDomainObjectList<T>, NamedDomainObjectSet<T>, NativeExecutableBinarySpec.TasksCollection, NativeTestSuiteBinarySpec.TasksCollection, NativeToolChainRegistry, PlatformContainer, PluginCollection<T>, PluginContainer, PmdReports, PolymorphicDomainObjectContainer<T>, PrebuiltLibraries, ProjectSourceSet, PublicationContainer, PublishArtifactSet, ReportContainer<T>, Repositories, RepositoryHandler, SharedLibraryBinarySpec.TasksCollection, SoftwareComponentContainer, SourceSetContainer, StaticLibraryBinarySpec.TasksCollection, TaskCollection<T>, TaskContainer, TestSuiteTaskCollection, TestTaskReports`

public interface DomainObjectCollection<T> extends Collection<T>

A `DomainObjectCollection` is a specialised `Collection` that adds the ability to receive modification notifications and use live filtered sub collections.

The filtered collections returned by the filtering methods, such as `matching(Closure)`, return collections that are live. That is, they reflect changes made to the source collection that they were created from. This is true for filtered collections made from filtered collections etc.

You can also add actions that are executed as elements are added to the collection. Actions added to filtered collections will be fired if an addition/removal occurs for the source collection that matches the filter.

`DomainObjectCollection` instances are not thread-safe and undefined behavior may result from the invocation of any method on a collection that is being mutated by another thread; this includes direct invocations, passing the collection to a method that might perform invocations, and using an existing iterator to examine the collection.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addAllLater(Provider<? extends Iterable<T>> provider)`
  Adds elements to this collection, given a `Provider` of `Iterable` that will provide the elements when required.
  `void`
  `addLater(Provider<? extends T> provider)`
  Adds an element to this collection, given a `Provider` that will provide the element when required.
  `void`
  `all(Closure action)`
  Executes the given closure against all objects in this collection, and any objects subsequently added to this collection.
  `void`
  `all(Action<? super T> action)`
  Executes the given action against all objects in this collection, and any objects subsequently added to this collection.
  `void`
  `configureEach(Action<? super T> action)`
  Configures each element in this collection using the given action, as each element is required.
  `default void`
  `disallowChanges()`
  Disallows further structural modifications to this collection.
  `Collection<T>`
  `findAll(Closure spec)`
  Deprecated.
  Use `matching(Spec)` or Collection APIs from the JDK.
  `default Provider<? extends Collection<T>>`
  `getElements()`
  Return a provider containing all elements added to this collection.
  `DomainObjectCollection<T>`
  `matching(Closure spec)`
  Returns a collection which contains the objects in this collection which meet the given closure specification.
  `DomainObjectCollection<T>`
  `matching(Spec<? super T> spec)`
  Returns a collection which contains the objects in this collection which meet the given specification.
  `void`
  `whenObjectAdded(Closure action)`
  Adds a closure to be called when an object is added to this collection.
  `Action<? super T>`
  `whenObjectAdded(Action<? super T> action)`
  Adds an `Action` to be executed when an object is added to this collection.
  `void`
  `whenObjectRemoved(Closure action)`
  Adds a closure to be called when an object is removed from this collection.
  `Action<? super T>`
  `whenObjectRemoved(Action<? super T> action)`
  Adds an `Action` to be executed when an object is removed from this collection.
  `<S extends T>
  DomainObjectCollection<S>`
  `withType(Class<S> type)`
  Returns a collection containing the objects in this collection of the given type.
  `<S extends T>
  DomainObjectCollection<S>`
  `withType(Class<S> type, Closure configureClosure)`
  Returns a collection containing the objects in this collection of the given type.
  `<S extends T>
  DomainObjectCollection<S>`
  `withType(Class<S> type, Action<? super S> configureAction)`
  Returns a collection containing the objects in this collection of the given type.

  ### Methods inherited from interface Collection

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

  ### Methods inherited from interface Iterable

  `forEach`

- ## Method Details

  - ### addLater

    void addLater(Provider<? extends T> provider)
    Adds an element to this collection, given a `Provider` that will provide the element when required.
    Parameters:
      `provider` - A `Provider` that can provide the element when required.
    Since:
      4.8

  - ### addAllLater

    void addAllLater(Provider<? extends Iterable<T>> provider)
    Adds elements to this collection, given a `Provider` of `Iterable` that will provide the elements when required.
    Parameters:
      `provider` - A `Provider` of `Iterable` that can provide the elements when required.
    Since:
      5.0

  - ### getElements

    @Incubating default Provider<? extends Collection<T>> getElements()
    Return a provider containing all elements added to this collection.

    The returned provider retains the build dependencies of providers added by `addLater(Provider)` or `addAllLater(Provider)`.

    The value produced by a provider with build dependencies, by definition, is not valid until those build dependencies are executed. For example, if a task produces an output file, the value of a provider returning that file is not valid until the task executes.

    Querying the contents of this provider before the build dependencies of providers added by `addLater(Provider)` and `addAllLater(Provider)` will result in undefined behavior. In general, the returned provider should only be used to wire values between tasks. Avoid directly calling `Provider.get()` or any other method that realizes the value of the returned provier.

    Since:
      9.7.0

  - ### withType

    <S extends T> DomainObjectCollection<S> withType(Class<S> type)
    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.

    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.

  - ### withType

    <S extends T> DomainObjectCollection<S> withType(Class<S> type, Action<? super S> configureAction)
    Returns a collection containing the objects in this collection of the given type. Equivalent to calling `withType(type).all(configureAction)`

    This method is a terminal eager operation. It will cause the realization of all elements of this collection.

    Parameters:
      `type` - The type of objects to find.
      `configureAction` - The action to execute for each object in the resulting collection.
    Returns:
      The matching objects. Returns an empty collection if there are no such objects in this collection.

  - ### withType

    <S extends T> DomainObjectCollection<S> withType(Class<S> type, @DelegatesTo(genericTypeIndex=0) Closure configureClosure)
    Returns a collection containing the objects in this collection of the given type. Equivalent to calling `withType(type).all(configureClosure)`.

    This method is a terminal eager operation. It will cause the realization of all elements of this collection.

    Parameters:
      `type` - The type of objects to find.
      `configureClosure` - The closure to execute for each object in the resulting collection.
    Returns:
      The matching objects. Returns an empty collection if there are no such objects in this collection.

  - ### matching

    DomainObjectCollection<T> matching(Spec<? super T> spec)
    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.

    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

    DomainObjectCollection<T> matching(Closure spec)
    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.

    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.

  - ### whenObjectAdded

    Action<? super T> whenObjectAdded(Action<? super T> action)
    Adds an `Action` to be executed when an object is added to this collection.

    Like `all(Action)`, this method will cause all objects in this container to be realized.

    Parameters:
      `action` - The action to be executed
    Returns:
      the supplied action

  - ### whenObjectAdded

    void whenObjectAdded(Closure action)
    Adds a closure to be called when an object is added to this collection. The newly added object is passed to the closure as the parameter.

    Like `all(Action)`, this method will cause all objects in this container to be realized.

    Parameters:
      `action` - The closure to be called
    See Also:
      - `whenObjectAdded(Action)`

  - ### whenObjectRemoved

    Action<? super T> whenObjectRemoved(Action<? super T> action)
    Adds an `Action` to be executed when an object is removed from this collection.

    This method will only be invoked for elements effectively added to the collection. Elements that are `addLater(Provider)` will be seen by this action only if they have been observed as part of the collection. All elements affected by this action will be realized.

    Parameters:
      `action` - The action to be executed
    Returns:
      the supplied action

  - ### whenObjectRemoved

    void whenObjectRemoved(Closure action)
    Adds a closure to be called when an object is removed from this collection. The removed object is passed to the closure as the parameter.

    This method will only be invoked for elements effectively added to the collection. Elements that are `addLater(Provider)` will be seen by this action only if they have been observed as part of the collection. All elements affected by this action will be realized.

    Parameters:
      `action` - The closure to be called

  - ### all

    void all(Action<? super T> action)
    Executes the given action against all objects in this collection, and any objects subsequently added to this collection.

    This method is a terminal eager operation. It will cause the realization of all elements of this collection.

    Parameters:
      `action` - The action to be executed

  - ### all

    void all(Closure action)
    Executes the given closure against all objects in this collection, and any objects subsequently added to this collection. The object is passed to the closure as the closure delegate. Alternatively, it is also passed as a parameter.

    This method is a terminal eager operation. It will cause the realization of all elements of this collection.

    Parameters:
      `action` - The action to be executed

  - ### configureEach

    void configureEach(Action<? super T> action)
    Configures each element in this collection using the given action, as each element is required. Actions are run in the order added.

    This method is a terminal lazy operation. Lazy collection elements will not be realized.

    Parameters:
      `action` - A `Action` that can configure the element when required.
    Since:
      4.9

  - ### findAll

    @Deprecated Collection<T> findAll(Closure spec)
    Deprecated.
    Use `matching(Spec)` or Collection APIs from the JDK.
    Returns a collection which contains the objects in this collection which meet the given closure specification.

    This method is an eager operation. The application of the filter forces the realization of all of the underlying collection elements.

    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.

  - ### disallowChanges

    @Incubating default void disallowChanges()
    Disallows further structural modifications to this collection.

    After this method is called, any attempt to add or remove elements — including via `Collection.add(E)`, `addLater(Provider)`, `addAllLater(Provider)`, `Collection.remove(Object)`, `Collection.clear()`, etc. — will throw `IllegalStateException`.

    This method does not force the realization of any pending (lazy) elements. Pending elements registered via `addLater(Provider)` or `NamedDomainObjectContainer.register(String, Action)` remain lazy; they will still be realized when first accessed. This method only prevents new elements from being added or existing elements from being removed.

    Calling this method more than once has no effect.

    Throws:
      `IllegalStateException` - if called from within a lazy configuration action (e.g., inside a `configureEach(Action)` callback)
    Since:
      9.5.0