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