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