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
9 * Sonatype, Inc. - initial API and implementation
10 *******************************************************************************/
11 package org
.eclipse
.aether
.resolution
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.HashMap
;
16 import java
.util
.List
;
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
;
26 * The result of a version range resolution request.
28 * @see RepositorySystem#resolveVersionRange(RepositorySystemSession, VersionRangeRequest)
30 public final class VersionRangeResult
33 private final VersionRangeRequest request
;
35 private List
<Exception
> exceptions
;
37 private List
<Version
> versions
;
39 private Map
<Version
, ArtifactRepository
> repositories
;
41 private VersionConstraint versionConstraint
;
44 * Creates a new result for the specified request.
46 * @param request The resolution request, must not be {@code null}.
48 public VersionRangeResult( VersionRangeRequest request
)
50 if ( request
== null )
52 throw new IllegalArgumentException( "version range request has not been specified" );
54 this.request
= request
;
55 exceptions
= Collections
.emptyList();
56 versions
= Collections
.emptyList();
57 repositories
= Collections
.emptyMap();
61 * Gets the resolution request that was made.
63 * @return The resolution request, never {@code null}.
65 public VersionRangeRequest
getRequest()
71 * Gets the exceptions that occurred while resolving the version range.
73 * @return The exceptions that occurred, never {@code null}.
75 public List
<Exception
> getExceptions()
81 * Records the specified exception while resolving the version range.
83 * @param exception The exception to record, may be {@code null}.
84 * @return This result for chaining, never {@code null}.
86 public VersionRangeResult
addException( Exception exception
)
88 if ( exception
!= null )
90 if ( exceptions
.isEmpty() )
92 exceptions
= new ArrayList
<Exception
>();
94 exceptions
.add( exception
);
100 * Gets the versions (in ascending order) that matched the requested range.
102 * @return The matching versions (if any), never {@code null}.
104 public List
<Version
> getVersions()
110 * Adds the specified version to the result. Note that versions must be added in ascending order.
112 * @param version The version to add, must not be {@code null}.
113 * @return This result for chaining, never {@code null}.
115 public VersionRangeResult
addVersion( Version version
)
117 if ( versions
.isEmpty() )
119 versions
= new ArrayList
<Version
>();
121 versions
.add( version
);
126 * Sets the versions (in ascending order) matching the requested range.
128 * @param versions The matching versions, may be empty or {@code null} if none.
129 * @return This result for chaining, never {@code null}.
131 public VersionRangeResult
setVersions( List
<Version
> versions
)
133 if ( versions
== null )
135 this.versions
= Collections
.emptyList();
139 this.versions
= versions
;
145 * Gets the lowest version matching the requested range.
147 * @return The lowest matching version or {@code null} if no versions matched the requested range.
149 public Version
getLowestVersion()
151 if ( versions
.isEmpty() )
155 return versions
.get( 0 );
159 * Gets the highest version matching the requested range.
161 * @return The highest matching version or {@code null} if no versions matched the requested range.
163 public Version
getHighestVersion()
165 if ( versions
.isEmpty() )
169 return versions
.get( versions
.size() - 1 );
173 * Gets the repository from which the specified version was resolved.
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.
178 public ArtifactRepository
getRepository( Version version
)
180 return repositories
.get( version
);
184 * Records the repository from which the specified version was resolved
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}.
190 public VersionRangeResult
setRepository( Version version
, ArtifactRepository repository
)
192 if ( repository
!= null )
194 if ( repositories
.isEmpty() )
196 repositories
= new HashMap
<Version
, ArtifactRepository
>();
198 repositories
.put( version
, repository
);
204 * Gets the version constraint that was parsed from the artifact's version string.
206 * @return The parsed version constraint or {@code null}.
208 public VersionConstraint
getVersionConstraint()
210 return versionConstraint
;
214 * Sets the version constraint that was parsed from the artifact's version string.
216 * @param versionConstraint The parsed version constraint, may be {@code null}.
217 * @return This result for chaining, never {@code null}.
219 public VersionRangeResult
setVersionConstraint( VersionConstraint versionConstraint
)
221 this.versionConstraint
= versionConstraint
;
226 public String
toString()
228 return String
.valueOf( repositories
);