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