View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2013 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether.collection;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.List;
16  
17  import org.eclipse.aether.RepositorySystem;
18  import org.eclipse.aether.RepositorySystemSession;
19  import org.eclipse.aether.graph.DependencyCycle;
20  import org.eclipse.aether.graph.DependencyNode;
21  
22  /**
23   * The result of a dependency collection request.
24   * 
25   * @see RepositorySystem#collectDependencies(RepositorySystemSession, CollectRequest)
26   */
27  public final class CollectResult
28  {
29  
30      private final CollectRequest request;
31  
32      private List<Exception> exceptions;
33  
34      private List<DependencyCycle> cycles;
35  
36      private DependencyNode root;
37  
38      /**
39       * Creates a new result for the specified request.
40       * 
41       * @param request The resolution request, must not be {@code null}.
42       */
43      public CollectResult( CollectRequest request )
44      {
45          if ( request == null )
46          {
47              throw new IllegalArgumentException( "dependency collection request has not been specified" );
48          }
49          this.request = request;
50          exceptions = Collections.emptyList();
51          cycles = Collections.emptyList();
52      }
53  
54      /**
55       * Gets the collection request that was made.
56       * 
57       * @return The collection request, never {@code null}.
58       */
59      public CollectRequest getRequest()
60      {
61          return request;
62      }
63  
64      /**
65       * Gets the exceptions that occurred while building the dependency graph.
66       * 
67       * @return The exceptions that occurred, never {@code null}.
68       */
69      public List<Exception> getExceptions()
70      {
71          return exceptions;
72      }
73  
74      /**
75       * Records the specified exception while building the dependency graph.
76       * 
77       * @param exception The exception to record, may be {@code null}.
78       * @return This result for chaining, never {@code null}.
79       */
80      public CollectResult addException( Exception exception )
81      {
82          if ( exception != null )
83          {
84              if ( exceptions.isEmpty() )
85              {
86                  exceptions = new ArrayList<Exception>();
87              }
88              exceptions.add( exception );
89          }
90          return this;
91      }
92  
93      /**
94       * Gets the dependency cycles that were encountered while building the dependency graph.
95       * 
96       * @return The dependency cycles in the (raw) graph, never {@code null}.
97       */
98      public List<DependencyCycle> getCycles()
99      {
100         return cycles;
101     }
102 
103     /**
104      * Records the specified dependency cycle.
105      * 
106      * @param cycle The dependency cycle to record, may be {@code null}.
107      * @return This result for chaining, never {@code null}.
108      */
109     public CollectResult addCycle( DependencyCycle cycle )
110     {
111         if ( cycle != null )
112         {
113             if ( cycles.isEmpty() )
114             {
115                 cycles = new ArrayList<DependencyCycle>();
116             }
117             cycles.add( cycle );
118         }
119         return this;
120     }
121 
122     /**
123      * Gets the root node of the dependency graph.
124      * 
125      * @return The root node of the dependency graph or {@code null} if none.
126      */
127     public DependencyNode getRoot()
128     {
129         return root;
130     }
131 
132     /**
133      * Sets the root node of the dependency graph.
134      * 
135      * @param root The root node of the dependency graph, may be {@code null}.
136      * @return This result for chaining, never {@code null}.
137      */
138     public CollectResult setRoot( DependencyNode root )
139     {
140         this.root = root;
141         return this;
142     }
143 
144     @Override
145     public String toString()
146     {
147         return String.valueOf( getRoot() );
148     }
149 
150 }