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      /** Locations */
55      final Map<Object, InputLocation> locations;
56  
57      /**
58        * Constructor for this class, package protected.
59        * @see Builder#build()
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       * The group id of the parent project to inherit from.
77       *
78       * @return a {@code String}
79       */
80      public String getGroupId() {
81          return this.groupId;
82      }
83  
84      /**
85       * The artifact id of the parent project to inherit from.
86       *
87       * @return a {@code String}
88       */
89      public String getArtifactId() {
90          return this.artifactId;
91      }
92  
93      /**
94       * The version of the parent project to inherit.
95       *
96       * @return a {@code String}
97       */
98      public String getVersion() {
99          return this.version;
100     }
101 
102     /**
103      * The relative path of the parent {@code pom.xml} file within the check out.
104      * If not specified, it defaults to {@code ../pom.xml}.
105      * Maven looks for the parent POM first in this location on
106      * the filesystem, then the local repository, and lastly in the remote repo.
107      * {@code relativePath} allows you to select a different location,
108      * for example when your structure is flat, or deeper without an intermediate parent POM.
109      * However, the group ID, artifact ID and version are still required,
110      * and must match the file in the location given or it will revert to the repository for the POM.
111      * This feature is only for enhancing the development in a local checkout of that project.
112      * Set the value to an empty string in case you want to disable the feature and always resolve
113      * the parent POM from the repositories.
114      *
115      * @return a {@code String}
116      */
117     public String getRelativePath() {
118         return this.relativePath;
119     }
120 
121     /**
122      * Gets the location of the specified field in the input source.
123      */
124     public InputLocation getLocation(Object key) {
125         return locations != null ? locations.get(key) : null;
126     }
127 
128     /**
129      * Creates a new builder with this object as the basis.
130      *
131      * @return a {@code Builder}
132      */
133     @Nonnull
134     public Builder with() {
135         return newBuilder(this);
136     }
137     /**
138      * Creates a new {@code Parent} instance using the specified groupId.
139      *
140      * @param groupId the new {@code String} to use
141      * @return a {@code Parent} with the specified groupId
142      */
143     @Nonnull
144     public Parent withGroupId(String groupId) {
145         return newBuilder(this, true).groupId(groupId).build();
146     }
147     /**
148      * Creates a new {@code Parent} instance using the specified artifactId.
149      *
150      * @param artifactId the new {@code String} to use
151      * @return a {@code Parent} with the specified artifactId
152      */
153     @Nonnull
154     public Parent withArtifactId(String artifactId) {
155         return newBuilder(this, true).artifactId(artifactId).build();
156     }
157     /**
158      * Creates a new {@code Parent} instance using the specified version.
159      *
160      * @param version the new {@code String} to use
161      * @return a {@code Parent} with the specified version
162      */
163     @Nonnull
164     public Parent withVersion(String version) {
165         return newBuilder(this, true).version(version).build();
166     }
167     /**
168      * Creates a new {@code Parent} instance using the specified relativePath.
169      *
170      * @param relativePath the new {@code String} to use
171      * @return a {@code Parent} with the specified relativePath
172      */
173     @Nonnull
174     public Parent withRelativePath(String relativePath) {
175         return newBuilder(this, true).relativePath(relativePath).build();
176     }
177 
178     /**
179      * Creates a new {@code Parent} instance.
180      * Equivalent to {@code newInstance(true)}.
181      * @see #newInstance(boolean)
182      *
183      * @return a new {@code Parent}
184      */
185     @Nonnull
186     public static Parent newInstance() {
187         return newInstance(true);
188     }
189 
190     /**
191      * Creates a new {@code Parent} instance using default values or not.
192      * Equivalent to {@code newBuilder(withDefaults).build()}.
193      *
194      * @param withDefaults the boolean indicating whether default values should be used
195      * @return a new {@code Parent}
196      */
197     @Nonnull
198     public static Parent newInstance(boolean withDefaults) {
199         return newBuilder(withDefaults).build();
200     }
201 
202     /**
203      * Creates a new {@code Parent} builder instance.
204      * Equivalent to {@code newBuilder(true)}.
205      * @see #newBuilder(boolean)
206      *
207      * @return a new {@code Builder}
208      */
209     @Nonnull
210     public static Builder newBuilder() {
211         return newBuilder(true);
212     }
213 
214     /**
215      * Creates a new {@code Parent} builder instance using default values or not.
216      *
217      * @param withDefaults the boolean indicating whether default values should be used
218      * @return a new {@code Builder}
219      */
220     @Nonnull
221     public static Builder newBuilder(boolean withDefaults) {
222         return new Builder(withDefaults);
223     }
224 
225     /**
226      * Creates a new {@code Parent} builder instance using the specified object as a basis.
227      * Equivalent to {@code newBuilder(from, false)}.
228      *
229      * @param from the {@code Parent} instance to use as a basis
230      * @return a new {@code Builder}
231      */
232     @Nonnull
233     public static Builder newBuilder(Parent from) {
234         return newBuilder(from, false);
235     }
236 
237     /**
238      * Creates a new {@code Parent} builder instance using the specified object as a basis.
239      *
240      * @param from the {@code Parent} instance to use as a basis
241      * @param forceCopy the boolean indicating if a copy should be forced
242      * @return a new {@code Builder}
243      */
244     @Nonnull
245     public static Builder newBuilder(Parent from, boolean forceCopy) {
246         return new Builder(from, forceCopy);
247     }
248 
249     /**
250      * Builder class used to create Parent instances.
251      * @see #with()
252      * @see #newBuilder()
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      * @return the id as {@code groupId:artifactId:version}
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 }