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