1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.resolution;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 import org.eclipse.aether.RequestTrace;
26 import org.eclipse.aether.artifact.Artifact;
27 import org.eclipse.aether.metadata.Metadata;
28 import org.eclipse.aether.repository.RemoteRepository;
29
30 /**
31 * A request to resolve a version range.
32 *
33 * @see org.eclipse.aether.RepositorySystem#resolveVersionRange(org.eclipse.aether.RepositorySystemSession,
34 * VersionRangeRequest)
35 */
36 public final class VersionRangeRequest {
37
38 private Artifact artifact;
39
40 private List<RemoteRepository> repositories = Collections.emptyList();
41
42 private Metadata.Nature nature = Metadata.Nature.RELEASE_OR_SNAPSHOT;
43
44 private String context = "";
45
46 private RequestTrace trace;
47
48 /**
49 * Creates an uninitialized request.
50 */
51 public VersionRangeRequest() {
52 // enables default constructor
53 }
54
55 /**
56 * Creates a request with the specified properties.
57 *
58 * @param artifact The artifact whose version range should be resolved, may be {@code null}.
59 * @param repositories The repositories to resolve the version from, may be {@code null}.
60 * @param context The context in which this request is made, may be {@code null}.
61 */
62 public VersionRangeRequest(Artifact artifact, List<RemoteRepository> repositories, String context) {
63 setArtifact(artifact);
64 setRepositories(repositories);
65 setRequestContext(context);
66 }
67
68 /**
69 * Creates a request with the specified properties.
70 *
71 * @param artifact The artifact whose version range should be resolved, may be {@code null}.
72 * @param repositories The repositories to resolve the version from, may be {@code null}.
73 * @param nature The nature of metadata to use for resolving the version from, may be {@code null}.
74 * @param context The context in which this request is made, may be {@code null}.
75 */
76 public VersionRangeRequest(
77 Artifact artifact, List<RemoteRepository> repositories, Metadata.Nature nature, String context) {
78 setArtifact(artifact);
79 setRepositories(repositories);
80 setNature(nature);
81 setRequestContext(context);
82 }
83
84 /**
85 * Gets the artifact whose version range shall be resolved.
86 *
87 * @return The artifact or {@code null} if not set.
88 */
89 public Artifact getArtifact() {
90 return artifact;
91 }
92
93 /**
94 * Sets the artifact whose version range shall be resolved.
95 *
96 * @param artifact The artifact, may be {@code null}.
97 * @return This request for chaining, never {@code null}.
98 */
99 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 }