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.Objects;
12  import java.util.Optional;
13  import java.util.Set;
14  import java.util.stream.Collectors;
15  import java.util.stream.Stream;
16  import org.apache.maven.api.annotations.Experimental;
17  import org.apache.maven.api.annotations.Generated;
18  import org.apache.maven.api.annotations.Immutable;
19  import org.apache.maven.api.annotations.Nonnull;
20  import org.apache.maven.api.annotations.NotThreadSafe;
21  import org.apache.maven.api.annotations.ThreadSafe;
22  
23  /**
24   * The {@code <parent>} element contains information required to locate the parent project from which
25   * this project will inherit from.
26   * <p><strong>Note:</strong> The children of this element are not interpolated and must be given as literal values.</p>
27   */
28  @Experimental
29  @Generated @ThreadSafe @Immutable
30  public class Parent
31      implements Serializable, InputLocationTracker
32  {
33      /**
34       * The group id of the parent project to inherit from.
35       */
36      final String groupId;
37      /**
38       * The artifact id of the parent project to inherit from.
39       */
40      final String artifactId;
41      /**
42       * The version of the parent project to inherit.
43       */
44      final String version;
45      /**
46       * The relative path of the parent subproject POM file or directory within the checkout.
47       * If not specified, it defaults to {@code ..}, i.e. the parent directory.
48       * Maven looks for the parent POM first in this location on
49       * the filesystem if explicitly provided, then in the reactor if groupId and artifactId are provided,
50       * then in the default parent directory, then the local repository, and lastly in the remote repo.
51       * However, if the both relative path and the group ID / artifact ID are provided,
52       * they must match the file in the location given.
53       * Specify either the {@code relativePath} or the {@code groupId}/{@code artifactId}, not both.
54       */
55      final String relativePath;
56      /** Locations */
57      final Map<Object, InputLocation> locations;
58      /** Location tracking */
59      final InputLocation importedFrom;
60  
61      /**
62        * Constructor for this class, to be called from its subclasses and {@link Builder}.
63        * @see Builder#build()
64        */
65      protected Parent(Builder builder) {
66          this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
67          this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
68          this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
69          this.relativePath = builder.relativePath != null ? builder.relativePath : (builder.base != null ? builder.base.relativePath : null);
70          this.locations = builder.computeLocations();
71          this.importedFrom = builder.importedFrom;
72      }
73  
74      /**
75       * The group id of the parent project to inherit from.
76       *
77       * @return a {@code String}
78       */
79      public String getGroupId() {
80          return this.groupId;
81      }
82  
83      /**
84       * The artifact id of the parent project to inherit from.
85       *
86       * @return a {@code String}
87       */
88      public String getArtifactId() {
89          return this.artifactId;
90      }
91  
92      /**
93       * The version of the parent project to inherit.
94       *
95       * @return a {@code String}
96       */
97      public String getVersion() {
98          return this.version;
99      }
100 
101     /**
102      * The relative path of the parent subproject POM file or directory within the checkout.
103      * If not specified, it defaults to {@code ..}, i.e. the parent directory.
104      * Maven looks for the parent POM first in this location on
105      * the filesystem if explicitly provided, then in the reactor if groupId and artifactId are provided,
106      * then in the default parent directory, then the local repository, and lastly in the remote repo.
107      * However, if the both relative path and the group ID / artifact ID are provided,
108      * they must match the file in the location given.
109      * Specify either the {@code relativePath} or the {@code groupId}/{@code artifactId}, not both.
110      *
111      * @return a {@code String}
112      */
113     public String getRelativePath() {
114         return this.relativePath;
115     }
116 
117     /**
118      * Gets the location of the specified field in the input source.
119      */
120     public InputLocation getLocation(Object key) {
121         return locations.get(key);
122     }
123 
124     /**
125      * Gets the keys of the locations of the input source.
126      */
127     public Set<Object> getLocationKeys() {
128         return locations.keySet();
129     }
130 
131     protected Stream<Object> getLocationKeyStream() {
132         return locations.keySet().stream();
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         Map<Object, InputLocation> computeLocations() {
355             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
356             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
357             if (newlocs.isEmpty()) {
358                 return Map.copyOf(oldlocs);
359             }
360             if (oldlocs.isEmpty()) {
361                 return Map.copyOf(newlocs);
362             }
363             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
364                     // Keep value from newlocs in case of duplicates
365                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
366         }
367     }
368 
369 
370             
371     /**
372      * @return the id as {@code groupId:artifactId:version}
373      */
374     public String getId() {
375         return getGroupId() + ":" + getArtifactId() + ":pom:" + getVersion();
376     }
377 
378     @Override
379     public String toString() {
380         return getId();
381     }
382             
383           
384 }