001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.resolution;
020
021import java.util.Collections;
022import java.util.List;
023
024import org.eclipse.aether.RepositorySystem;
025import org.eclipse.aether.graph.DependencyCycle;
026import org.eclipse.aether.graph.DependencyNode;
027
028import static java.util.Objects.requireNonNull;
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    private final DependencyRequest request;
038
039    private DependencyNode root;
040
041    private List<DependencyCycle> cycles;
042
043    private List<Exception> collectExceptions;
044
045    private List<DependencyNode> dependencyNodeResults;
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        this.request = requireNonNull(request, "dependency request cannot be null");
056        root = request.getRoot();
057        cycles = Collections.emptyList();
058        collectExceptions = Collections.emptyList();
059        this.dependencyNodeResults = 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        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        return root;
080    }
081
082    /**
083     * Sets the root node of the resolved dependency graph.
084     *
085     * @param root The root node of the resolved dependency graph, may be {@code null}.
086     * @return This result for chaining, never {@code null}.
087     */
088    public DependencyResult setRoot(DependencyNode root) {
089        this.root = root;
090        return this;
091    }
092
093    /**
094     * Gets the dependency cycles that were encountered while building the dependency graph. Note that dependency cycles
095     * will only be reported here if the underlying request was created from a
096     * {@link org.eclipse.aether.collection.CollectRequest CollectRequest}. If the underlying {@link DependencyRequest}
097     * was created from an existing dependency graph, information about cycles will not be available in this result.
098     *
099     * @return The dependency cycles in the (raw) graph, never {@code null}.
100     */
101    public List<DependencyCycle> getCycles() {
102        return cycles;
103    }
104
105    /**
106     * Records the specified dependency cycles while building the dependency graph.
107     *
108     * @param cycles The dependency cycles to record, may be {@code null}.
109     * @return This result for chaining, never {@code null}.
110     */
111    public DependencyResult setCycles(List<DependencyCycle> cycles) {
112        if (cycles == null) {
113            this.cycles = Collections.emptyList();
114        } else {
115            this.cycles = cycles;
116        }
117        return this;
118    }
119
120    /**
121     * Gets the exceptions that occurred while building the dependency graph.
122     *
123     * @return The exceptions that occurred, never {@code null}.
124     */
125    public List<Exception> getCollectExceptions() {
126        return collectExceptions;
127    }
128
129    /**
130     * Records the specified exceptions while building the dependency graph.
131     *
132     * @param exceptions The exceptions to record, may be {@code null}.
133     * @return This result for chaining, never {@code null}.
134     */
135    public DependencyResult setCollectExceptions(List<Exception> exceptions) {
136        if (exceptions == null) {
137            this.collectExceptions = Collections.emptyList();
138        } else {
139            this.collectExceptions = exceptions;
140        }
141        return this;
142    }
143
144    /**
145     * Gets the resolution results for the dependency nodes that matched {@link DependencyRequest#getFilter()}.
146     *
147     * @return The resolution results for the dependency nodes, never {@code null}.
148     * @since 2.0.0
149     */
150    public List<DependencyNode> getDependencyNodeResults() {
151        return dependencyNodeResults;
152    }
153
154    /**
155     * Sets the resolution results for the dependency nodes that matched {@link DependencyRequest#getFilter()}.
156     *
157     * @param results The resolution results for the dependency nodes, may be {@code null}.
158     * @return This result for chaining, never {@code null}.
159     * @since 2.0.0
160     */
161    public DependencyResult setDependencyNodeResults(List<DependencyNode> results) {
162        if (results == null) {
163            this.dependencyNodeResults = Collections.emptyList();
164        } else {
165            this.dependencyNodeResults = results;
166        }
167        return this;
168    }
169
170    /**
171     * Gets the resolution results for the dependency artifacts that matched {@link DependencyRequest#getFilter()}.
172     *
173     * @return The resolution results for the dependency artifacts, never {@code null}.
174     */
175    public List<ArtifactResult> getArtifactResults() {
176        return artifactResults;
177    }
178
179    /**
180     * Sets the resolution results for the artifacts that matched {@link DependencyRequest#getFilter()}.
181     *
182     * @param results The resolution results for the artifacts, may be {@code null}.
183     * @return This result for chaining, never {@code null}.
184     */
185    public DependencyResult setArtifactResults(List<ArtifactResult> results) {
186        if (results == null) {
187            this.artifactResults = Collections.emptyList();
188        } else {
189            this.artifactResults = results;
190        }
191        return this;
192    }
193
194    @Override
195    public String toString() {
196        return String.valueOf(artifactResults);
197    }
198}