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