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 * @since 2.0.11
76 */
77 public VersionRangeRequest(
78 Artifact artifact, List<RemoteRepository> repositories, Metadata.Nature nature, String context) {
79 setArtifact(artifact);
80 setRepositories(repositories);
81 setNature(nature);
82 setRequestContext(context);
83 }
84
85 /**
86 * Gets the artifact whose version range shall be resolved.
87 *
88 * @return The artifact or {@code null} if not set.
89 */
90 public Artifact getArtifact() {
91 return artifact;
92 }
93
94 /**
95 * Sets the artifact whose version range shall be resolved.
96 *
97 * @param artifact The artifact, may be {@code null}.
98 * @return This request for chaining, never {@code null}.
99 */
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 }