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