1 // =================== DO NOT EDIT THIS FILE ====================
2 // Generated by Modello Velocity from model.vm
3 // template, any modifications will be overwritten.
4 // ==============================================================
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Optional;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import java.util.stream.Stream;
16 import org.apache.maven.api.annotations.Experimental;
17 import org.apache.maven.api.annotations.Generated;
18 import org.apache.maven.api.annotations.Immutable;
19 import org.apache.maven.api.annotations.Nonnull;
20 import org.apache.maven.api.annotations.NotThreadSafe;
21 import org.apache.maven.api.annotations.ThreadSafe;
22
23 /**
24 * The {@code <parent>} element contains information required to locate the parent project from which
25 * this project will inherit from.
26 * <p><strong>Note:</strong> The children of this element are not interpolated and must be given as literal values.</p>
27 */
28 @Experimental
29 @Generated @ThreadSafe @Immutable
30 public class Parent
31 implements Serializable, InputLocationTracker
32 {
33 /**
34 * The group id of the parent project to inherit from.
35 */
36 final String groupId;
37 /**
38 * The artifact id of the parent project to inherit from.
39 */
40 final String artifactId;
41 /**
42 * The version of the parent project to inherit.
43 */
44 final String version;
45 /**
46 * The relative path of the parent subproject POM file or directory within the checkout.
47 * If not specified, it defaults to {@code ..}, i.e. the parent directory.
48 * Maven looks for the parent POM first in this location on
49 * the filesystem if explicitly provided, then in the reactor if groupId and artifactId are provided,
50 * then in the default parent directory, then the local repository, and lastly in the remote repo.
51 * However, if the both relative path and the group ID / artifact ID are provided,
52 * they must match the file in the location given.
53 * Specify either the {@code relativePath} or the {@code groupId}/{@code artifactId}, not both.
54 */
55 final String relativePath;
56 /** Locations */
57 final Map<Object, InputLocation> locations;
58 /** Location tracking */
59 final InputLocation importedFrom;
60
61 /**
62 * Constructor for this class, to be called from its subclasses and {@link Builder}.
63 * @see Builder#build()
64 */
65 protected Parent(Builder builder) {
66 this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
67 this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
68 this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
69 this.relativePath = builder.relativePath != null ? builder.relativePath : (builder.base != null ? builder.base.relativePath : null);
70 this.locations = builder.computeLocations();
71 this.importedFrom = builder.importedFrom;
72 }
73
74 /**
75 * The group id of the parent project to inherit from.
76 *
77 * @return a {@code String}
78 */
79 public String getGroupId() {
80 return this.groupId;
81 }
82
83 /**
84 * The artifact id of the parent project to inherit from.
85 *
86 * @return a {@code String}
87 */
88 public String getArtifactId() {
89 return this.artifactId;
90 }
91
92 /**
93 * The version of the parent project to inherit.
94 *
95 * @return a {@code String}
96 */
97 public String getVersion() {
98 return this.version;
99 }
100
101 /**
102 * The relative path of the parent subproject POM file or directory within the checkout.
103 * If not specified, it defaults to {@code ..}, i.e. the parent directory.
104 * Maven looks for the parent POM first in this location on
105 * the filesystem if explicitly provided, then in the reactor if groupId and artifactId are provided,
106 * then in the default parent directory, then the local repository, and lastly in the remote repo.
107 * However, if the both relative path and the group ID / artifact ID are provided,
108 * they must match the file in the location given.
109 * Specify either the {@code relativePath} or the {@code groupId}/{@code artifactId}, not both.
110 *
111 * @return a {@code String}
112 */
113 public String getRelativePath() {
114 return this.relativePath;
115 }
116
117 /**
118 * Gets the location of the specified field in the input source.
119 *
120 * @param key the key of the field, must not be {@code null}
121 * @return the location of the field in the input source or {@code null} if unknown
122 * @throws NullPointerException if {@code key} is {@code null}
123 */
124 public InputLocation getLocation(Object key) {
125 Objects.requireNonNull(key, "key");
126 return locations.get(key);
127 }
128
129 /**
130 * Gets the keys of the locations of the input source.
131 */
132 public Set<Object> getLocationKeys() {
133 return locations.keySet();
134 }
135
136 protected Stream<Object> getLocationKeyStream() {
137 return locations.keySet().stream();
138 }
139
140 /**
141 * Gets the input location that caused this model to be read.
142 */
143 public InputLocation getImportedFrom() {
144 return importedFrom;
145 }
146
147 /**
148 * Creates a new builder with this object as the basis.
149 *
150 * @return a {@code Builder}
151 */
152 @Nonnull
153 public Builder with() {
154 return newBuilder(this);
155 }
156 /**
157 * Creates a new {@code Parent} instance using the specified groupId.
158 *
159 * @param groupId the new {@code String} to use
160 * @return a {@code Parent} with the specified groupId
161 */
162 @Nonnull
163 public Parent withGroupId(String groupId) {
164 return newBuilder(this, true).groupId(groupId).build();
165 }
166 /**
167 * Creates a new {@code Parent} instance using the specified artifactId.
168 *
169 * @param artifactId the new {@code String} to use
170 * @return a {@code Parent} with the specified artifactId
171 */
172 @Nonnull
173 public Parent withArtifactId(String artifactId) {
174 return newBuilder(this, true).artifactId(artifactId).build();
175 }
176 /**
177 * Creates a new {@code Parent} instance using the specified version.
178 *
179 * @param version the new {@code String} to use
180 * @return a {@code Parent} with the specified version
181 */
182 @Nonnull
183 public Parent withVersion(String version) {
184 return newBuilder(this, true).version(version).build();
185 }
186 /**
187 * Creates a new {@code Parent} instance using the specified relativePath.
188 *
189 * @param relativePath the new {@code String} to use
190 * @return a {@code Parent} with the specified relativePath
191 */
192 @Nonnull
193 public Parent withRelativePath(String relativePath) {
194 return newBuilder(this, true).relativePath(relativePath).build();
195 }
196
197 /**
198 * Creates a new {@code Parent} instance.
199 * Equivalent to {@code newInstance(true)}.
200 * @see #newInstance(boolean)
201 *
202 * @return a new {@code Parent}
203 */
204 @Nonnull
205 public static Parent newInstance() {
206 return newInstance(true);
207 }
208
209 /**
210 * Creates a new {@code Parent} instance using default values or not.
211 * Equivalent to {@code newBuilder(withDefaults).build()}.
212 *
213 * @param withDefaults the boolean indicating whether default values should be used
214 * @return a new {@code Parent}
215 */
216 @Nonnull
217 public static Parent newInstance(boolean withDefaults) {
218 return newBuilder(withDefaults).build();
219 }
220
221 /**
222 * Creates a new {@code Parent} builder instance.
223 * Equivalent to {@code newBuilder(true)}.
224 * @see #newBuilder(boolean)
225 *
226 * @return a new {@code Builder}
227 */
228 @Nonnull
229 public static Builder newBuilder() {
230 return newBuilder(true);
231 }
232
233 /**
234 * Creates a new {@code Parent} builder instance using default values or not.
235 *
236 * @param withDefaults the boolean indicating whether default values should be used
237 * @return a new {@code Builder}
238 */
239 @Nonnull
240 public static Builder newBuilder(boolean withDefaults) {
241 return new Builder(withDefaults);
242 }
243
244 /**
245 * Creates a new {@code Parent} builder instance using the specified object as a basis.
246 * Equivalent to {@code newBuilder(from, false)}.
247 *
248 * @param from the {@code Parent} instance to use as a basis
249 * @return a new {@code Builder}
250 */
251 @Nonnull
252 public static Builder newBuilder(Parent from) {
253 return newBuilder(from, false);
254 }
255
256 /**
257 * Creates a new {@code Parent} builder instance using the specified object as a basis.
258 *
259 * @param from the {@code Parent} instance to use as a basis
260 * @param forceCopy the boolean indicating if a copy should be forced
261 * @return a new {@code Builder}
262 */
263 @Nonnull
264 public static Builder newBuilder(Parent from, boolean forceCopy) {
265 return new Builder(from, forceCopy);
266 }
267
268 /**
269 * Builder class used to create Parent instances.
270 * @see #with()
271 * @see #newBuilder()
272 */
273 @NotThreadSafe
274 public static class Builder
275 {
276 Parent base;
277 String groupId;
278 String artifactId;
279 String version;
280 String relativePath;
281 Map<Object, InputLocation> locations;
282 InputLocation importedFrom;
283
284 protected Builder(boolean withDefaults) {
285 if (withDefaults) {
286 }
287 }
288
289 protected Builder(Parent base, boolean forceCopy) {
290 if (forceCopy) {
291 this.groupId = base.groupId;
292 this.artifactId = base.artifactId;
293 this.version = base.version;
294 this.relativePath = base.relativePath;
295 this.locations = base.locations;
296 this.importedFrom = base.importedFrom;
297 } else {
298 this.base = base;
299 }
300 }
301
302 @Nonnull
303 public Builder groupId(String groupId) {
304 this.groupId = groupId;
305 return this;
306 }
307
308 @Nonnull
309 public Builder artifactId(String artifactId) {
310 this.artifactId = artifactId;
311 return this;
312 }
313
314 @Nonnull
315 public Builder version(String version) {
316 this.version = version;
317 return this;
318 }
319
320 @Nonnull
321 public Builder relativePath(String relativePath) {
322 this.relativePath = relativePath;
323 return this;
324 }
325
326
327 @Nonnull
328 public Builder location(Object key, InputLocation location) {
329 if (location != null) {
330 if (!(this.locations instanceof HashMap)) {
331 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
332 }
333 this.locations.put(key, location);
334 }
335 return this;
336 }
337
338 @Nonnull
339 public Builder importedFrom(InputLocation importedFrom) {
340 this.importedFrom = importedFrom;
341 return this;
342 }
343
344 @Nonnull
345 public Parent build() {
346 // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
347 if (base != null
348 && (groupId == null || groupId == base.groupId)
349 && (artifactId == null || artifactId == base.artifactId)
350 && (version == null || version == base.version)
351 && (relativePath == null || relativePath == base.relativePath)
352 ) {
353 return base;
354 }
355 return new Parent(this);
356 }
357
358 Map<Object, InputLocation> computeLocations() {
359 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
360 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
361 if (newlocs.isEmpty()) {
362 return Map.copyOf(oldlocs);
363 }
364 if (oldlocs.isEmpty()) {
365 return Map.copyOf(newlocs);
366 }
367 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
368 // Keep value from newlocs in case of duplicates
369 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
370 }
371 }
372
373
374
375 /**
376 * @return the id as {@code groupId:artifactId:version}
377 */
378 public String getId() {
379 return getGroupId() + ":" + getArtifactId() + ":pom:" + getVersion();
380 }
381
382 @Override
383 public String toString() {
384 return getId();
385 }
386
387
388 }