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;
027
028import org.eclipse.aether.RepositorySystem;
029import org.eclipse.aether.RepositorySystemSession;
030import org.eclipse.aether.repository.ArtifactRepository;
031import org.eclipse.aether.version.Version;
032import org.eclipse.aether.version.VersionConstraint;
033
034/**
035 * The result of a version range resolution request.
036 * 
037 * @see RepositorySystem#resolveVersionRange(RepositorySystemSession, VersionRangeRequest)
038 */
039public final class VersionRangeResult
040{
041
042    private final VersionRangeRequest request;
043
044    private List<Exception> exceptions;
045
046    private List<Version> versions;
047
048    private Map<Version, ArtifactRepository> repositories;
049
050    private VersionConstraint versionConstraint;
051
052    /**
053     * Creates a new result for the specified request.
054     * 
055     * @param request The resolution request, must not be {@code null}.
056     */
057    public VersionRangeResult( VersionRangeRequest request )
058    {
059        if ( request == null )
060        {
061            throw new IllegalArgumentException( "version range request has not been specified" );
062        }
063        this.request = request;
064        exceptions = Collections.emptyList();
065        versions = Collections.emptyList();
066        repositories = Collections.emptyMap();
067    }
068
069    /**
070     * Gets the resolution request that was made.
071     * 
072     * @return The resolution request, never {@code null}.
073     */
074    public VersionRangeRequest getRequest()
075    {
076        return request;
077    }
078
079    /**
080     * Gets the exceptions that occurred while resolving the version range.
081     * 
082     * @return The exceptions that occurred, never {@code null}.
083     */
084    public List<Exception> getExceptions()
085    {
086        return exceptions;
087    }
088
089    /**
090     * Records the specified exception while resolving the version range.
091     * 
092     * @param exception The exception to record, may be {@code null}.
093     * @return This result for chaining, never {@code null}.
094     */
095    public VersionRangeResult addException( Exception exception )
096    {
097        if ( exception != null )
098        {
099            if ( exceptions.isEmpty() )
100            {
101                exceptions = new ArrayList<Exception>();
102            }
103            exceptions.add( exception );
104        }
105        return this;
106    }
107
108    /**
109     * Gets the versions (in ascending order) that matched the requested range.
110     * 
111     * @return The matching versions (if any), never {@code null}.
112     */
113    public List<Version> getVersions()
114    {
115        return versions;
116    }
117
118    /**
119     * Adds the specified version to the result. Note that versions must be added in ascending order.
120     * 
121     * @param version The version to add, must not be {@code null}.
122     * @return This result for chaining, never {@code null}.
123     */
124    public VersionRangeResult addVersion( Version version )
125    {
126        if ( versions.isEmpty() )
127        {
128            versions = new ArrayList<Version>();
129        }
130        versions.add( version );
131        return this;
132    }
133
134    /**
135     * Sets the versions (in ascending order) matching the requested range.
136     * 
137     * @param versions The matching versions, may be empty or {@code null} if none.
138     * @return This result for chaining, never {@code null}.
139     */
140    public VersionRangeResult setVersions( List<Version> versions )
141    {
142        if ( versions == null )
143        {
144            this.versions = Collections.emptyList();
145        }
146        else
147        {
148            this.versions = versions;
149        }
150        return this;
151    }
152
153    /**
154     * Gets the lowest version matching the requested range.
155     * 
156     * @return The lowest matching version or {@code null} if no versions matched the requested range.
157     */
158    public Version getLowestVersion()
159    {
160        if ( versions.isEmpty() )
161        {
162            return null;
163        }
164        return versions.get( 0 );
165    }
166
167    /**
168     * Gets the highest version matching the requested range.
169     * 
170     * @return The highest matching version or {@code null} if no versions matched the requested range.
171     */
172    public Version getHighestVersion()
173    {
174        if ( versions.isEmpty() )
175        {
176            return null;
177        }
178        return versions.get( versions.size() - 1 );
179    }
180
181    /**
182     * Gets the repository from which the specified version was resolved.
183     * 
184     * @param version The version whose source repository should be retrieved, must not be {@code null}.
185     * @return The repository from which the version was resolved or {@code null} if unknown.
186     */
187    public ArtifactRepository getRepository( Version version )
188    {
189        return repositories.get( version );
190    }
191
192    /**
193     * Records the repository from which the specified version was resolved
194     * 
195     * @param version The version whose source repository is to be recorded, must not be {@code null}.
196     * @param repository The repository from which the version was resolved, may be {@code null}.
197     * @return This result for chaining, never {@code null}.
198     */
199    public VersionRangeResult setRepository( Version version, ArtifactRepository repository )
200    {
201        if ( repository != null )
202        {
203            if ( repositories.isEmpty() )
204            {
205                repositories = new HashMap<Version, ArtifactRepository>();
206            }
207            repositories.put( version, repository );
208        }
209        return this;
210    }
211
212    /**
213     * Gets the version constraint that was parsed from the artifact's version string.
214     * 
215     * @return The parsed version constraint or {@code null}.
216     */
217    public VersionConstraint getVersionConstraint()
218    {
219        return versionConstraint;
220    }
221
222    /**
223     * Sets the version constraint that was parsed from the artifact's version string.
224     * 
225     * @param versionConstraint The parsed version constraint, may be {@code null}.
226     * @return This result for chaining, never {@code null}.
227     */
228    public VersionRangeResult setVersionConstraint( VersionConstraint versionConstraint )
229    {
230        this.versionConstraint = versionConstraint;
231        return this;
232    }
233
234    @Override
235    public String toString()
236    {
237        return String.valueOf( repositories );
238    }
239
240}