View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.resolution;
20  
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.eclipse.aether.repository.ArtifactRepository;
28  import org.eclipse.aether.version.Version;
29  import org.eclipse.aether.version.VersionConstraint;
30  
31  import static java.util.Objects.requireNonNull;
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      private final VersionRangeRequest request;
42  
43      private List<Exception> exceptions;
44  
45      private List<Version> versions;
46  
47      private Map<Version, ArtifactRepository> repositories;
48  
49      private VersionConstraint versionConstraint;
50  
51      /**
52       * Creates a new result for the specified request.
53       *
54       * @param request The resolution request, must not be {@code null}.
55       */
56      public VersionRangeResult(VersionRangeRequest request) {
57          this.request = requireNonNull(request, "version range request cannot be null");
58          exceptions = Collections.emptyList();
59          versions = Collections.emptyList();
60          repositories = Collections.emptyMap();
61      }
62  
63      /**
64       * Gets the resolution request that was made.
65       *
66       * @return The resolution request, never {@code null}.
67       */
68      public VersionRangeRequest getRequest() {
69          return request;
70      }
71  
72      /**
73       * Gets the exceptions that occurred while resolving the version range.
74       *
75       * @return The exceptions that occurred, never {@code null}.
76       */
77      public List<Exception> getExceptions() {
78          return exceptions;
79      }
80  
81      /**
82       * Records the specified exception while resolving the version range.
83       *
84       * @param exception The exception to record, may be {@code null}.
85       * @return This result for chaining, never {@code null}.
86       */
87      public VersionRangeResult addException(Exception exception) {
88          if (exception != null) {
89              if (exceptions.isEmpty()) {
90                  exceptions = new ArrayList<>();
91              }
92              exceptions.add(exception);
93          }
94          return this;
95      }
96  
97      /**
98       * Gets the versions (in ascending order) that matched the requested range.
99       *
100      * @return The matching versions (if any), never {@code null}.
101      */
102     public List<Version> getVersions() {
103         return versions;
104     }
105 
106     /**
107      * Adds the specified version to the result. Note that versions must be added in ascending order.
108      *
109      * @param version The version to add, must not be {@code null}.
110      * @return This result for chaining, never {@code null}.
111      */
112     public VersionRangeResult addVersion(Version version) {
113         if (versions.isEmpty()) {
114             versions = new ArrayList<>();
115         }
116         versions.add(version);
117         return this;
118     }
119 
120     /**
121      * Sets the versions (in ascending order) matching the requested range.
122      *
123      * @param versions The matching versions, may be empty or {@code null} if none.
124      * @return This result for chaining, never {@code null}.
125      */
126     public VersionRangeResult setVersions(List<Version> versions) {
127         if (versions == null) {
128             this.versions = Collections.emptyList();
129         } else {
130             this.versions = versions;
131         }
132         return this;
133     }
134 
135     /**
136      * Gets the lowest version matching the requested range.
137      *
138      * @return The lowest matching version or {@code null} if no versions matched the requested range.
139      */
140     public Version getLowestVersion() {
141         if (versions.isEmpty()) {
142             return null;
143         }
144         return versions.get(0);
145     }
146 
147     /**
148      * Gets the highest version matching the requested range.
149      *
150      * @return The highest matching version or {@code null} if no versions matched the requested range.
151      */
152     public Version getHighestVersion() {
153         if (versions.isEmpty()) {
154             return null;
155         }
156         return versions.get(versions.size() - 1);
157     }
158 
159     /**
160      * Gets the repository from which the specified version was resolved.
161      *
162      * @param version The version whose source repository should be retrieved, must not be {@code null}.
163      * @return The repository from which the version was resolved or {@code null} if unknown.
164      */
165     public ArtifactRepository getRepository(Version version) {
166         return repositories.get(version);
167     }
168 
169     /**
170      * Records the repository from which the specified version was resolved
171      *
172      * @param version The version whose source repository is to be recorded, must not be {@code null}.
173      * @param repository The repository from which the version was resolved, may be {@code null}.
174      * @return This result for chaining, never {@code null}.
175      */
176     public VersionRangeResult setRepository(Version version, ArtifactRepository repository) {
177         if (repository != null) {
178             if (repositories.isEmpty()) {
179                 repositories = new HashMap<>();
180             }
181             repositories.put(version, repository);
182         }
183         return this;
184     }
185 
186     /**
187      * Gets the version constraint that was parsed from the artifact's version string.
188      *
189      * @return The parsed version constraint or {@code null}.
190      */
191     public VersionConstraint getVersionConstraint() {
192         return versionConstraint;
193     }
194 
195     /**
196      * Sets the version constraint that was parsed from the artifact's version string.
197      *
198      * @param versionConstraint The parsed version constraint, may be {@code null}.
199      * @return This result for chaining, never {@code null}.
200      */
201     public VersionRangeResult setVersionConstraint(VersionConstraint versionConstraint) {
202         this.versionConstraint = versionConstraint;
203         return this;
204     }
205 
206     @Override
207     public String toString() {
208         return String.valueOf(repositories);
209     }
210 }