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.graph.DependencyCycle;
29  import org.eclipse.aether.graph.DependencyNode;
30  
31  /**
32   * The result of a dependency collection request.
33   * 
34   * @see RepositorySystem#collectDependencies(org.eclipse.aether.RepositorySystemSession, CollectRequest)
35   */
36  public final class CollectResult
37  {
38  
39      private final CollectRequest request;
40  
41      private List<Exception> exceptions;
42  
43      private List<DependencyCycle> cycles;
44  
45      private DependencyNode root;
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 CollectResult( CollectRequest request )
53      {
54          this.request = requireNonNull( request, "dependency collection request cannot be null" );
55          exceptions = Collections.emptyList();
56          cycles = Collections.emptyList();
57      }
58  
59      /**
60       * Gets the collection request that was made.
61       *
62       * @return The collection request, never {@code null}.
63       */
64      public CollectRequest getRequest()
65      {
66          return request;
67      }
68  
69      /**
70       * Gets the exceptions that occurred while building the dependency graph.
71       * 
72       * @return The exceptions that occurred, never {@code null}.
73       */
74      public List<Exception> getExceptions()
75      {
76          return exceptions;
77      }
78  
79      /**
80       * Records the specified exception while building the dependency graph.
81       * 
82       * @param exception The exception to record, may be {@code null}.
83       * @return This result for chaining, never {@code null}.
84       */
85      public CollectResult addException( Exception exception )
86      {
87          if ( exception != null )
88          {
89              if ( exceptions.isEmpty() )
90              {
91                  exceptions = new ArrayList<>();
92              }
93              exceptions.add( exception );
94          }
95          return this;
96      }
97  
98      /**
99       * Gets the dependency cycles that were encountered while building the dependency graph.
100      * 
101      * @return The dependency cycles in the (raw) graph, never {@code null}.
102      */
103     public List<DependencyCycle> getCycles()
104     {
105         return cycles;
106     }
107 
108     /**
109      * Records the specified dependency cycle.
110      * 
111      * @param cycle The dependency cycle to record, may be {@code null}.
112      * @return This result for chaining, never {@code null}.
113      */
114     public CollectResult addCycle( DependencyCycle cycle )
115     {
116         if ( cycle != null )
117         {
118             if ( cycles.isEmpty() )
119             {
120                 cycles = new ArrayList<>();
121             }
122             cycles.add( cycle );
123         }
124         return this;
125     }
126 
127     /**
128      * Gets the root node of the dependency graph.
129      * 
130      * @return The root node of the dependency graph or {@code null} if none.
131      */
132     public DependencyNode getRoot()
133     {
134         return root;
135     }
136 
137     /**
138      * Sets the root node of the dependency graph.
139      * 
140      * @param root The root node of the dependency graph, may be {@code null}.
141      * @return This result for chaining, never {@code null}.
142      */
143     public CollectResult setRoot( DependencyNode root )
144     {
145         this.root = root;
146         return this;
147     }
148 
149     @Override
150     public String toString()
151     {
152         return String.valueOf( getRoot() );
153     }
154 
155 }