All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.gradle.api.NamedDomainObjectCollection Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gradle.api;

import groovy.lang.Closure;
import org.gradle.api.provider.Provider;
import org.gradle.api.specs.Spec;
import org.gradle.api.tasks.Internal;

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.SortedMap;
import java.util.SortedSet;

/**
 * 

A {@code 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 {@code 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 {@code 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 {@code 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 * {@link #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 {@code []} 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"
 * 
* *

{@link 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"
 * 
* * @param The type of domain objects in this collection. */ public interface NamedDomainObjectCollection extends DomainObjectCollection { /** * Adds an object to the collection, if there is no existing object in the collection with the same name. * * @param e the item to add to the collection * @return {@code true} if the item was added, or {@code} false if an item with the same name already exists. */ boolean add(T e); /** * Adds any of the given objects to the collection that do not have the same name as any existing element. * * @param c the items to add to the collection * @return {@code true} if any item was added, or {@code} false if all items have non unique names within this collection. */ boolean addAll(Collection c); /** * An object that represents the naming strategy used to name objects of this collection. * * @return Object representing the naming strategy. */ Namer getNamer(); /** *

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).

* * @return The objects. Returns an empty map if this collection is empty. */ SortedMap getAsMap(); /** *

Returns the names of the objects in this collection as a Set of Strings.

* *

The set of names is in natural ordering.

* * @return The names. Returns an empty set if this collection is empty. */ SortedSet getNames(); /** * Locates an object by name, returning null if there is no such object. * * @param name The object name * @return The object with the given name, or null if there is no such object in this collection. */ @Nullable T findByName(String name); /** * Locates an object by name, failing if there is no such object. * * @param name The object name * @return The object with the given name. Never returns null. * @throws UnknownDomainObjectException when there is no such object in this collection. */ T getByName(String name) throws UnknownDomainObjectException; /** * 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. * * @param name The object name * @param configureClosure The closure to use to configure the object. * @return 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. */ T getByName(String name, Closure configureClosure) throws UnknownDomainObjectException; /** * 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. * * @param name The object name * @param configureAction The action to use to configure the object. * @return 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 */ T getByName(String name, Action configureAction) throws UnknownDomainObjectException; /** * Locates an object by name, failing if there is no such task. This method is identical to {@link * #getByName(String)}. You can call this method in your build script by using the groovy {@code []} operator. * * @param name The object name * @return The object with the given name. Never returns null. * @throws UnknownDomainObjectException when there is no such object in this collection. */ T getAt(String name) throws UnknownDomainObjectException; /** * Adds a rule to this collection. The given rule is invoked when an unknown object is requested by name. * * @param rule The rule to add. * @return The added rule. */ Rule addRule(Rule rule); /** * 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. * * @param description The description of the rule. * @param ruleAction The closure to execute to apply the rule. * @return The added rule. */ Rule addRule(String description, Closure ruleAction); /** * 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. * * @param description The description of the rule. * @param ruleAction The action to execute to apply the rule. * @return The added rule. * @since 3.3 */ Rule addRule(String description, Action ruleAction); /** * Returns the rules used by this collection. * * @return The rules, in the order they will be applied. */ List getRules(); /** * {@inheritDoc} */ NamedDomainObjectCollection withType(Class type); /** * {@inheritDoc} */ NamedDomainObjectCollection matching(Spec spec); /** * {@inheritDoc} */ NamedDomainObjectCollection matching(Closure spec); /** * Locates a object by name, without triggering its creation or configuration, failing if there is no such object. * * @param name The object's name * @return A {@link 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 */ NamedDomainObjectProvider named(String name) throws UnknownDomainObjectException; /** * Locates a object by name, without triggering its creation or configuration, failing if there is no such object. * The given configure action is executed against the object before it is returned from the provider. * * @param name The object's name * @return A {@link 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 5.0 */ NamedDomainObjectProvider named(String name, Action configurationAction) throws UnknownDomainObjectException; /** * Locates a object by name and type, without triggering its creation or configuration, failing if there is no such object. * * @param name The object's name * @param type The object's type * @return A {@link 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 5.0 */ NamedDomainObjectProvider named(String name, Class type) throws UnknownDomainObjectException; /** * Locates a object by name and type, without triggering its creation or configuration, failing if there is no such object. * The given configure action is executed against the object before it is returned from the provider. * * @param name The object's name * @param type The object's type * @param configurationAction The action to use to configure the object. * @return A {@link 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 5.0 */ NamedDomainObjectProvider named(String name, Class type, Action configurationAction) throws UnknownDomainObjectException; /** * Provides access to the schema of all created or registered named domain objects in this collection. * * @since 4.10 */ @Internal NamedDomainObjectCollectionSchema getCollectionSchema(); }