1
2
3
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
25
26
27
28 @Experimental
29 @Generated @ThreadSafe @Immutable
30 public class Parent
31 implements Serializable, InputLocationTracker
32 {
33
34
35
36 final String groupId;
37
38
39
40 final String artifactId;
41
42
43
44 final String version;
45
46
47
48
49
50
51
52
53
54
55 final String relativePath;
56
57 final Map<Object, InputLocation> locations;
58
59 final InputLocation importedFrom;
60
61
62
63
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
76
77
78
79 public String getGroupId() {
80 return this.groupId;
81 }
82
83
84
85
86
87
88 public String getArtifactId() {
89 return this.artifactId;
90 }
91
92
93
94
95
96
97 public String getVersion() {
98 return this.version;
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113 public String getRelativePath() {
114 return this.relativePath;
115 }
116
117
118
119
120 public InputLocation getLocation(Object key) {
121 return locations.get(key);
122 }
123
124
125
126
127 public Set<Object> getLocationKeys() {
128 return locations.keySet();
129 }
130
131 protected Stream<Object> getLocationKeyStream() {
132 return locations.keySet().stream();
133 }
134
135
136
137
138 public InputLocation getImportedFrom()
139 {
140 return importedFrom;
141 }
142
143
144
145
146
147
148 @Nonnull
149 public Builder with() {
150 return newBuilder(this);
151 }
152
153
154
155
156
157
158 @Nonnull
159 public Parent withGroupId(String groupId) {
160 return newBuilder(this, true).groupId(groupId).build();
161 }
162
163
164
165
166
167
168 @Nonnull
169 public Parent withArtifactId(String artifactId) {
170 return newBuilder(this, true).artifactId(artifactId).build();
171 }
172
173
174
175
176
177
178 @Nonnull
179 public Parent withVersion(String version) {
180 return newBuilder(this, true).version(version).build();
181 }
182
183
184
185
186
187
188 @Nonnull
189 public Parent withRelativePath(String relativePath) {
190 return newBuilder(this, true).relativePath(relativePath).build();
191 }
192
193
194
195
196
197
198
199
200 @Nonnull
201 public static Parent newInstance() {
202 return newInstance(true);
203 }
204
205
206
207
208
209
210
211
212 @Nonnull
213 public static Parent newInstance(boolean withDefaults) {
214 return newBuilder(withDefaults).build();
215 }
216
217
218
219
220
221
222
223
224 @Nonnull
225 public static Builder newBuilder() {
226 return newBuilder(true);
227 }
228
229
230
231
232
233
234
235 @Nonnull
236 public static Builder newBuilder(boolean withDefaults) {
237 return new Builder(withDefaults);
238 }
239
240
241
242
243
244
245
246
247 @Nonnull
248 public static Builder newBuilder(Parent from) {
249 return newBuilder(from, false);
250 }
251
252
253
254
255
256
257
258
259 @Nonnull
260 public static Builder newBuilder(Parent from, boolean forceCopy) {
261 return new Builder(from, forceCopy);
262 }
263
264
265
266
267
268
269 @NotThreadSafe
270 public static class Builder
271 {
272 Parent base;
273 String groupId;
274 String artifactId;
275 String version;
276 String relativePath;
277 Map<Object, InputLocation> locations;
278 InputLocation importedFrom;
279
280 protected Builder(boolean withDefaults) {
281 if (withDefaults) {
282 }
283 }
284
285 protected Builder(Parent base, boolean forceCopy) {
286 if (forceCopy) {
287 this.groupId = base.groupId;
288 this.artifactId = base.artifactId;
289 this.version = base.version;
290 this.relativePath = base.relativePath;
291 this.locations = base.locations;
292 this.importedFrom = base.importedFrom;
293 } else {
294 this.base = base;
295 }
296 }
297
298 @Nonnull
299 public Builder groupId(String groupId) {
300 this.groupId = groupId;
301 return this;
302 }
303
304 @Nonnull
305 public Builder artifactId(String artifactId) {
306 this.artifactId = artifactId;
307 return this;
308 }
309
310 @Nonnull
311 public Builder version(String version) {
312 this.version = version;
313 return this;
314 }
315
316 @Nonnull
317 public Builder relativePath(String relativePath) {
318 this.relativePath = relativePath;
319 return this;
320 }
321
322
323 @Nonnull
324 public Builder location(Object key, InputLocation location) {
325 if (location != null) {
326 if (!(this.locations instanceof HashMap)) {
327 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
328 }
329 this.locations.put(key, location);
330 }
331 return this;
332 }
333
334 @Nonnull
335 public Builder importedFrom(InputLocation importedFrom) {
336 this.importedFrom = importedFrom;
337 return this;
338 }
339
340 @Nonnull
341 public Parent build() {
342
343 if (base != null
344 && (groupId == null || groupId == base.groupId)
345 && (artifactId == null || artifactId == base.artifactId)
346 && (version == null || version == base.version)
347 && (relativePath == null || relativePath == base.relativePath)
348 ) {
349 return base;
350 }
351 return new Parent(this);
352 }
353
354 Map<Object, InputLocation> computeLocations() {
355 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
356 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
357 if (newlocs.isEmpty()) {
358 return Map.copyOf(oldlocs);
359 }
360 if (oldlocs.isEmpty()) {
361 return Map.copyOf(newlocs);
362 }
363 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
364
365 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
366 }
367 }
368
369
370
371
372
373
374 public String getId() {
375 return getGroupId() + ":" + getArtifactId() + ":pom:" + getVersion();
376 }
377
378 @Override
379 public String toString() {
380 return getId();
381 }
382
383
384 }