001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.resolution;
020
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.eclipse.aether.repository.ArtifactRepository;
028import org.eclipse.aether.version.Version;
029import org.eclipse.aether.version.VersionConstraint;
030
031import static java.util.Objects.requireNonNull;
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    private final VersionRangeRequest request;
042
043    private List<Exception> exceptions;
044
045    private List<Version> versions;
046
047    private Map<Version, ArtifactRepository> repositories;
048
049    private VersionConstraint versionConstraint;
050
051    /**
052     * Creates a new result for the specified request.
053     *
054     * @param request The resolution request, must not be {@code null}.
055     */
056    public VersionRangeResult(VersionRangeRequest request) {
057        this.request = requireNonNull(request, "version range request cannot be null");
058        exceptions = Collections.emptyList();
059        versions = Collections.emptyList();
060        repositories = Collections.emptyMap();
061    }
062
063    /**
064     * Gets the resolution request that was made.
065     *
066     * @return The resolution request, never {@code null}.
067     */
068    public VersionRangeRequest getRequest() {
069        return request;
070    }
071
072    /**
073     * Gets the exceptions that occurred while resolving the version range.
074     *
075     * @return The exceptions that occurred, never {@code null}.
076     */
077    public List<Exception> getExceptions() {
078        return exceptions;
079    }
080
081    /**
082     * Records the specified exception while resolving the version range.
083     *
084     * @param exception The exception to record, may be {@code null}.
085     * @return This result for chaining, never {@code null}.
086     */
087    public VersionRangeResult addException(Exception exception) {
088        if (exception != null) {
089            if (exceptions.isEmpty()) {
090                exceptions = new ArrayList<>();
091            }
092            exceptions.add(exception);
093        }
094        return this;
095    }
096
097    /**
098     * Gets the versions (in ascending order) that matched the requested range.
099     *
100     * @return The matching versions (if any), never {@code null}.
101     */
102    public List<Version> getVersions() {
103        return versions;
104    }
105
106    /**
107     * Adds the specified version to the result. Note that versions must be added in ascending order.
108     *
109     * @param version The version to add, must not be {@code null}.
110     * @return This result for chaining, never {@code null}.
111     */
112    public VersionRangeResult addVersion(Version version) {
113        if (versions.isEmpty()) {
114            versions = new ArrayList<>();
115        }
116        versions.add(version);
117        return this;
118    }
119
120    /**
121     * Sets the versions (in ascending order) matching the requested range.
122     *
123     * @param versions The matching versions, may be empty or {@code null} if none.
124     * @return This result for chaining, never {@code null}.
125     */
126    public VersionRangeResult setVersions(List<Version> versions) {
127        if (versions == null) {
128            this.versions = Collections.emptyList();
129        } else {
130            this.versions = versions;
131        }
132        return this;
133    }
134
135    /**
136     * Gets the lowest version matching the requested range.
137     *
138     * @return The lowest matching version or {@code null} if no versions matched the requested range.
139     */
140    public Version getLowestVersion() {
141        if (versions.isEmpty()) {
142            return null;
143        }
144        return versions.get(0);
145    }
146
147    /**
148     * Gets the highest version matching the requested range.
149     *
150     * @return The highest matching version or {@code null} if no versions matched the requested range.
151     */
152    public Version getHighestVersion() {
153        if (versions.isEmpty()) {
154            return null;
155        }
156        return versions.get(versions.size() - 1);
157    }
158
159    /**
160     * Gets the repository from which the specified version was resolved.
161     *
162     * @param version The version whose source repository should be retrieved, must not be {@code null}.
163     * @return The repository from which the version was resolved or {@code null} if unknown.
164     */
165    public ArtifactRepository getRepository(Version version) {
166        return repositories.get(version);
167    }
168
169    /**
170     * Records the repository from which the specified version was resolved
171     *
172     * @param version The version whose source repository is to be recorded, must not be {@code null}.
173     * @param repository The repository from which the version was resolved, may be {@code null}.
174     * @return This result for chaining, never {@code null}.
175     */
176    public VersionRangeResult setRepository(Version version, ArtifactRepository repository) {
177        if (repository != null) {
178            if (repositories.isEmpty()) {
179                repositories = new HashMap<>();
180            }
181            repositories.put(version, repository);
182        }
183        return this;
184    }
185
186    /**
187     * Gets the version constraint that was parsed from the artifact's version string.
188     *
189     * @return The parsed version constraint or {@code null}.
190     */
191    public VersionConstraint getVersionConstraint() {
192        return versionConstraint;
193    }
194
195    /**
196     * Sets the version constraint that was parsed from the artifact's version string.
197     *
198     * @param versionConstraint The parsed version constraint, may be {@code null}.
199     * @return This result for chaining, never {@code null}.
200     */
201    public VersionRangeResult setVersionConstraint(VersionConstraint versionConstraint) {
202        this.versionConstraint = versionConstraint;
203        return this;
204    }
205
206    @Override
207    public String toString() {
208        return String.valueOf(repositories);
209    }
210}