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     */
076    public VersionRangeRequest(
077            Artifact artifact, List<RemoteRepository> repositories, Metadata.Nature nature, String context) {
078        setArtifact(artifact);
079        setRepositories(repositories);
080        setNature(nature);
081        setRequestContext(context);
082    }
083
084    /**
085     * Gets the artifact whose version range shall be resolved.
086     *
087     * @return The artifact or {@code null} if not set.
088     */
089    public Artifact getArtifact() {
090        return artifact;
091    }
092
093    /**
094     * Sets the artifact whose version range shall be resolved.
095     *
096     * @param artifact The artifact, may be {@code null}.
097     * @return This request for chaining, never {@code null}.
098     */
099    public VersionRangeRequest setArtifact(Artifact artifact) {
100        this.artifact = artifact;
101        return this;
102    }
103
104    /**
105     * Gets the repositories to resolve the version range from.
106     *
107     * @return The repositories, never {@code null}.
108     */
109    public List<RemoteRepository> getRepositories() {
110        return repositories;
111    }
112
113    /**
114     * Sets the repositories to resolve the version range from.
115     *
116     * @param repositories The repositories, may be {@code null}.
117     * @return This request for chaining, never {@code null}.
118     */
119    public VersionRangeRequest setRepositories(List<RemoteRepository> repositories) {
120        if (repositories == null) {
121            this.repositories = Collections.emptyList();
122        } else {
123            this.repositories = repositories;
124        }
125        return this;
126    }
127
128    /**
129     * Adds the specified repository for the resolution.
130     *
131     * @param repository The repository to add, may be {@code null}.
132     * @return This request for chaining, never {@code null}.
133     */
134    public VersionRangeRequest addRepository(RemoteRepository repository) {
135        if (repository != null) {
136            if (this.repositories.isEmpty()) {
137                this.repositories = new ArrayList<>();
138            }
139            this.repositories.add(repository);
140        }
141        return this;
142    }
143
144    /**
145     * The nature of metadata to use for resolving the version from, never {@code null}.
146     *
147     * @return The nature, never {@code null}.
148     * @since 1.9.25
149     */
150    public Metadata.Nature getNature() {
151        return nature;
152    }
153
154    /**
155     * Sets the nature of metadata to use for resolving the version from
156     *
157     * @param nature The nature, may be {@code null}.
158     * @return This request for chaining, never {@code null}.
159     * @since 1.9.25
160     */
161    public VersionRangeRequest setNature(Metadata.Nature nature) {
162        if (nature == null) {
163            this.nature = Metadata.Nature.RELEASE_OR_SNAPSHOT;
164        } else {
165            this.nature = nature;
166        }
167        return this;
168    }
169
170    /**
171     * Gets the context in which this request is made.
172     *
173     * @return The context, never {@code null}.
174     */
175    public String getRequestContext() {
176        return context;
177    }
178
179    /**
180     * Sets the context in which this request is made.
181     *
182     * @param context The context, may be {@code null}.
183     * @return This request for chaining, never {@code null}.
184     */
185    public VersionRangeRequest setRequestContext(String context) {
186        this.context = (context != null) ? context.intern() : "";
187        return this;
188    }
189
190    /**
191     * Gets the trace information that describes the higher level request/operation in which this request is issued.
192     *
193     * @return The trace information about the higher level operation or {@code null} if none.
194     */
195    public RequestTrace getTrace() {
196        return trace;
197    }
198
199    /**
200     * Sets the trace information that describes the higher level request/operation in which this request is issued.
201     *
202     * @param trace The trace information about the higher level operation, may be {@code null}.
203     * @return This request for chaining, never {@code null}.
204     */
205    public VersionRangeRequest setTrace(RequestTrace trace) {
206        this.trace = trace;
207        return this;
208    }
209
210    @Override
211    public String toString() {
212        return getArtifact() + " < " + getRepositories();
213    }
214}