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.settings;
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 conditions within the build runtime environment which will trigger
25   * the automatic inclusion of the parent build profile.
26   */
27  @Experimental
28  @Generated @ThreadSafe @Immutable
29  public class Activation
30      implements Serializable, InputLocationTracker
31  {
32      /**
33       * Flag specifying whether this profile is active as a default.
34       */
35      final boolean activeByDefault;
36      /**
37       * Specifies that this profile will be activated when a matching JDK is detected.
38       */
39      final String jdk;
40      /**
41       * Specifies that this profile will be activated when matching OS attributes are detected.
42       */
43      final ActivationOS os;
44      /**
45       * Specifies that this profile will be activated when this property is specified.
46       */
47      final ActivationProperty property;
48      /**
49       * Specifies that this profile will be activated based on existence of a file.
50       */
51      final ActivationFile file;
52      /**
53       * Specifies that this profile will be activated based on the project's packaging.
54       */
55      final String packaging;
56      /**
57       * The condition which must be satisfied to activate the profile.
58       */
59      final String condition;
60      /** Locations */
61      final Map<Object, InputLocation> locations;
62      /** Location tracking */
63      final InputLocation importedFrom;
64  
65      /**
66        * Constructor for this class, to be called from its subclasses and {@link Builder}.
67        * @see Builder#build()
68        */
69      protected Activation(Builder builder) {
70          this.activeByDefault = builder.activeByDefault != null ? builder.activeByDefault : (builder.base != null ? builder.base.activeByDefault : false);
71          this.jdk = builder.jdk != null ? builder.jdk : (builder.base != null ? builder.base.jdk : null);
72          this.os = builder.os != null ? builder.os : (builder.base != null ? builder.base.os : null);
73          this.property = builder.property != null ? builder.property : (builder.base != null ? builder.base.property : null);
74          this.file = builder.file != null ? builder.file : (builder.base != null ? builder.base.file : null);
75          this.packaging = builder.packaging != null ? builder.packaging : (builder.base != null ? builder.base.packaging : null);
76          this.condition = builder.condition != null ? builder.condition : (builder.base != null ? builder.base.condition : null);
77          this.locations = builder.computeLocations();
78          this.importedFrom = builder.importedFrom;
79      }
80  
81      /**
82       * Flag specifying whether this profile is active as a default.
83       *
84       * @return a {@code boolean}
85       */
86      public boolean isActiveByDefault() {
87          return this.activeByDefault;
88      }
89  
90      /**
91       * Specifies that this profile will be activated when a matching JDK is detected.
92       *
93       * @return a {@code String}
94       */
95      public String getJdk() {
96          return this.jdk;
97      }
98  
99      /**
100      * Specifies that this profile will be activated when matching OS attributes are detected.
101      *
102      * @return a {@code ActivationOS}
103      */
104     public ActivationOS getOs() {
105         return this.os;
106     }
107 
108     /**
109      * Specifies that this profile will be activated when this property is specified.
110      *
111      * @return a {@code ActivationProperty}
112      */
113     public ActivationProperty getProperty() {
114         return this.property;
115     }
116 
117     /**
118      * Specifies that this profile will be activated based on existence of a file.
119      *
120      * @return a {@code ActivationFile}
121      */
122     public ActivationFile getFile() {
123         return this.file;
124     }
125 
126     /**
127      * Specifies that this profile will be activated based on the project's packaging.
128      *
129      * @return a {@code String}
130      */
131     public String getPackaging() {
132         return this.packaging;
133     }
134 
135     /**
136      * The condition which must be satisfied to activate the profile.
137      *
138      * @return a {@code String}
139      */
140     public String getCondition() {
141         return this.condition;
142     }
143 
144     /**
145      * Gets the location of the specified field in the input source.
146      */
147     public InputLocation getLocation(Object key) {
148         return locations.get(key);
149     }
150 
151     /**
152      * Gets the keys of the locations of the input source.
153      */
154     public Set<Object> getLocationKeys() {
155         return locations.keySet();
156     }
157 
158     protected Stream<Object> getLocationKeyStream() {
159         return locations.keySet().stream();
160     }
161 
162     /**
163      * Gets the input location that caused this model to be read.
164      */
165     public InputLocation getImportedFrom()
166     {
167         return importedFrom;
168     }
169 
170     /**
171      * Creates a new builder with this object as the basis.
172      *
173      * @return a {@code Builder}
174      */
175     @Nonnull
176     public Builder with() {
177         return newBuilder(this);
178     }
179     /**
180      * Creates a new {@code Activation} instance using the specified activeByDefault.
181      *
182      * @param activeByDefault the new {@code boolean} to use
183      * @return a {@code Activation} with the specified activeByDefault
184      */
185     @Nonnull
186     public Activation withActiveByDefault(boolean activeByDefault) {
187         return newBuilder(this, true).activeByDefault(activeByDefault).build();
188     }
189     /**
190      * Creates a new {@code Activation} instance using the specified jdk.
191      *
192      * @param jdk the new {@code String} to use
193      * @return a {@code Activation} with the specified jdk
194      */
195     @Nonnull
196     public Activation withJdk(String jdk) {
197         return newBuilder(this, true).jdk(jdk).build();
198     }
199     /**
200      * Creates a new {@code Activation} instance using the specified os.
201      *
202      * @param os the new {@code ActivationOS} to use
203      * @return a {@code Activation} with the specified os
204      */
205     @Nonnull
206     public Activation withOs(ActivationOS os) {
207         return newBuilder(this, true).os(os).build();
208     }
209     /**
210      * Creates a new {@code Activation} instance using the specified property.
211      *
212      * @param property the new {@code ActivationProperty} to use
213      * @return a {@code Activation} with the specified property
214      */
215     @Nonnull
216     public Activation withProperty(ActivationProperty property) {
217         return newBuilder(this, true).property(property).build();
218     }
219     /**
220      * Creates a new {@code Activation} instance using the specified file.
221      *
222      * @param file the new {@code ActivationFile} to use
223      * @return a {@code Activation} with the specified file
224      */
225     @Nonnull
226     public Activation withFile(ActivationFile file) {
227         return newBuilder(this, true).file(file).build();
228     }
229     /**
230      * Creates a new {@code Activation} instance using the specified packaging.
231      *
232      * @param packaging the new {@code String} to use
233      * @return a {@code Activation} with the specified packaging
234      */
235     @Nonnull
236     public Activation withPackaging(String packaging) {
237         return newBuilder(this, true).packaging(packaging).build();
238     }
239     /**
240      * Creates a new {@code Activation} instance using the specified condition.
241      *
242      * @param condition the new {@code String} to use
243      * @return a {@code Activation} with the specified condition
244      */
245     @Nonnull
246     public Activation withCondition(String condition) {
247         return newBuilder(this, true).condition(condition).build();
248     }
249 
250     /**
251      * Creates a new {@code Activation} instance.
252      * Equivalent to {@code newInstance(true)}.
253      * @see #newInstance(boolean)
254      *
255      * @return a new {@code Activation}
256      */
257     @Nonnull
258     public static Activation newInstance() {
259         return newInstance(true);
260     }
261 
262     /**
263      * Creates a new {@code Activation} instance using default values or not.
264      * Equivalent to {@code newBuilder(withDefaults).build()}.
265      *
266      * @param withDefaults the boolean indicating whether default values should be used
267      * @return a new {@code Activation}
268      */
269     @Nonnull
270     public static Activation newInstance(boolean withDefaults) {
271         return newBuilder(withDefaults).build();
272     }
273 
274     /**
275      * Creates a new {@code Activation} builder instance.
276      * Equivalent to {@code newBuilder(true)}.
277      * @see #newBuilder(boolean)
278      *
279      * @return a new {@code Builder}
280      */
281     @Nonnull
282     public static Builder newBuilder() {
283         return newBuilder(true);
284     }
285 
286     /**
287      * Creates a new {@code Activation} builder instance using default values or not.
288      *
289      * @param withDefaults the boolean indicating whether default values should be used
290      * @return a new {@code Builder}
291      */
292     @Nonnull
293     public static Builder newBuilder(boolean withDefaults) {
294         return new Builder(withDefaults);
295     }
296 
297     /**
298      * Creates a new {@code Activation} builder instance using the specified object as a basis.
299      * Equivalent to {@code newBuilder(from, false)}.
300      *
301      * @param from the {@code Activation} instance to use as a basis
302      * @return a new {@code Builder}
303      */
304     @Nonnull
305     public static Builder newBuilder(Activation from) {
306         return newBuilder(from, false);
307     }
308 
309     /**
310      * Creates a new {@code Activation} builder instance using the specified object as a basis.
311      *
312      * @param from the {@code Activation} instance to use as a basis
313      * @param forceCopy the boolean indicating if a copy should be forced
314      * @return a new {@code Builder}
315      */
316     @Nonnull
317     public static Builder newBuilder(Activation from, boolean forceCopy) {
318         return new Builder(from, forceCopy);
319     }
320 
321     /**
322      * Builder class used to create Activation instances.
323      * @see #with()
324      * @see #newBuilder()
325      */
326     @NotThreadSafe
327     public static class Builder
328     {
329         Activation base;
330         Boolean activeByDefault;
331         String jdk;
332         ActivationOS os;
333         ActivationProperty property;
334         ActivationFile file;
335         String packaging;
336         String condition;
337         Map<Object, InputLocation> locations;
338         InputLocation importedFrom;
339 
340         protected Builder(boolean withDefaults) {
341             if (withDefaults) {
342                 this.activeByDefault = false;
343             }
344         }
345 
346         protected Builder(Activation base, boolean forceCopy) {
347             if (forceCopy) {
348                 this.activeByDefault = base.activeByDefault;
349                 this.jdk = base.jdk;
350                 this.os = base.os;
351                 this.property = base.property;
352                 this.file = base.file;
353                 this.packaging = base.packaging;
354                 this.condition = base.condition;
355                 this.locations = base.locations;
356                 this.importedFrom = base.importedFrom;
357             } else {
358                 this.base = base;
359             }
360         }
361 
362         @Nonnull
363         public Builder activeByDefault(boolean activeByDefault) {
364             this.activeByDefault = activeByDefault;
365             return this;
366         }
367 
368         @Nonnull
369         public Builder jdk(String jdk) {
370             this.jdk = jdk;
371             return this;
372         }
373 
374         @Nonnull
375         public Builder os(ActivationOS os) {
376             this.os = os;
377             return this;
378         }
379 
380         @Nonnull
381         public Builder property(ActivationProperty property) {
382             this.property = property;
383             return this;
384         }
385 
386         @Nonnull
387         public Builder file(ActivationFile file) {
388             this.file = file;
389             return this;
390         }
391 
392         @Nonnull
393         public Builder packaging(String packaging) {
394             this.packaging = packaging;
395             return this;
396         }
397 
398         @Nonnull
399         public Builder condition(String condition) {
400             this.condition = condition;
401             return this;
402         }
403 
404 
405         @Nonnull
406         public Builder location(Object key, InputLocation location) {
407             if (location != null) {
408                 if (!(this.locations instanceof HashMap)) {
409                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
410                 }
411                 this.locations.put(key, location);
412             }
413             return this;
414         }
415 
416         @Nonnull
417         public Builder importedFrom(InputLocation importedFrom) {
418             this.importedFrom = importedFrom;
419             return this;
420         }
421 
422         @Nonnull
423         public Activation build() {
424             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
425             if (base != null
426                     && (activeByDefault == null || activeByDefault == base.activeByDefault)
427                     && (jdk == null || jdk == base.jdk)
428                     && (os == null || os == base.os)
429                     && (property == null || property == base.property)
430                     && (file == null || file == base.file)
431                     && (packaging == null || packaging == base.packaging)
432                     && (condition == null || condition == base.condition)
433             ) {
434                 return base;
435             }
436             return new Activation(this);
437         }
438 
439         Map<Object, InputLocation> computeLocations() {
440             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
441             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
442             if (newlocs.isEmpty()) {
443                 return Map.copyOf(oldlocs);
444             }
445             if (oldlocs.isEmpty()) {
446                 return Map.copyOf(newlocs);
447             }
448             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
449                     // Keep value from newlocs in case of duplicates
450                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
451         }
452     }
453 
454 }