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 java.util.Collections;
023import java.util.List;
024import static java.util.Objects.requireNonNull;
025
026import org.eclipse.aether.RepositorySystem;
027import org.eclipse.aether.graph.DependencyCycle;
028import org.eclipse.aether.graph.DependencyNode;
029
030/**
031 * The result of a dependency resolution request.
032 * 
033 * @see RepositorySystem#resolveDependencies(org.eclipse.aether.RepositorySystemSession, DependencyRequest)
034 */
035public final class DependencyResult
036{
037
038    private final DependencyRequest request;
039
040    private DependencyNode root;
041
042    private List<DependencyCycle> cycles;
043
044    private List<Exception> collectExceptions;
045
046    private List<ArtifactResult> artifactResults;
047
048    /**
049     * Creates a new result for the specified request.
050     *
051     * @param request The resolution request, must not be {@code null}.
052     */
053    public DependencyResult( DependencyRequest request )
054    {
055        this.request = requireNonNull( request, "dependency request cannot be null" );
056        root = request.getRoot();
057        cycles = Collections.emptyList();
058        collectExceptions = Collections.emptyList();
059        artifactResults = Collections.emptyList();
060    }
061
062    /**
063     * Gets the resolution request that was made.
064     * 
065     * @return The resolution request, never {@code null}.
066     */
067    public DependencyRequest getRequest()
068    {
069        return request;
070    }
071
072    /**
073     * Gets the root node of the resolved dependency graph. Note that this dependency graph might be
074     * incomplete/unfinished in case of {@link #getCollectExceptions()} indicating errors during its calculation.
075     * 
076     * @return The root node of the resolved dependency graph or {@code null} if none.
077     */
078    public DependencyNode getRoot()
079    {
080        return root;
081    }
082
083    /**
084     * Sets the root node of the resolved dependency graph.
085     * 
086     * @param root The root node of the resolved dependency graph, may be {@code null}.
087     * @return This result for chaining, never {@code null}.
088     */
089    public DependencyResult setRoot( DependencyNode root )
090    {
091        this.root = root;
092        return this;
093    }
094
095    /**
096     * Gets the dependency cycles that were encountered while building the dependency graph. Note that dependency cycles
097     * will only be reported here if the underlying request was created from a
098     * {@link org.eclipse.aether.collection.CollectRequest CollectRequest}. If the underlying {@link DependencyRequest}
099     * was created from an existing dependency graph, information about cycles will not be available in this result.
100     * 
101     * @return The dependency cycles in the (raw) graph, never {@code null}.
102     */
103    public List<DependencyCycle> getCycles()
104    {
105        return cycles;
106    }
107
108    /**
109     * Records the specified dependency cycles while building the dependency graph.
110     * 
111     * @param cycles The dependency cycles to record, may be {@code null}.
112     * @return This result for chaining, never {@code null}.
113     */
114    public DependencyResult setCycles( List<DependencyCycle> cycles )
115    {
116        if ( cycles == null )
117        {
118            this.cycles = Collections.emptyList();
119        }
120        else
121        {
122            this.cycles = cycles;
123        }
124        return this;
125    }
126
127    /**
128     * Gets the exceptions that occurred while building the dependency graph.
129     * 
130     * @return The exceptions that occurred, never {@code null}.
131     */
132    public List<Exception> getCollectExceptions()
133    {
134        return collectExceptions;
135    }
136
137    /**
138     * Records the specified exceptions while building the dependency graph.
139     * 
140     * @param exceptions The exceptions to record, may be {@code null}.
141     * @return This result for chaining, never {@code null}.
142     */
143    public DependencyResult setCollectExceptions( List<Exception> exceptions )
144    {
145        if ( exceptions == null )
146        {
147            this.collectExceptions = Collections.emptyList();
148        }
149        else
150        {
151            this.collectExceptions = exceptions;
152        }
153        return this;
154    }
155
156    /**
157     * Gets the resolution results for the dependency artifacts that matched {@link DependencyRequest#getFilter()}.
158     * 
159     * @return The resolution results for the dependency artifacts, never {@code null}.
160     */
161    public List<ArtifactResult> getArtifactResults()
162    {
163        return artifactResults;
164    }
165
166    /**
167     * Sets the resolution results for the artifacts that matched {@link DependencyRequest#getFilter()}.
168     * 
169     * @param results The resolution results for the artifacts, may be {@code null}.
170     * @return This result for chaining, never {@code null}.
171     */
172    public DependencyResult setArtifactResults( List<ArtifactResult> results )
173    {
174        if ( results == null )
175        {
176            this.artifactResults = Collections.emptyList();
177        }
178        else
179        {
180            this.artifactResults = results;
181        }
182        return this;
183    }
184
185    @Override
186    public String toString()
187    {
188        return String.valueOf( artifactResults );
189    }
190
191}