001package org.eclipse.aether.collection;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.List;
025
026import org.eclipse.aether.RepositorySystem;
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.RequestTrace;
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.graph.Dependency;
031import org.eclipse.aether.repository.RemoteRepository;
032
033/**
034 * A request to collect the transitive dependencies and to build a dependency graph from them. There are three ways to
035 * create a dependency graph. First, only the root dependency can be given. Second, a root dependency and direct
036 * dependencies can be specified in which case the specified direct dependencies are merged with the direct dependencies
037 * retrieved from the artifact descriptor of the root dependency. And last, only direct dependencies can be specified in
038 * which case the root node of the resulting graph has no associated dependency.
039 * 
040 * @see RepositorySystem#collectDependencies(RepositorySystemSession, CollectRequest)
041 */
042public final class CollectRequest
043{
044
045    private Artifact rootArtifact;
046
047    private Dependency root;
048
049    private List<Dependency> dependencies = Collections.emptyList();
050
051    private List<Dependency> managedDependencies = Collections.emptyList();
052
053    private List<RemoteRepository> repositories = Collections.emptyList();
054
055    private String context = "";
056
057    private RequestTrace trace;
058
059    /**
060     * Creates an uninitialized request.
061     */
062    public CollectRequest()
063    {
064        // enables default constructor
065    }
066
067    /**
068     * Creates a request with the specified properties.
069     * 
070     * @param root The root dependency whose transitive dependencies should be collected, may be {@code null}.
071     * @param repositories The repositories to use for the collection, may be {@code null}.
072     */
073    public CollectRequest( Dependency root, List<RemoteRepository> repositories )
074    {
075        setRoot( root );
076        setRepositories( repositories );
077    }
078
079    /**
080     * Creates a new request with the specified properties.
081     * 
082     * @param root The root dependency whose transitive dependencies should be collected, may be {@code null}.
083     * @param dependencies The direct dependencies to merge with the direct dependencies from the root dependency's
084     *            artifact descriptor.
085     * @param repositories The repositories to use for the collection, may be {@code null}.
086     */
087    public CollectRequest( Dependency root, List<Dependency> dependencies, List<RemoteRepository> repositories )
088    {
089        setRoot( root );
090        setDependencies( dependencies );
091        setRepositories( repositories );
092    }
093
094    /**
095     * Creates a new request with the specified properties.
096     * 
097     * @param dependencies The direct dependencies of some imaginary root, may be {@code null}.
098     * @param managedDependencies The dependency management information to apply to the transitive dependencies, may be
099     *            {@code null}.
100     * @param repositories The repositories to use for the collection, may be {@code null}.
101     */
102    public CollectRequest( List<Dependency> dependencies, List<Dependency> managedDependencies,
103                           List<RemoteRepository> repositories )
104    {
105        setDependencies( dependencies );
106        setManagedDependencies( managedDependencies );
107        setRepositories( repositories );
108    }
109
110    /**
111     * Gets the root artifact for the dependency graph.
112     * 
113     * @return The root artifact for the dependency graph or {@code null} if none.
114     */
115    public Artifact getRootArtifact()
116    {
117        return rootArtifact;
118    }
119
120    /**
121     * Sets the root artifact for the dependency graph. This must not be confused with {@link #setRoot(Dependency)}: The
122     * root <em>dependency</em>, like any other specified dependency, will be subject to dependency
123     * collection/resolution, i.e. should have an artifact descriptor and a corresponding artifact file. The root
124     * <em>artifact</em> on the other hand is only used as a label for the root node of the graph in case no root
125     * dependency was specified. As such, the configured root artifact is ignored if {@link #getRoot()} does not return
126     * {@code null}.
127     * 
128     * @param rootArtifact The root artifact for the dependency graph, may be {@code null}.
129     * @return This request for chaining, never {@code null}.
130     */
131    public CollectRequest setRootArtifact( Artifact rootArtifact )
132    {
133        this.rootArtifact = rootArtifact;
134        return this;
135    }
136
137    /**
138     * Gets the root dependency of the graph.
139     * 
140     * @return The root dependency of the graph or {@code null} if none.
141     */
142    public Dependency getRoot()
143    {
144        return root;
145    }
146
147    /**
148     * Sets the root dependency of the graph.
149     * 
150     * @param root The root dependency of the graph, may be {@code null}.
151     * @return This request for chaining, never {@code null}.
152     */
153    public CollectRequest setRoot( Dependency root )
154    {
155        this.root = root;
156        return this;
157    }
158
159    /**
160     * Gets the direct dependencies.
161     * 
162     * @return The direct dependencies, never {@code null}.
163     */
164    public List<Dependency> getDependencies()
165    {
166        return dependencies;
167    }
168
169    /**
170     * Sets the direct dependencies. If both a root dependency and direct dependencies are given in the request, the
171     * direct dependencies from the request will be merged with the direct dependencies from the root dependency's
172     * artifact descriptor, giving higher priority to the dependencies from the request.
173     * 
174     * @param dependencies The direct dependencies, may be {@code null}.
175     * @return This request for chaining, never {@code null}.
176     */
177    public CollectRequest setDependencies( List<Dependency> dependencies )
178    {
179        if ( dependencies == null )
180        {
181            this.dependencies = Collections.emptyList();
182        }
183        else
184        {
185            this.dependencies = dependencies;
186        }
187        return this;
188    }
189
190    /**
191     * Adds the specified direct dependency.
192     * 
193     * @param dependency The dependency to add, may be {@code null}.
194     * @return This request for chaining, never {@code null}.
195     */
196    public CollectRequest addDependency( Dependency dependency )
197    {
198        if ( dependency != null )
199        {
200            if ( this.dependencies.isEmpty() )
201            {
202                this.dependencies = new ArrayList<Dependency>();
203            }
204            this.dependencies.add( dependency );
205        }
206        return this;
207    }
208
209    /**
210     * Gets the dependency management to apply to transitive dependencies.
211     * 
212     * @return The dependency management to apply to transitive dependencies, never {@code null}.
213     */
214    public List<Dependency> getManagedDependencies()
215    {
216        return managedDependencies;
217    }
218
219    /**
220     * Sets the dependency management to apply to transitive dependencies. To clarify, this management does not apply to
221     * the direct dependencies of the root node.
222     * 
223     * @param managedDependencies The dependency management, may be {@code null}.
224     * @return This request for chaining, never {@code null}.
225     */
226    public CollectRequest setManagedDependencies( List<Dependency> managedDependencies )
227    {
228        if ( managedDependencies == null )
229        {
230            this.managedDependencies = Collections.emptyList();
231        }
232        else
233        {
234            this.managedDependencies = managedDependencies;
235        }
236        return this;
237    }
238
239    /**
240     * Adds the specified managed dependency.
241     * 
242     * @param managedDependency The managed dependency to add, may be {@code null}.
243     * @return This request for chaining, never {@code null}.
244     */
245    public CollectRequest addManagedDependency( Dependency managedDependency )
246    {
247        if ( managedDependency != null )
248        {
249            if ( this.managedDependencies.isEmpty() )
250            {
251                this.managedDependencies = new ArrayList<Dependency>();
252            }
253            this.managedDependencies.add( managedDependency );
254        }
255        return this;
256    }
257
258    /**
259     * Gets the repositories to use for the collection.
260     * 
261     * @return The repositories to use for the collection, never {@code null}.
262     */
263    public List<RemoteRepository> getRepositories()
264    {
265        return repositories;
266    }
267
268    /**
269     * Sets the repositories to use for the collection.
270     * 
271     * @param repositories The repositories to use for the collection, may be {@code null}.
272     * @return This request for chaining, never {@code null}.
273     */
274    public CollectRequest setRepositories( List<RemoteRepository> repositories )
275    {
276        if ( repositories == null )
277        {
278            this.repositories = Collections.emptyList();
279        }
280        else
281        {
282            this.repositories = repositories;
283        }
284        return this;
285    }
286
287    /**
288     * Adds the specified repository for collection.
289     * 
290     * @param repository The repository to collect dependency information from, may be {@code null}.
291     * @return This request for chaining, never {@code null}.
292     */
293    public CollectRequest addRepository( RemoteRepository repository )
294    {
295        if ( repository != null )
296        {
297            if ( this.repositories.isEmpty() )
298            {
299                this.repositories = new ArrayList<RemoteRepository>();
300            }
301            this.repositories.add( repository );
302        }
303        return this;
304    }
305
306    /**
307     * Gets the context in which this request is made.
308     * 
309     * @return The context, never {@code null}.
310     */
311    public String getRequestContext()
312    {
313        return context;
314    }
315
316    /**
317     * Sets the context in which this request is made.
318     * 
319     * @param context The context, may be {@code null}.
320     * @return This request for chaining, never {@code null}.
321     */
322    public CollectRequest setRequestContext( String context )
323    {
324        this.context = ( context != null ) ? context : "";
325        return this;
326    }
327
328    /**
329     * Gets the trace information that describes the higher level request/operation in which this request is issued.
330     * 
331     * @return The trace information about the higher level operation or {@code null} if none.
332     */
333    public RequestTrace getTrace()
334    {
335        return trace;
336    }
337
338    /**
339     * Sets the trace information that describes the higher level request/operation in which this request is issued.
340     * 
341     * @param trace The trace information about the higher level operation, may be {@code null}.
342     * @return This request for chaining, never {@code null}.
343     */
344    public CollectRequest setTrace( RequestTrace trace )
345    {
346        this.trace = trace;
347        return this;
348    }
349
350    @Override
351    public String toString()
352    {
353        return getRoot() + " -> " + getDependencies() + " < " + getRepositories();
354    }
355
356}