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.List;
024
025import org.eclipse.aether.RequestTrace;
026import org.eclipse.aether.artifact.Artifact;
027import org.eclipse.aether.metadata.Metadata;
028import org.eclipse.aether.repository.RemoteRepository;
029
030/**
031 * A request to resolve a version range.
032 *
033 * @see org.eclipse.aether.RepositorySystem#resolveVersionRange(org.eclipse.aether.RepositorySystemSession,
034 *  VersionRangeRequest)
035 */
036public final class VersionRangeRequest {
037
038    private Artifact artifact;
039
040    private List<RemoteRepository> repositories = Collections.emptyList();
041
042    private Metadata.Nature nature = Metadata.Nature.RELEASE_OR_SNAPSHOT;
043
044    private String context = "";
045
046    private RequestTrace trace;
047
048    /**
049     * Creates an uninitialized request.
050     */
051    public VersionRangeRequest() {
052        // enables default constructor
053    }
054
055    /**
056     * Creates a request with the specified properties.
057     *
058     * @param artifact The artifact whose version range should be resolved, may be {@code null}.
059     * @param repositories The repositories to resolve the version from, may be {@code null}.
060     * @param context The context in which this request is made, may be {@code null}.
061     */
062    public VersionRangeRequest(Artifact artifact, List<RemoteRepository> repositories, String context) {
063        setArtifact(artifact);
064        setRepositories(repositories);
065        setRequestContext(context);
066    }
067
068    /**
069     * Creates a request with the specified properties.
070     *
071     * @param artifact The artifact whose version range should be resolved, may be {@code null}.
072     * @param repositories The repositories to resolve the version from, may be {@code null}.
073     * @param nature The nature of metadata to use for resolving the version from, may be {@code null}.
074     * @param context The context in which this request is made, may be {@code null}.
075     * @since 2.0.11
076     */
077    public VersionRangeRequest(
078            Artifact artifact, List<RemoteRepository> repositories, Metadata.Nature nature, String context) {
079        setArtifact(artifact);
080        setRepositories(repositories);
081        setNature(nature);
082        setRequestContext(context);
083    }
084
085    /**
086     * Gets the artifact whose version range shall be resolved.
087     *
088     * @return The artifact or {@code null} if not set.
089     */
090    public Artifact getArtifact() {
091        return artifact;
092    }
093
094    /**
095     * Sets the artifact whose version range shall be resolved.
096     *
097     * @param artifact The artifact, may be {@code null}.
098     * @return This request for chaining, never {@code null}.
099     */
100    public VersionRangeRequest setArtifact(Artifact artifact) {
101        this.artifact = artifact;
102        return this;
103    }
104
105    /**
106     * Gets the repositories to resolve the version range from.
107     *
108     * @return The repositories, never {@code null}.
109     */
110    public List<RemoteRepository> getRepositories() {
111        return repositories;
112    }
113
114    /**
115     * Sets the repositories to resolve the version range from.
116     *
117     * @param repositories The repositories, may be {@code null}.
118     * @return This request for chaining, never {@code null}.
119     */
120    public VersionRangeRequest setRepositories(List<RemoteRepository> repositories) {
121        if (repositories == null) {
122            this.repositories = Collections.emptyList();
123        } else {
124            this.repositories = repositories;
125        }
126        return this;
127    }
128
129    /**
130     * Adds the specified repository for the resolution.
131     *
132     * @param repository The repository to add, may be {@code null}.
133     * @return This request for chaining, never {@code null}.
134     */
135    public VersionRangeRequest addRepository(RemoteRepository repository) {
136        if (repository != null) {
137            if (this.repositories.isEmpty()) {
138                this.repositories = new ArrayList<>();
139            }
140            this.repositories.add(repository);
141        }
142        return this;
143    }
144
145    /**
146     * The nature of metadata to use for resolving the version from, never {@code null}.
147     *
148     * @return The nature, never {@code null}.
149     * @since 2.0.11
150     */
151    public Metadata.Nature getNature() {
152        return nature;
153    }
154
155    /**
156     * Sets the nature of metadata to use for resolving the version from
157     *
158     * @param nature The nature, may be {@code null}.
159     * @return This request for chaining, never {@code null}.
160     * @since 2.0.11
161     */
162    public VersionRangeRequest setNature(Metadata.Nature nature) {
163        if (nature == null) {
164            this.nature = Metadata.Nature.RELEASE_OR_SNAPSHOT;
165        } else {
166            this.nature = nature;
167        }
168        return this;
169    }
170
171    /**
172     * Gets the context in which this request is made.
173     *
174     * @return The context, never {@code null}.
175     */
176    public String getRequestContext() {
177        return context;
178    }
179
180    /**
181     * Sets the context in which this request is made.
182     *
183     * @param context The context, may be {@code null}.
184     * @return This request for chaining, never {@code null}.
185     */
186    public VersionRangeRequest setRequestContext(String context) {
187        this.context = (context != null) ? context.intern() : "";
188        return this;
189    }
190
191    /**
192     * Gets the trace information that describes the higher level request/operation in which this request is issued.
193     *
194     * @return The trace information about the higher level operation or {@code null} if none.
195     */
196    public RequestTrace getTrace() {
197        return trace;
198    }
199
200    /**
201     * Sets the trace information that describes the higher level request/operation in which this request is issued.
202     *
203     * @param trace The trace information about the higher level operation, may be {@code null}.
204     * @return This request for chaining, never {@code null}.
205     */
206    public VersionRangeRequest setTrace(RequestTrace trace) {
207        this.trace = trace;
208        return this;
209    }
210
211    @Override
212    public String toString() {
213        return getArtifact() + " < " + getRepositories();
214    }
215}