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