View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.resolution;
20  
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.eclipse.aether.RepositorySystem;
25  import org.eclipse.aether.graph.DependencyCycle;
26  import org.eclipse.aether.graph.DependencyNode;
27  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * The result of a dependency resolution request.
32   *
33   * @see RepositorySystem#resolveDependencies(org.eclipse.aether.RepositorySystemSession, DependencyRequest)
34   */
35  public final class DependencyResult {
36  
37      private final DependencyRequest request;
38  
39      private DependencyNode root;
40  
41      private List<DependencyCycle> cycles;
42  
43      private List<Exception> collectExceptions;
44  
45      private List<ArtifactResult> artifactResults;
46  
47      /**
48       * Creates a new result for the specified request.
49       *
50       * @param request The resolution request, must not be {@code null}.
51       */
52      public DependencyResult(DependencyRequest request) {
53          this.request = requireNonNull(request, "dependency request cannot be null");
54          root = request.getRoot();
55          cycles = Collections.emptyList();
56          collectExceptions = Collections.emptyList();
57          artifactResults = Collections.emptyList();
58      }
59  
60      /**
61       * Gets the resolution request that was made.
62       *
63       * @return The resolution request, never {@code null}.
64       */
65      public DependencyRequest getRequest() {
66          return request;
67      }
68  
69      /**
70       * Gets the root node of the resolved dependency graph. Note that this dependency graph might be
71       * incomplete/unfinished in case of {@link #getCollectExceptions()} indicating errors during its calculation.
72       *
73       * @return The root node of the resolved dependency graph or {@code null} if none.
74       */
75      public DependencyNode getRoot() {
76          return root;
77      }
78  
79      /**
80       * Sets the root node of the resolved dependency graph.
81       *
82       * @param root The root node of the resolved dependency graph, may be {@code null}.
83       * @return This result for chaining, never {@code null}.
84       */
85      public DependencyResult setRoot(DependencyNode root) {
86          this.root = root;
87          return this;
88      }
89  
90      /**
91       * Gets the dependency cycles that were encountered while building the dependency graph. Note that dependency cycles
92       * will only be reported here if the underlying request was created from a
93       * {@link org.eclipse.aether.collection.CollectRequest CollectRequest}. If the underlying {@link DependencyRequest}
94       * was created from an existing dependency graph, information about cycles will not be available in this result.
95       *
96       * @return The dependency cycles in the (raw) graph, never {@code null}.
97       */
98      public List<DependencyCycle> getCycles() {
99          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 }