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