View Javadoc
1   package org.eclipse.aether.collection;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  import static java.util.Objects.requireNonNull;
26  
27  import org.eclipse.aether.RepositorySystem;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.graph.DependencyCycle;
30  import org.eclipse.aether.graph.DependencyNode;
31  
32  /**
33   * The result of a dependency collection request.
34   * 
35   * @see RepositorySystem#collectDependencies(RepositorySystemSession, CollectRequest)
36   */
37  public final class CollectResult
38  {
39  
40      private final CollectRequest request;
41  
42      private List<Exception> exceptions;
43  
44      private List<DependencyCycle> cycles;
45  
46      private DependencyNode root;
47  
48      /**
49       * Creates a new result for the specified request.
50       *
51       * @param request The resolution request, must not be {@code null}.
52       */
53      public CollectResult( CollectRequest request )
54      {
55          this.request = requireNonNull( request, "dependency collection request cannot be null" );
56          exceptions = Collections.emptyList();
57          cycles = Collections.emptyList();
58      }
59  
60      /**
61       * Gets the collection request that was made.
62       *
63       * @return The collection request, never {@code null}.
64       */
65      public CollectRequest getRequest()
66      {
67          return request;
68      }
69  
70      /**
71       * Gets the exceptions that occurred while building the dependency graph.
72       * 
73       * @return The exceptions that occurred, never {@code null}.
74       */
75      public List<Exception> getExceptions()
76      {
77          return exceptions;
78      }
79  
80      /**
81       * Records the specified exception while building the dependency graph.
82       * 
83       * @param exception The exception to record, may be {@code null}.
84       * @return This result for chaining, never {@code null}.
85       */
86      public CollectResult addException( Exception exception )
87      {
88          if ( exception != null )
89          {
90              if ( exceptions.isEmpty() )
91              {
92                  exceptions = new ArrayList<Exception>();
93              }
94              exceptions.add( exception );
95          }
96          return this;
97      }
98  
99      /**
100      * Gets the dependency cycles that were encountered while building the dependency graph.
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 cycle.
111      * 
112      * @param cycle The dependency cycle to record, may be {@code null}.
113      * @return This result for chaining, never {@code null}.
114      */
115     public CollectResult addCycle( DependencyCycle cycle )
116     {
117         if ( cycle != null )
118         {
119             if ( cycles.isEmpty() )
120             {
121                 cycles = new ArrayList<DependencyCycle>();
122             }
123             cycles.add( cycle );
124         }
125         return this;
126     }
127 
128     /**
129      * Gets the root node of the dependency graph.
130      * 
131      * @return The root node of the dependency graph or {@code null} if none.
132      */
133     public DependencyNode getRoot()
134     {
135         return root;
136     }
137 
138     /**
139      * Sets the root node of the dependency graph.
140      * 
141      * @param root The root node of the dependency graph, may be {@code null}.
142      * @return This result for chaining, never {@code null}.
143      */
144     public CollectResult setRoot( DependencyNode root )
145     {
146         this.root = root;
147         return this;
148     }
149 
150     @Override
151     public String toString()
152     {
153         return String.valueOf( getRoot() );
154     }
155 
156 }