001package org.eclipse.aether.resolution;
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 org.eclipse.aether.RepositorySystem;
023import org.eclipse.aether.RequestTrace;
024import org.eclipse.aether.artifact.Artifact;
025import org.eclipse.aether.collection.CollectRequest;
026import org.eclipse.aether.graph.DependencyFilter;
027import org.eclipse.aether.graph.DependencyNode;
028
029/**
030 * A request to resolve transitive dependencies. This request can either be supplied with a {@link CollectRequest} to
031 * calculate the transitive dependencies or with an already resolved dependency graph.
032 * 
033 * @see RepositorySystem#resolveDependencies(org.eclipse.aether.RepositorySystemSession, DependencyRequest)
034 * @see Artifact#getFile()
035 */
036public final class DependencyRequest
037{
038
039    private DependencyNode root;
040
041    private CollectRequest collectRequest;
042
043    private DependencyFilter filter;
044
045    private RequestTrace trace;
046
047    /**
048     * Creates an uninitialized request. Note that either {@link #setRoot(DependencyNode)} or
049     * {@link #setCollectRequest(CollectRequest)} must eventually be called to create a valid request.
050     */
051    public DependencyRequest()
052    {
053        // enables default constructor
054    }
055
056    /**
057     * Creates a request for the specified dependency graph and with the given resolution filter.
058     * 
059     * @param node The root node of the dependency graph whose artifacts should be resolved, may be {@code null}.
060     * @param filter The resolution filter to use, may be {@code null}.
061     */
062    public DependencyRequest( DependencyNode node, DependencyFilter filter )
063    {
064        setRoot( node );
065        setFilter( filter );
066    }
067
068    /**
069     * Creates a request for the specified collect request and with the given resolution filter.
070     * 
071     * @param request The collect request used to calculate the dependency graph whose artifacts should be resolved, may
072     *            be {@code null}.
073     * @param filter The resolution filter to use, may be {@code null}.
074     */
075    public DependencyRequest( CollectRequest request, DependencyFilter filter )
076    {
077        setCollectRequest( request );
078        setFilter( filter );
079    }
080
081    /**
082     * Gets the root node of the dependency graph whose artifacts should be resolved.
083     * 
084     * @return The root node of the dependency graph or {@code null} if none.
085     */
086    public DependencyNode getRoot()
087    {
088        return root;
089    }
090
091    /**
092     * Sets the root node of the dependency graph whose artifacts should be resolved. When this request is processed,
093     * the nodes of the given dependency graph will be updated to refer to the resolved artifacts. Eventually, either
094     * {@link #setRoot(DependencyNode)} or {@link #setCollectRequest(CollectRequest)} must be called to create a valid
095     * request.
096     * 
097     * @param root The root node of the dependency graph, may be {@code null}.
098     * @return This request for chaining, never {@code null}.
099     */
100    public DependencyRequest setRoot( DependencyNode root )
101    {
102        this.root = root;
103        return this;
104    }
105
106    /**
107     * Gets the collect request used to calculate the dependency graph whose artifacts should be resolved.
108     * 
109     * @return The collect request or {@code null} if none.
110     */
111    public CollectRequest getCollectRequest()
112    {
113        return collectRequest;
114    }
115
116    /**
117     * Sets the collect request used to calculate the dependency graph whose artifacts should be resolved. Eventually,
118     * either {@link #setRoot(DependencyNode)} or {@link #setCollectRequest(CollectRequest)} must be called to create a
119     * valid request. If this request is supplied with a dependency node via {@link #setRoot(DependencyNode)}, the
120     * collect request is ignored.
121     * 
122     * @param collectRequest The collect request, may be {@code null}.
123     * @return This request for chaining, never {@code null}.
124     */
125    public DependencyRequest setCollectRequest( CollectRequest collectRequest )
126    {
127        this.collectRequest = collectRequest;
128        return this;
129    }
130
131    /**
132     * Gets the resolution filter used to select which artifacts of the dependency graph should be resolved.
133     * 
134     * @return The resolution filter or {@code null} to resolve all artifacts of the dependency graph.
135     */
136    public DependencyFilter getFilter()
137    {
138        return filter;
139    }
140
141    /**
142     * Sets the resolution filter used to select which artifacts of the dependency graph should be resolved. For
143     * example, use this filter to restrict resolution to dependencies of a certain scope.
144     * 
145     * @param filter The resolution filter, may be {@code null} to resolve all artifacts of the dependency graph.
146     * @return This request for chaining, never {@code null}.
147     */
148    public DependencyRequest setFilter( DependencyFilter filter )
149    {
150        this.filter = filter;
151        return this;
152    }
153
154    /**
155     * Gets the trace information that describes the higher level request/operation in which this request is issued.
156     * 
157     * @return The trace information about the higher level operation or {@code null} if none.
158     */
159    public RequestTrace getTrace()
160    {
161        return trace;
162    }
163
164    /**
165     * Sets the trace information that describes the higher level request/operation in which this request is issued.
166     * 
167     * @param trace The trace information about the higher level operation, may be {@code null}.
168     * @return This request for chaining, never {@code null}.
169     */
170    public DependencyRequest setTrace( RequestTrace trace )
171    {
172        this.trace = trace;
173        return this;
174    }
175
176    @Override
177    public String toString()
178    {
179        if ( root != null )
180        {
181            return String.valueOf( root );
182        }
183        return String.valueOf( collectRequest );
184    }
185
186}