View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2012 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.resolution;
12  
13  import java.util.ArrayList;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.List;
17  import java.util.Map;
18  
19  import org.eclipse.aether.RepositorySystem;
20  import org.eclipse.aether.RepositorySystemSession;
21  import org.eclipse.aether.repository.ArtifactRepository;
22  import org.eclipse.aether.version.Version;
23  import org.eclipse.aether.version.VersionConstraint;
24  
25  /**
26   * The result of a version range resolution request.
27   * 
28   * @see RepositorySystem#resolveVersionRange(RepositorySystemSession, VersionRangeRequest)
29   */
30  public final class VersionRangeResult
31  {
32  
33      private final VersionRangeRequest request;
34  
35      private List<Exception> exceptions;
36  
37      private List<Version> versions;
38  
39      private Map<Version, ArtifactRepository> repositories;
40  
41      private VersionConstraint versionConstraint;
42  
43      /**
44       * Creates a new result for the specified request.
45       * 
46       * @param request The resolution request, must not be {@code null}.
47       */
48      public VersionRangeResult( VersionRangeRequest request )
49      {
50          if ( request == null )
51          {
52              throw new IllegalArgumentException( "version range request has not been specified" );
53          }
54          this.request = request;
55          exceptions = Collections.emptyList();
56          versions = Collections.emptyList();
57          repositories = Collections.emptyMap();
58      }
59  
60      /**
61       * Gets the resolution request that was made.
62       * 
63       * @return The resolution request, never {@code null}.
64       */
65      public VersionRangeRequest getRequest()
66      {
67          return request;
68      }
69  
70      /**
71       * Gets the exceptions that occurred while resolving the version range.
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 resolving the version range.
82       * 
83       * @param exception The exception to record, may be {@code null}.
84       * @return This result for chaining, never {@code null}.
85       */
86      public VersionRangeResult 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 versions (in ascending order) that matched the requested range.
101      * 
102      * @return The matching versions (if any), never {@code null}.
103      */
104     public List<Version> getVersions()
105     {
106         return versions;
107     }
108 
109     /**
110      * Adds the specified version to the result. Note that versions must be added in ascending order.
111      * 
112      * @param version The version to add, must not be {@code null}.
113      * @return This result for chaining, never {@code null}.
114      */
115     public VersionRangeResult addVersion( Version version )
116     {
117         if ( versions.isEmpty() )
118         {
119             versions = new ArrayList<Version>();
120         }
121         versions.add( version );
122         return this;
123     }
124 
125     /**
126      * Sets the versions (in ascending order) matching the requested range.
127      * 
128      * @param versions The matching versions, may be empty or {@code null} if none.
129      * @return This result for chaining, never {@code null}.
130      */
131     public VersionRangeResult setVersions( List<Version> versions )
132     {
133         if ( versions == null )
134         {
135             this.versions = Collections.emptyList();
136         }
137         else
138         {
139             this.versions = versions;
140         }
141         return this;
142     }
143 
144     /**
145      * Gets the lowest version matching the requested range.
146      * 
147      * @return The lowest matching version or {@code null} if no versions matched the requested range.
148      */
149     public Version getLowestVersion()
150     {
151         if ( versions.isEmpty() )
152         {
153             return null;
154         }
155         return versions.get( 0 );
156     }
157 
158     /**
159      * Gets the highest version matching the requested range.
160      * 
161      * @return The highest matching version or {@code null} if no versions matched the requested range.
162      */
163     public Version getHighestVersion()
164     {
165         if ( versions.isEmpty() )
166         {
167             return null;
168         }
169         return versions.get( versions.size() - 1 );
170     }
171 
172     /**
173      * Gets the repository from which the specified version was resolved.
174      * 
175      * @param version The version whose source repository should be retrieved, must not be {@code null}.
176      * @return The repository from which the version was resolved or {@code null} if unknown.
177      */
178     public ArtifactRepository getRepository( Version version )
179     {
180         return repositories.get( version );
181     }
182 
183     /**
184      * Records the repository from which the specified version was resolved
185      * 
186      * @param version The version whose source repository is to be recorded, must not be {@code null}.
187      * @param repository The repository from which the version was resolved, may be {@code null}.
188      * @return This result for chaining, never {@code null}.
189      */
190     public VersionRangeResult setRepository( Version version, ArtifactRepository repository )
191     {
192         if ( repository != null )
193         {
194             if ( repositories.isEmpty() )
195             {
196                 repositories = new HashMap<Version, ArtifactRepository>();
197             }
198             repositories.put( version, repository );
199         }
200         return this;
201     }
202 
203     /**
204      * Gets the version constraint that was parsed from the artifact's version string.
205      * 
206      * @return The parsed version constraint or {@code null}.
207      */
208     public VersionConstraint getVersionConstraint()
209     {
210         return versionConstraint;
211     }
212 
213     /**
214      * Sets the version constraint that was parsed from the artifact's version string.
215      * 
216      * @param versionConstraint The parsed version constraint, may be {@code null}.
217      * @return This result for chaining, never {@code null}.
218      */
219     public VersionRangeResult setVersionConstraint( VersionConstraint versionConstraint )
220     {
221         this.versionConstraint = versionConstraint;
222         return this;
223     }
224 
225     @Override
226     public String toString()
227     {
228         return String.valueOf( repositories );
229     }
230 
231 }