View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from model.vm
3   //  template, any modifications will be overwritten.
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   * The {@code <parent>} element contains information required to locate the parent project from which
20   * this project will inherit from.
21   * <strong>Note:</strong> The children of this element are not interpolated and must be given as literal values.
22   */
23  @Experimental
24  @Generated @ThreadSafe @Immutable
25  public class Parent
26      implements Serializable, InputLocationTracker
27  {
28      /**
29       * The group id of the parent project to inherit from.
30       */
31      final String groupId;
32      /**
33       * The artifact id of the parent project to inherit from.
34       */
35      final String artifactId;
36      /**
37       * The version of the parent project to inherit.
38       */
39      final String version;
40      /**
41       * The relative path of the parent {@code pom.xml} file within the check out.
42       * If not specified, it defaults to {@code ../pom.xml}.
43       * Maven looks for the parent POM first in this location on
44       * the filesystem, then the local repository, and lastly in the remote repo.
45       * {@code relativePath} allows you to select a different location,
46       * for example when your structure is flat, or deeper without an intermediate parent POM.
47       * However, the group ID, artifact ID and version are still required,
48       * and must match the file in the location given or it will revert to the repository for the POM.
49       * This feature is only for enhancing the development in a local checkout of that project.
50       * Set the value to an empty string in case you want to disable the feature and always resolve
51       * the parent POM from the repositories.
52       */
53      final String relativePath;
54      /** Location of the xml element for this object. */
55      final InputLocation location;
56      /** Location of the xml element for the field groupId. */
57      final InputLocation groupIdLocation;
58      /** Location of the xml element for the field artifactId. */
59      final InputLocation artifactIdLocation;
60      /** Location of the xml element for the field version. */
61      final InputLocation versionLocation;
62      /** Location of the xml element for the field relativePath. */
63      final InputLocation relativePathLocation;
64      /** Other locations */
65      final Map<Object, InputLocation> locations;
66  
67      /**
68        * Constructor for this class, package protected.
69        * @see Builder#build()
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       * The group id of the parent project to inherit from.
98       *
99       * @return a {@code String}
100      */
101     public String getGroupId()
102     {
103         return this.groupId;
104     }
105 
106     /**
107      * The artifact id of the parent project to inherit from.
108      *
109      * @return a {@code String}
110      */
111     public String getArtifactId()
112     {
113         return this.artifactId;
114     }
115 
116     /**
117      * The version of the parent project to inherit.
118      *
119      * @return a {@code String}
120      */
121     public String getVersion()
122     {
123         return this.version;
124     }
125 
126     /**
127      * The relative path of the parent {@code pom.xml} file within the check out.
128      * If not specified, it defaults to {@code ../pom.xml}.
129      * Maven looks for the parent POM first in this location on
130      * the filesystem, then the local repository, and lastly in the remote repo.
131      * {@code relativePath} allows you to select a different location,
132      * for example when your structure is flat, or deeper without an intermediate parent POM.
133      * However, the group ID, artifact ID and version are still required,
134      * and must match the file in the location given or it will revert to the repository for the POM.
135      * This feature is only for enhancing the development in a local checkout of that project.
136      * Set the value to an empty string in case you want to disable the feature and always resolve
137      * the parent POM from the repositories.
138      *
139      * @return a {@code String}
140      */
141     public String getRelativePath()
142     {
143         return this.relativePath;
144     }
145 
146     /**
147      * Gets the location of the specified field in the input source.
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      * Creates a new builder with this object as the basis.
172      *
173      * @return a {@code Builder}
174      */
175     @Nonnull
176     public Builder with()
177     {
178         return newBuilder( this );
179     }
180     /**
181      * Creates a new {@code Parent} instance using the specified groupId.
182      *
183      * @param groupId the new {@code String} to use
184      * @return a {@code Parent} with the specified groupId
185      */
186     @Nonnull
187     public Parent withGroupId( String groupId )
188     {
189         return with().groupId( groupId ).build();
190     }
191     /**
192      * Creates a new {@code Parent} instance using the specified artifactId.
193      *
194      * @param artifactId the new {@code String} to use
195      * @return a {@code Parent} with the specified artifactId
196      */
197     @Nonnull
198     public Parent withArtifactId( String artifactId )
199     {
200         return with().artifactId( artifactId ).build();
201     }
202     /**
203      * Creates a new {@code Parent} instance using the specified version.
204      *
205      * @param version the new {@code String} to use
206      * @return a {@code Parent} with the specified version
207      */
208     @Nonnull
209     public Parent withVersion( String version )
210     {
211         return with().version( version ).build();
212     }
213     /**
214      * Creates a new {@code Parent} instance using the specified relativePath.
215      *
216      * @param relativePath the new {@code String} to use
217      * @return a {@code Parent} with the specified relativePath
218      */
219     @Nonnull
220     public Parent withRelativePath( String relativePath )
221     {
222         return with().relativePath( relativePath ).build();
223     }
224 
225     /**
226      * Creates a new {@code Parent} instance.
227      * Equivalent to {@code newInstance( true )}.
228      * @see #newInstance(boolean)
229      *
230      * @return a new {@code Parent}
231      */
232     @Nonnull
233     public static Parent newInstance()
234     {
235         return newInstance( true );
236     }
237 
238     /**
239      * Creates a new {@code Parent} instance using default values or not.
240      * Equivalent to {@code newBuilder( withDefaults ).build()}.
241      *
242      * @param withDefaults the boolean indicating whether default values should be used
243      * @return a new {@code Parent}
244      */
245     @Nonnull
246     public static Parent newInstance( boolean withDefaults )
247     {
248         return newBuilder( withDefaults ).build();
249     }
250 
251     /**
252      * Creates a new {@code Parent} builder instance.
253      * Equivalent to {@code newBuilder( true )}.
254      * @see #newBuilder(boolean)
255      *
256      * @return a new {@code Builder}
257      */
258     @Nonnull
259     public static Builder newBuilder()
260     {
261         return newBuilder( true );
262     }
263 
264     /**
265      * Creates a new {@code Parent} builder instance using default values or not.
266      *
267      * @param withDefaults the boolean indicating whether default values should be used
268      * @return a new {@code Builder}
269      */
270     @Nonnull
271     public static Builder newBuilder( boolean withDefaults )
272     {
273         return new Builder( withDefaults );
274     }
275 
276     /**
277      * Creates a new {@code Parent} builder instance using the specified object as a basis.
278      * Equivalent to {@code newBuilder( from, false )}.
279      *
280      * @param from the {@code Parent} instance to use as a basis
281      * @return a new {@code Builder}
282      */
283     @Nonnull
284     public static Builder newBuilder( Parent from )
285     {
286         return newBuilder( from, false );
287     }
288 
289     /**
290      * Creates a new {@code Parent} builder instance using the specified object as a basis.
291      *
292      * @param from the {@code Parent} instance to use as a basis
293      * @param forceCopy the boolean indicating if a copy should be forced
294      * @return a new {@code Builder}
295      */
296     @Nonnull
297     public static Builder newBuilder( Parent from, boolean forceCopy )
298     {
299         return new Builder( from, forceCopy );
300     }
301 
302     /**
303      * Builder class used to create Parent instances.
304      * @see #with()
305      * @see #newBuilder()
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      * @return the id as {@code groupId:artifactId:version}
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 }