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 java.util.Set;
12  import org.apache.maven.api.annotations.Experimental;
13  import org.apache.maven.api.annotations.Generated;
14  import org.apache.maven.api.annotations.Immutable;
15  import org.apache.maven.api.annotations.Nonnull;
16  import org.apache.maven.api.annotations.NotThreadSafe;
17  import org.apache.maven.api.annotations.ThreadSafe;
18  
19  /**
20   * The {@code <parent>} element contains information required to locate the parent project from which
21   * this project will inherit from.
22   * <p><strong>Note:</strong> The children of this element are not interpolated and must be given as literal values.</p>
23   */
24  @Experimental
25  @Generated @ThreadSafe @Immutable
26  public class Parent
27      implements Serializable, InputLocationTracker
28  {
29      /**
30       * The group id of the parent project to inherit from.
31       */
32      final String groupId;
33      /**
34       * The artifact id of the parent project to inherit from.
35       */
36      final String artifactId;
37      /**
38       * The version of the parent project to inherit.
39       */
40      final String version;
41      /**
42       * The relative path of the parent subproject POM file or directory within the checkout.
43       * If not specified, it defaults to {@code ..}, i.e. the parent directory.
44       * Maven looks for the parent POM first in this location on
45       * the filesystem if explicitly provided, then in the reactor if groupId and artifactId are provided,
46       * then in the default parent directory, then the local repository, and lastly in the remote repo.
47       * However, if the both relative path and the group ID / artifact ID are provided,
48       * they must match the file in the location given.
49       * Specify either the {@code relativePath} or the {@code groupId}/{@code artifactId}, not both.
50       */
51      final String relativePath;
52      /** Locations (this potentially hides the same name field from the super class) */
53      final Map<Object, InputLocation> locations;
54      /** Location tracking */
55      final InputLocation importedFrom;
56  
57      /**
58        * Constructor for this class, to be called from its subclasses and {@link Builder}.
59        * @see Builder#build()
60        */
61      protected Parent(Builder builder) {
62          this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
63          this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
64          this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
65          this.relativePath = builder.relativePath != null ? builder.relativePath : (builder.base != null ? builder.base.relativePath : null);
66          Map<Object, InputLocation> newlocs = builder.locations != null ? builder.locations : Collections.emptyMap();
67          Map<Object, InputLocation> oldlocs = builder.base != null && builder.base.locations != null ? builder.base.locations : Collections.emptyMap();
68          Map<Object, InputLocation> mutableLocations = new HashMap<>();
69          this.importedFrom = builder.importedFrom;
70          mutableLocations.put("", newlocs.containsKey("") ? newlocs.get("") : oldlocs.get(""));
71          mutableLocations.put("groupId", newlocs.containsKey("groupId") ? newlocs.get("groupId") : oldlocs.get("groupId"));
72          mutableLocations.put("artifactId", newlocs.containsKey("artifactId") ? newlocs.get("artifactId") : oldlocs.get("artifactId"));
73          mutableLocations.put("version", newlocs.containsKey("version") ? newlocs.get("version") : oldlocs.get("version"));
74          mutableLocations.put("relativePath", newlocs.containsKey("relativePath") ? newlocs.get("relativePath") : oldlocs.get("relativePath"));
75          this.locations = Collections.unmodifiableMap(mutableLocations);
76      }
77  
78      /**
79       * The group id of the parent project to inherit from.
80       *
81       * @return a {@code String}
82       */
83      public String getGroupId() {
84          return this.groupId;
85      }
86  
87      /**
88       * The artifact id of the parent project to inherit from.
89       *
90       * @return a {@code String}
91       */
92      public String getArtifactId() {
93          return this.artifactId;
94      }
95  
96      /**
97       * The version of the parent project to inherit.
98       *
99       * @return a {@code String}
100      */
101     public String getVersion() {
102         return this.version;
103     }
104 
105     /**
106      * The relative path of the parent subproject POM file or directory within the checkout.
107      * If not specified, it defaults to {@code ..}, i.e. the parent directory.
108      * Maven looks for the parent POM first in this location on
109      * the filesystem if explicitly provided, then in the reactor if groupId and artifactId are provided,
110      * then in the default parent directory, then the local repository, and lastly in the remote repo.
111      * However, if the both relative path and the group ID / artifact ID are provided,
112      * they must match the file in the location given.
113      * Specify either the {@code relativePath} or the {@code groupId}/{@code artifactId}, not both.
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     * Gets the keys of the locations of the input source.
130     */
131     public Set<Object> getLocationKeys() {
132         return locations != null ? locations.keySet() : null;
133     }
134 
135     /**
136      * Gets the input location that caused this model to be read.
137      */
138     public InputLocation getImportedFrom()
139     {
140         return importedFrom;
141     }
142 
143     /**
144      * Creates a new builder with this object as the basis.
145      *
146      * @return a {@code Builder}
147      */
148     @Nonnull
149     public Builder with() {
150         return newBuilder(this);
151     }
152     /**
153      * Creates a new {@code Parent} instance using the specified groupId.
154      *
155      * @param groupId the new {@code String} to use
156      * @return a {@code Parent} with the specified groupId
157      */
158     @Nonnull
159     public Parent withGroupId(String groupId) {
160         return newBuilder(this, true).groupId(groupId).build();
161     }
162     /**
163      * Creates a new {@code Parent} instance using the specified artifactId.
164      *
165      * @param artifactId the new {@code String} to use
166      * @return a {@code Parent} with the specified artifactId
167      */
168     @Nonnull
169     public Parent withArtifactId(String artifactId) {
170         return newBuilder(this, true).artifactId(artifactId).build();
171     }
172     /**
173      * Creates a new {@code Parent} instance using the specified version.
174      *
175      * @param version the new {@code String} to use
176      * @return a {@code Parent} with the specified version
177      */
178     @Nonnull
179     public Parent withVersion(String version) {
180         return newBuilder(this, true).version(version).build();
181     }
182     /**
183      * Creates a new {@code Parent} instance using the specified relativePath.
184      *
185      * @param relativePath the new {@code String} to use
186      * @return a {@code Parent} with the specified relativePath
187      */
188     @Nonnull
189     public Parent withRelativePath(String relativePath) {
190         return newBuilder(this, true).relativePath(relativePath).build();
191     }
192 
193     /**
194      * Creates a new {@code Parent} instance.
195      * Equivalent to {@code newInstance(true)}.
196      * @see #newInstance(boolean)
197      *
198      * @return a new {@code Parent}
199      */
200     @Nonnull
201     public static Parent newInstance() {
202         return newInstance(true);
203     }
204 
205     /**
206      * Creates a new {@code Parent} instance using default values or not.
207      * Equivalent to {@code newBuilder(withDefaults).build()}.
208      *
209      * @param withDefaults the boolean indicating whether default values should be used
210      * @return a new {@code Parent}
211      */
212     @Nonnull
213     public static Parent newInstance(boolean withDefaults) {
214         return newBuilder(withDefaults).build();
215     }
216 
217     /**
218      * Creates a new {@code Parent} builder instance.
219      * Equivalent to {@code newBuilder(true)}.
220      * @see #newBuilder(boolean)
221      *
222      * @return a new {@code Builder}
223      */
224     @Nonnull
225     public static Builder newBuilder() {
226         return newBuilder(true);
227     }
228 
229     /**
230      * Creates a new {@code Parent} builder instance using default values or not.
231      *
232      * @param withDefaults the boolean indicating whether default values should be used
233      * @return a new {@code Builder}
234      */
235     @Nonnull
236     public static Builder newBuilder(boolean withDefaults) {
237         return new Builder(withDefaults);
238     }
239 
240     /**
241      * Creates a new {@code Parent} builder instance using the specified object as a basis.
242      * Equivalent to {@code newBuilder(from, false)}.
243      *
244      * @param from the {@code Parent} instance to use as a basis
245      * @return a new {@code Builder}
246      */
247     @Nonnull
248     public static Builder newBuilder(Parent from) {
249         return newBuilder(from, false);
250     }
251 
252     /**
253      * Creates a new {@code Parent} builder instance using the specified object as a basis.
254      *
255      * @param from the {@code Parent} instance to use as a basis
256      * @param forceCopy the boolean indicating if a copy should be forced
257      * @return a new {@code Builder}
258      */
259     @Nonnull
260     public static Builder newBuilder(Parent from, boolean forceCopy) {
261         return new Builder(from, forceCopy);
262     }
263 
264     /**
265      * Builder class used to create Parent instances.
266      * @see #with()
267      * @see #newBuilder()
268      */
269     @NotThreadSafe
270     public static class Builder
271     {
272         Parent base;
273         String groupId;
274         String artifactId;
275         String version;
276         String relativePath;
277         Map<Object, InputLocation> locations;
278         InputLocation importedFrom;
279 
280         protected Builder(boolean withDefaults) {
281             if (withDefaults) {
282             }
283         }
284 
285         protected Builder(Parent base, boolean forceCopy) {
286             if (forceCopy) {
287                 this.groupId = base.groupId;
288                 this.artifactId = base.artifactId;
289                 this.version = base.version;
290                 this.relativePath = base.relativePath;
291                 this.locations = base.locations;
292                 this.importedFrom = base.importedFrom;
293             } else {
294                 this.base = base;
295             }
296         }
297 
298         @Nonnull
299         public Builder groupId(String groupId) {
300             this.groupId = groupId;
301             return this;
302         }
303 
304         @Nonnull
305         public Builder artifactId(String artifactId) {
306             this.artifactId = artifactId;
307             return this;
308         }
309 
310         @Nonnull
311         public Builder version(String version) {
312             this.version = version;
313             return this;
314         }
315 
316         @Nonnull
317         public Builder relativePath(String relativePath) {
318             this.relativePath = relativePath;
319             return this;
320         }
321 
322 
323         @Nonnull
324         public Builder location(Object key, InputLocation location) {
325             if (location != null) {
326                 if (!(this.locations instanceof HashMap)) {
327                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
328                 }
329                 this.locations.put(key, location);
330             }
331             return this;
332         }
333 
334         @Nonnull
335         public Builder importedFrom(InputLocation importedFrom) {
336             this.importedFrom = importedFrom;
337             return this;
338         }
339 
340         @Nonnull
341         public Parent build() {
342             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
343             if (base != null
344                     && (groupId == null || groupId == base.groupId)
345                     && (artifactId == null || artifactId == base.artifactId)
346                     && (version == null || version == base.version)
347                     && (relativePath == null || relativePath == base.relativePath)
348             ) {
349                 return base;
350             }
351             return new Parent(this);
352         }
353     }
354 
355 
356             
357     /**
358      * @return the id as {@code groupId:artifactId:version}
359      */
360     public String getId() {
361         return getGroupId() + ":" + getArtifactId() + ":pom:" + getVersion();
362     }
363 
364     @Override
365     public String toString() {
366         return getId();
367     }
368             
369           
370 }