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