001package org.eclipse.aether.resolution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import static java.util.Objects.requireNonNull;
028
029import org.eclipse.aether.RepositorySystem;
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.repository.ArtifactRepository;
032import org.eclipse.aether.version.Version;
033import org.eclipse.aether.version.VersionConstraint;
034
035/**
036 * The result of a version range resolution request.
037 * 
038 * @see RepositorySystem#resolveVersionRange(RepositorySystemSession, VersionRangeRequest)
039 */
040public final class VersionRangeResult
041{
042
043    private final VersionRangeRequest request;
044
045    private List<Exception> exceptions;
046
047    private List<Version> versions;
048
049    private Map<Version, ArtifactRepository> repositories;
050
051    private VersionConstraint versionConstraint;
052
053    /**
054     * Creates a new result for the specified request.
055     *
056     * @param request The resolution request, must not be {@code null}.
057     */
058    public VersionRangeResult( VersionRangeRequest request )
059    {
060        this.request = requireNonNull( request, "version range request cannot be null" );
061        exceptions = Collections.emptyList();
062        versions = Collections.emptyList();
063        repositories = Collections.emptyMap();
064    }
065
066    /**
067     * Gets the resolution request that was made.
068     * 
069     * @return The resolution request, never {@code null}.
070     */
071    public VersionRangeRequest getRequest()
072    {
073        return request;
074    }
075
076    /**
077     * Gets the exceptions that occurred while resolving the version range.
078     * 
079     * @return The exceptions that occurred, never {@code null}.
080     */
081    public List<Exception> getExceptions()
082    {
083        return exceptions;
084    }
085
086    /**
087     * Records the specified exception while resolving the version range.
088     * 
089     * @param exception The exception to record, may be {@code null}.
090     * @return This result for chaining, never {@code null}.
091     */
092    public VersionRangeResult addException( Exception exception )
093    {
094        if ( exception != null )
095        {
096            if ( exceptions.isEmpty() )
097            {
098                exceptions = new ArrayList<Exception>();
099            }
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}