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