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