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 InputLocation location;
56
57 final InputLocation groupIdLocation;
58
59 final InputLocation artifactIdLocation;
60
61 final InputLocation versionLocation;
62
63 final InputLocation relativePathLocation;
64
65 final Map<Object, InputLocation> locations;
66
67
68
69
70
71 Parent(
72 String groupId,
73 String artifactId,
74 String version,
75 String relativePath,
76 Map<Object, InputLocation> locations,
77 InputLocation location,
78 InputLocation groupIdLocation,
79 InputLocation artifactIdLocation,
80 InputLocation versionLocation,
81 InputLocation relativePathLocation
82 )
83 {
84 this.groupId = groupId;
85 this.artifactId = artifactId;
86 this.version = version;
87 this.relativePath = relativePath;
88 this.locations = ImmutableCollections.copy( locations );
89 this.location = location;
90 this.groupIdLocation = groupIdLocation;
91 this.artifactIdLocation = artifactIdLocation;
92 this.versionLocation = versionLocation;
93 this.relativePathLocation = relativePathLocation;
94 }
95
96
97
98
99
100
101 public String getGroupId()
102 {
103 return this.groupId;
104 }
105
106
107
108
109
110
111 public String getArtifactId()
112 {
113 return this.artifactId;
114 }
115
116
117
118
119
120
121 public String getVersion()
122 {
123 return this.version;
124 }
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141 public String getRelativePath()
142 {
143 return this.relativePath;
144 }
145
146
147
148
149 public InputLocation getLocation( Object key )
150 {
151 if ( key instanceof String )
152 {
153 switch ( ( String ) key )
154 {
155 case "":
156 return location;
157 case "groupId":
158 return groupIdLocation;
159 case "artifactId":
160 return artifactIdLocation;
161 case "version":
162 return versionLocation;
163 case "relativePath":
164 return relativePathLocation;
165 }
166 }
167 return locations != null ? locations.get( key ) : null;
168 }
169
170
171
172
173
174
175 @Nonnull
176 public Builder with()
177 {
178 return newBuilder( this );
179 }
180
181
182
183
184
185
186 @Nonnull
187 public Parent withGroupId( String groupId )
188 {
189 return with().groupId( groupId ).build();
190 }
191
192
193
194
195
196
197 @Nonnull
198 public Parent withArtifactId( String artifactId )
199 {
200 return with().artifactId( artifactId ).build();
201 }
202
203
204
205
206
207
208 @Nonnull
209 public Parent withVersion( String version )
210 {
211 return with().version( version ).build();
212 }
213
214
215
216
217
218
219 @Nonnull
220 public Parent withRelativePath( String relativePath )
221 {
222 return with().relativePath( relativePath ).build();
223 }
224
225
226
227
228
229
230
231
232 @Nonnull
233 public static Parent newInstance()
234 {
235 return newInstance( true );
236 }
237
238
239
240
241
242
243
244
245 @Nonnull
246 public static Parent newInstance( boolean withDefaults )
247 {
248 return newBuilder( withDefaults ).build();
249 }
250
251
252
253
254
255
256
257
258 @Nonnull
259 public static Builder newBuilder()
260 {
261 return newBuilder( true );
262 }
263
264
265
266
267
268
269
270 @Nonnull
271 public static Builder newBuilder( boolean withDefaults )
272 {
273 return new Builder( withDefaults );
274 }
275
276
277
278
279
280
281
282
283 @Nonnull
284 public static Builder newBuilder( Parent from )
285 {
286 return newBuilder( from, false );
287 }
288
289
290
291
292
293
294
295
296 @Nonnull
297 public static Builder newBuilder( Parent from, boolean forceCopy )
298 {
299 return new Builder( from, forceCopy );
300 }
301
302
303
304
305
306
307 @NotThreadSafe
308 public static class Builder
309 {
310 Parent base;
311 String groupId;
312 String artifactId;
313 String version;
314 String relativePath;
315 Map<Object, InputLocation> locations;
316
317 Builder( boolean withDefaults )
318 {
319 if ( withDefaults )
320 {
321 this.relativePath = "../pom.xml";
322 }
323 }
324
325 Builder( Parent base, boolean forceCopy )
326 {
327 if ( forceCopy )
328 {
329 this.groupId = base.groupId;
330 this.artifactId = base.artifactId;
331 this.version = base.version;
332 this.relativePath = base.relativePath;
333 }
334 else
335 {
336 this.base = base;
337 }
338 }
339
340 @Nonnull
341 public Builder groupId( String groupId )
342 {
343 this.groupId = groupId;
344 return this;
345 }
346
347 @Nonnull
348 public Builder artifactId( String artifactId )
349 {
350 this.artifactId = artifactId;
351 return this;
352 }
353
354 @Nonnull
355 public Builder version( String version )
356 {
357 this.version = version;
358 return this;
359 }
360
361 @Nonnull
362 public Builder relativePath( String relativePath )
363 {
364 this.relativePath = relativePath;
365 return this;
366 }
367
368
369 @Nonnull
370 public Builder location( Object key, InputLocation location )
371 {
372 if ( location != null )
373 {
374 if ( this.locations == null )
375 {
376 this.locations = new HashMap<>();
377 }
378 this.locations.put( key, location );
379 }
380 return this;
381 }
382
383 @Nonnull
384 public Parent build()
385 {
386 if ( base != null
387 && ( groupId == null || groupId == base.groupId )
388 && ( artifactId == null || artifactId == base.artifactId )
389 && ( version == null || version == base.version )
390 && ( relativePath == null || relativePath == base.relativePath )
391 )
392 {
393 return base;
394 }
395 Map<Object, InputLocation> locations = null;
396 InputLocation location = null;
397 InputLocation groupIdLocation = null;
398 InputLocation artifactIdLocation = null;
399 InputLocation versionLocation = null;
400 InputLocation relativePathLocation = null;
401 if ( this.locations != null )
402 {
403 locations = this.locations;
404 location = locations.remove( "" );
405 groupIdLocation = locations.remove( "groupId" );
406 artifactIdLocation = locations.remove( "artifactId" );
407 versionLocation = locations.remove( "version" );
408 relativePathLocation = locations.remove( "relativePath" );
409 }
410 return new Parent(
411 groupId != null ? groupId : ( base != null ? base.groupId : null ),
412 artifactId != null ? artifactId : ( base != null ? base.artifactId : null ),
413 version != null ? version : ( base != null ? base.version : null ),
414 relativePath != null ? relativePath : ( base != null ? base.relativePath : null ),
415 locations != null ? locations : ( base != null ? base.locations : null ),
416 location != null ? location : ( base != null ? base.location : null ),
417 groupIdLocation != null ? groupIdLocation : ( base != null ? base.groupIdLocation : null ),
418 artifactIdLocation != null ? artifactIdLocation : ( base != null ? base.artifactIdLocation : null ),
419 versionLocation != null ? versionLocation : ( base != null ? base.versionLocation : null ),
420 relativePathLocation != null ? relativePathLocation : ( base != null ? base.relativePathLocation : null )
421 );
422 }
423 }
424
425
426
427
428
429
430 public String getId()
431 {
432 StringBuilder id = new StringBuilder( 64 );
433
434 id.append( getGroupId() );
435 id.append( ":" );
436 id.append( getArtifactId() );
437 id.append( ":" );
438 id.append( "pom" );
439 id.append( ":" );
440 id.append( getVersion() );
441
442 return id.toString();
443 }
444
445 @Override
446 public String toString()
447 {
448 return getId();
449 }
450
451
452 }