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         return importedFrom;
167     }
168 
169     /**
170      * Creates a new builder with this object as the basis.
171      *
172      * @return a {@code Builder}
173      */
174     @Nonnull
175     public Builder with() {
176         return newBuilder(this);
177     }
178     /**
179      * Creates a new {@code Activation} instance using the specified activeByDefault.
180      *
181      * @param activeByDefault the new {@code boolean} to use
182      * @return a {@code Activation} with the specified activeByDefault
183      */
184     @Nonnull
185     public Activation withActiveByDefault(boolean activeByDefault) {
186         return newBuilder(this, true).activeByDefault(activeByDefault).build();
187     }
188     /**
189      * Creates a new {@code Activation} instance using the specified jdk.
190      *
191      * @param jdk the new {@code String} to use
192      * @return a {@code Activation} with the specified jdk
193      */
194     @Nonnull
195     public Activation withJdk(String jdk) {
196         return newBuilder(this, true).jdk(jdk).build();
197     }
198     /**
199      * Creates a new {@code Activation} instance using the specified os.
200      *
201      * @param os the new {@code ActivationOS} to use
202      * @return a {@code Activation} with the specified os
203      */
204     @Nonnull
205     public Activation withOs(ActivationOS os) {
206         return newBuilder(this, true).os(os).build();
207     }
208     /**
209      * Creates a new {@code Activation} instance using the specified property.
210      *
211      * @param property the new {@code ActivationProperty} to use
212      * @return a {@code Activation} with the specified property
213      */
214     @Nonnull
215     public Activation withProperty(ActivationProperty property) {
216         return newBuilder(this, true).property(property).build();
217     }
218     /**
219      * Creates a new {@code Activation} instance using the specified file.
220      *
221      * @param file the new {@code ActivationFile} to use
222      * @return a {@code Activation} with the specified file
223      */
224     @Nonnull
225     public Activation withFile(ActivationFile file) {
226         return newBuilder(this, true).file(file).build();
227     }
228     /**
229      * Creates a new {@code Activation} instance using the specified packaging.
230      *
231      * @param packaging the new {@code String} to use
232      * @return a {@code Activation} with the specified packaging
233      */
234     @Nonnull
235     public Activation withPackaging(String packaging) {
236         return newBuilder(this, true).packaging(packaging).build();
237     }
238     /**
239      * Creates a new {@code Activation} instance using the specified condition.
240      *
241      * @param condition the new {@code String} to use
242      * @return a {@code Activation} with the specified condition
243      */
244     @Nonnull
245     public Activation withCondition(String condition) {
246         return newBuilder(this, true).condition(condition).build();
247     }
248 
249     /**
250      * Creates a new {@code Activation} instance.
251      * Equivalent to {@code newInstance(true)}.
252      * @see #newInstance(boolean)
253      *
254      * @return a new {@code Activation}
255      */
256     @Nonnull
257     public static Activation newInstance() {
258         return newInstance(true);
259     }
260 
261     /**
262      * Creates a new {@code Activation} instance using default values or not.
263      * Equivalent to {@code newBuilder(withDefaults).build()}.
264      *
265      * @param withDefaults the boolean indicating whether default values should be used
266      * @return a new {@code Activation}
267      */
268     @Nonnull
269     public static Activation newInstance(boolean withDefaults) {
270         return newBuilder(withDefaults).build();
271     }
272 
273     /**
274      * Creates a new {@code Activation} builder instance.
275      * Equivalent to {@code newBuilder(true)}.
276      * @see #newBuilder(boolean)
277      *
278      * @return a new {@code Builder}
279      */
280     @Nonnull
281     public static Builder newBuilder() {
282         return newBuilder(true);
283     }
284 
285     /**
286      * Creates a new {@code Activation} builder instance using default values or not.
287      *
288      * @param withDefaults the boolean indicating whether default values should be used
289      * @return a new {@code Builder}
290      */
291     @Nonnull
292     public static Builder newBuilder(boolean withDefaults) {
293         return new Builder(withDefaults);
294     }
295 
296     /**
297      * Creates a new {@code Activation} builder instance using the specified object as a basis.
298      * Equivalent to {@code newBuilder(from, false)}.
299      *
300      * @param from the {@code Activation} instance to use as a basis
301      * @return a new {@code Builder}
302      */
303     @Nonnull
304     public static Builder newBuilder(Activation from) {
305         return newBuilder(from, false);
306     }
307 
308     /**
309      * Creates a new {@code Activation} builder instance using the specified object as a basis.
310      *
311      * @param from the {@code Activation} instance to use as a basis
312      * @param forceCopy the boolean indicating if a copy should be forced
313      * @return a new {@code Builder}
314      */
315     @Nonnull
316     public static Builder newBuilder(Activation from, boolean forceCopy) {
317         return new Builder(from, forceCopy);
318     }
319 
320     /**
321      * Builder class used to create Activation instances.
322      * @see #with()
323      * @see #newBuilder()
324      */
325     @NotThreadSafe
326     public static class Builder
327     {
328         Activation base;
329         Boolean activeByDefault;
330         String jdk;
331         ActivationOS os;
332         ActivationProperty property;
333         ActivationFile file;
334         String packaging;
335         String condition;
336         Map<Object, InputLocation> locations;
337         InputLocation importedFrom;
338 
339         protected Builder(boolean withDefaults) {
340             if (withDefaults) {
341                 this.activeByDefault = false;
342             }
343         }
344 
345         protected Builder(Activation base, boolean forceCopy) {
346             if (forceCopy) {
347                 this.activeByDefault = base.activeByDefault;
348                 this.jdk = base.jdk;
349                 this.os = base.os;
350                 this.property = base.property;
351                 this.file = base.file;
352                 this.packaging = base.packaging;
353                 this.condition = base.condition;
354                 this.locations = base.locations;
355                 this.importedFrom = base.importedFrom;
356             } else {
357                 this.base = base;
358             }
359         }
360 
361         @Nonnull
362         public Builder activeByDefault(boolean activeByDefault) {
363             this.activeByDefault = activeByDefault;
364             return this;
365         }
366 
367         @Nonnull
368         public Builder jdk(String jdk) {
369             this.jdk = jdk;
370             return this;
371         }
372 
373         @Nonnull
374         public Builder os(ActivationOS os) {
375             this.os = os;
376             return this;
377         }
378 
379         @Nonnull
380         public Builder property(ActivationProperty property) {
381             this.property = property;
382             return this;
383         }
384 
385         @Nonnull
386         public Builder file(ActivationFile file) {
387             this.file = file;
388             return this;
389         }
390 
391         @Nonnull
392         public Builder packaging(String packaging) {
393             this.packaging = packaging;
394             return this;
395         }
396 
397         @Nonnull
398         public Builder condition(String condition) {
399             this.condition = condition;
400             return this;
401         }
402 
403 
404         @Nonnull
405         public Builder location(Object key, InputLocation location) {
406             if (location != null) {
407                 if (!(this.locations instanceof HashMap)) {
408                     this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
409                 }
410                 this.locations.put(key, location);
411             }
412             return this;
413         }
414 
415         @Nonnull
416         public Builder importedFrom(InputLocation importedFrom) {
417             this.importedFrom = importedFrom;
418             return this;
419         }
420 
421         @Nonnull
422         public Activation build() {
423             // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
424             if (base != null
425                     && (activeByDefault == null || activeByDefault == base.activeByDefault)
426                     && (jdk == null || jdk == base.jdk)
427                     && (os == null || os == base.os)
428                     && (property == null || property == base.property)
429                     && (file == null || file == base.file)
430                     && (packaging == null || packaging == base.packaging)
431                     && (condition == null || condition == base.condition)
432             ) {
433                 return base;
434             }
435             return new Activation(this);
436         }
437 
438         Map<Object, InputLocation> computeLocations() {
439             Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
440             Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
441             if (newlocs.isEmpty()) {
442                 return Map.copyOf(oldlocs);
443             }
444             if (oldlocs.isEmpty()) {
445                 return Map.copyOf(newlocs);
446             }
447             return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
448                     // Keep value from newlocs in case of duplicates
449                     .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
450         }
451     }
452 
453 }