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  import org.apache.maven.api.xml.XmlNode;
23  
24  /**
25   * Describes a build extension to utilise.
26   */
27  @Experimental
28  @Generated @ThreadSafe @Immutable
29  public class Extension
30      implements Serializable, InputLocationTracker
31  {
32      /**
33       * The group ID of the extension's artifact.
34       */
35      final String groupId;
36      /**
37       * The artifact ID of the extension.
38       */
39      final String artifactId;
40      /**
41       * The version of the extension.
42       */
43      final String version;
44      /**
45       * The configuration of the extension.
46       * @since Maven 4.0.0
47       */
48      final XmlNode configuration;
49      /** Locations */
50      final Map<Object, InputLocation> locations;
51      /** Location tracking */
52      final InputLocation importedFrom;
53  
54      /**
55        * Constructor for this class, to be called from its subclasses and {@link Builder}.
56        * @see Builder#build()
57        */
58      protected Extension(Builder builder) {
59          this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
60          this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
61          this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
62          this.configuration = builder.configuration != null ? builder.configuration : (builder.base != null ? builder.base.configuration : null);
63          this.locations = builder.computeLocations();
64          this.importedFrom = builder.importedFrom;
65      }
66  
67      /**
68       * The group ID of the extension's artifact.
69       *
70       * @return a {@code String}
71       */
72      public String getGroupId() {
73          return this.groupId;
74      }
75  
76      /**
77       * The artifact ID of the extension.
78       *
79       * @return a {@code String}
80       */
81      public String getArtifactId() {
82          return this.artifactId;
83      }
84  
85      /**
86       * The version of the extension.
87       *
88       * @return a {@code String}
89       */
90      public String getVersion() {
91          return this.version;
92      }
93  
94      /**
95       * The configuration of the extension.
96       * @since Maven 4.0.0
97       *
98       * @return a {@code XmlNode}
99       */
100     public XmlNode getConfiguration() {
101         return this.configuration;
102     }
103 
104     /**
105      * Gets the location of the specified field in the input source.
106      */
107     public InputLocation getLocation(Object key) {
108         return locations.get(key);
109     }
110 
111     /**
112      * Gets the keys of the locations of the input source.
113      */
114     public Set<Object> getLocationKeys() {
115         return locations.keySet();
116     }
117 
118     protected Stream<Object> getLocationKeyStream() {
119         return locations.keySet().stream();
120     }
121 
122     /**
123      * Gets the input location that caused this model to be read.
124      */
125     public InputLocation getImportedFrom()
126     {
127         return importedFrom;
128     }
129 
130     /**
131      * Creates a new builder with this object as the basis.
132      *
133      * @return a {@code Builder}
134      */
135     @Nonnull
136     public Builder with() {
137         return newBuilder(this);
138     }
139     /**
140      * Creates a new {@code Extension} instance using the specified groupId.
141      *
142      * @param groupId the new {@code String} to use
143      * @return a {@code Extension} with the specified groupId
144      */
145     @Nonnull
146     public Extension withGroupId(String groupId) {
147         return newBuilder(this, true).groupId(groupId).build();
148     }
149     /**
150      * Creates a new {@code Extension} instance using the specified artifactId.
151      *
152      * @param artifactId the new {@code String} to use
153      * @return a {@code Extension} with the specified artifactId
154      */
155     @Nonnull
156     public Extension withArtifactId(String artifactId) {
157         return newBuilder(this, true).artifactId(artifactId).build();
158     }
159     /**
160      * Creates a new {@code Extension} instance using the specified version.
161      *
162      * @param version the new {@code String} to use
163      * @return a {@code Extension} with the specified version
164      */
165     @Nonnull
166     public Extension withVersion(String version) {
167         return newBuilder(this, true).version(version).build();
168     }
169     /**
170      * Creates a new {@code Extension} instance using the specified configuration.
171      *
172      * @param configuration the new {@code XmlNode} to use
173      * @return a {@code Extension} with the specified configuration
174      */
175     @Nonnull
176     public Extension withConfiguration(XmlNode configuration) {
177         return newBuilder(this, true).configuration(configuration).build();
178     }
179 
180     /**
181      * Creates a new {@code Extension} instance.
182      * Equivalent to {@code newInstance(true)}.
183      * @see #newInstance(boolean)
184      *
185      * @return a new {@code Extension}
186      */
187     @Nonnull
188     public static Extension newInstance() {
189         return newInstance(true);
190     }
191 
192     /**
193      * Creates a new {@code Extension} instance using default values or not.
194      * Equivalent to {@code newBuilder(withDefaults).build()}.
195      *
196      * @param withDefaults the boolean indicating whether default values should be used
197      * @return a new {@code Extension}
198      */
199     @Nonnull
200     public static Extension newInstance(boolean withDefaults) {
201         return newBuilder(withDefaults).build();
202     }
203 
204     /**
205      * Creates a new {@code Extension} builder instance.
206      * Equivalent to {@code newBuilder(true)}.
207      * @see #newBuilder(boolean)
208      *
209      * @return a new {@code Builder}
210      */
211     @Nonnull
212     public static Builder newBuilder() {
213         return newBuilder(true);
214     }
215 
216     /**
217      * Creates a new {@code Extension} builder instance using default values or not.
218      *
219      * @param withDefaults the boolean indicating whether default values should be used
220      * @return a new {@code Builder}
221      */
222     @Nonnull
223     public static Builder newBuilder(boolean withDefaults) {
224         return new Builder(withDefaults);
225     }
226 
227     /**
228      * Creates a new {@code Extension} builder instance using the specified object as a basis.
229      * Equivalent to {@code newBuilder(from, false)}.
230      *
231      * @param from the {@code Extension} instance to use as a basis
232      * @return a new {@code Builder}
233      */
234     @Nonnull
235     public static Builder newBuilder(Extension from) {
236         return newBuilder(from, false);
237     }
238 
239     /**
240      * Creates a new {@code Extension} builder instance using the specified object as a basis.
241      *
242      * @param from the {@code Extension} instance to use as a basis
243      * @param forceCopy the boolean indicating if a copy should be forced
244      * @return a new {@code Builder}
245      */
246     @Nonnull
247     public static Builder newBuilder(Extension from, boolean forceCopy) {
248         return new Builder(from, forceCopy);
249     }
250 
251     /**
252      * Builder class used to create Extension instances.
253      * @see #with()
254      * @see #newBuilder()
255      */
256     @NotThreadSafe
257     public static class Builder
258     {
259         Extension base;
260         String groupId;
261         String artifactId;
262         String version;
263         XmlNode configuration;
264         Map<Object, InputLocation> locations;
265         InputLocation importedFrom;
266 
267         protected Builder(boolean withDefaults) {
268             if (withDefaults) {
269             }
270         }
271 
272         protected Builder(Extension base, boolean forceCopy) {
273             if (forceCopy) {
274                 this.groupId = base.groupId;
275                 this.artifactId = base.artifactId;
276                 this.version = base.version;
277                 this.configuration = base.configuration;
278                 this.locations = base.locations;
279                 this.importedFrom = base.importedFrom;
280             } else {
281                 this.base = base;
282             }
283         }
284 
285         @Nonnull
286         public Builder groupId(String groupId) {
287             this.groupId = groupId;
288             return this;
289         }
290 
291         @Nonnull
292         public Builder artifactId(String artifactId) {
293             this.artifactId = artifactId;
294             return this;
295         }
296 
297         @Nonnull
298         public Builder version(String version) {
299             this.version = version;
300             return this;
301         }
302 
303         @Nonnull
304         public Builder configuration(XmlNode configuration) {
305             this.configuration = configuration;
306             return this;
307         }
308 
309 
310         @Nonnull
311         public Builder location(Object key, InputLocation location) {
312             if (location != null) {
313                 if (!(this.locations instanceof HashMap)) {
314                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
315                 }
316                 this.locations.put(key, location);
317             }
318             return this;
319         }
320 
321         @Nonnull
322         public Builder importedFrom(InputLocation importedFrom) {
323             this.importedFrom = importedFrom;
324             return this;
325         }
326 
327         @Nonnull
328         public Extension build() {
329             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
330             if (base != null
331                     && (groupId == null || groupId == base.groupId)
332                     && (artifactId == null || artifactId == base.artifactId)
333                     && (version == null || version == base.version)
334                     && (configuration == null || configuration == base.configuration)
335             ) {
336                 return base;
337             }
338             return new Extension(this);
339         }
340 
341         Map<Object, InputLocation> computeLocations() {
342             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
343             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
344             if (newlocs.isEmpty()) {
345                 return Map.copyOf(oldlocs);
346             }
347             if (oldlocs.isEmpty()) {
348                 return Map.copyOf(newlocs);
349             }
350             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
351                     // Keep value from newlocs in case of duplicates
352                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
353         }
354     }
355 
356 
357             
358     /**
359      * @see java.lang.Object#equals(java.lang.Object)
360      */
361     public boolean equals(Object o) {
362         if (this == o) {
363             return true;
364         } else if (!(o instanceof Extension)) {
365             return false;
366         } else {
367             Extension e = (Extension) o;
368             return java.util.Objects.equals(e.getArtifactId(), getArtifactId())
369                 && java.util.Objects.equals(e.getGroupId(), getGroupId())
370                 && java.util.Objects.equals(e.getVersion(), getVersion());
371         }
372     }
373 
374     /**
375      * @see java.lang.Object#hashCode()
376      */
377     public int hashCode() {
378         return java.util.Objects.hash(getArtifactId(), getGroupId(), getVersion());
379     }
380             
381           
382 }