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 * @param key the key of the field, must not be {@code null}
114 * @return the location of the field in the input source or {@code null} if unknown
115 * @throws NullPointerException if {@code key} is {@code null}
116 */
117 public InputLocation getLocation(Object key) {
118 Objects.requireNonNull(key, "key");
119 return locations.get(key);
120 }
121
122 /**
123 * Gets the keys of the locations of the input source.
124 */
125 public Set<Object> getLocationKeys() {
126 return locations.keySet();
127 }
128
129 protected Stream<Object> getLocationKeyStream() {
130 return locations.keySet().stream();
131 }
132
133 /**
134 * Gets the input location that caused this model to be read.
135 */
136 public InputLocation getImportedFrom() {
137 return importedFrom;
138 }
139
140 /**
141 * Creates a new builder with this object as the basis.
142 *
143 * @return a {@code Builder}
144 */
145 @Nonnull
146 public Builder with() {
147 return newBuilder(this);
148 }
149 /**
150 * Creates a new {@code ActivationOS} instance using the specified name.
151 *
152 * @param name the new {@code String} to use
153 * @return a {@code ActivationOS} with the specified name
154 */
155 @Nonnull
156 public ActivationOS withName(String name) {
157 return newBuilder(this, true).name(name).build();
158 }
159 /**
160 * Creates a new {@code ActivationOS} instance using the specified family.
161 *
162 * @param family the new {@code String} to use
163 * @return a {@code ActivationOS} with the specified family
164 */
165 @Nonnull
166 public ActivationOS withFamily(String family) {
167 return newBuilder(this, true).family(family).build();
168 }
169 /**
170 * Creates a new {@code ActivationOS} instance using the specified arch.
171 *
172 * @param arch the new {@code String} to use
173 * @return a {@code ActivationOS} with the specified arch
174 */
175 @Nonnull
176 public ActivationOS withArch(String arch) {
177 return newBuilder(this, true).arch(arch).build();
178 }
179 /**
180 * Creates a new {@code ActivationOS} instance using the specified version.
181 *
182 * @param version the new {@code String} to use
183 * @return a {@code ActivationOS} with the specified version
184 */
185 @Nonnull
186 public ActivationOS withVersion(String version) {
187 return newBuilder(this, true).version(version).build();
188 }
189
190 /**
191 * Creates a new {@code ActivationOS} instance.
192 * Equivalent to {@code newInstance(true)}.
193 * @see #newInstance(boolean)
194 *
195 * @return a new {@code ActivationOS}
196 */
197 @Nonnull
198 public static ActivationOS newInstance() {
199 return newInstance(true);
200 }
201
202 /**
203 * Creates a new {@code ActivationOS} instance using default values or not.
204 * Equivalent to {@code newBuilder(withDefaults).build()}.
205 *
206 * @param withDefaults the boolean indicating whether default values should be used
207 * @return a new {@code ActivationOS}
208 */
209 @Nonnull
210 public static ActivationOS newInstance(boolean withDefaults) {
211 return newBuilder(withDefaults).build();
212 }
213
214 /**
215 * Creates a new {@code ActivationOS} builder instance.
216 * Equivalent to {@code newBuilder(true)}.
217 * @see #newBuilder(boolean)
218 *
219 * @return a new {@code Builder}
220 */
221 @Nonnull
222 public static Builder newBuilder() {
223 return newBuilder(true);
224 }
225
226 /**
227 * Creates a new {@code ActivationOS} builder instance using default values or not.
228 *
229 * @param withDefaults the boolean indicating whether default values should be used
230 * @return a new {@code Builder}
231 */
232 @Nonnull
233 public static Builder newBuilder(boolean withDefaults) {
234 return new Builder(withDefaults);
235 }
236
237 /**
238 * Creates a new {@code ActivationOS} builder instance using the specified object as a basis.
239 * Equivalent to {@code newBuilder(from, false)}.
240 *
241 * @param from the {@code ActivationOS} instance to use as a basis
242 * @return a new {@code Builder}
243 */
244 @Nonnull
245 public static Builder newBuilder(ActivationOS from) {
246 return newBuilder(from, false);
247 }
248
249 /**
250 * Creates a new {@code ActivationOS} builder instance using the specified object as a basis.
251 *
252 * @param from the {@code ActivationOS} instance to use as a basis
253 * @param forceCopy the boolean indicating if a copy should be forced
254 * @return a new {@code Builder}
255 */
256 @Nonnull
257 public static Builder newBuilder(ActivationOS from, boolean forceCopy) {
258 return new Builder(from, forceCopy);
259 }
260
261 /**
262 * Builder class used to create ActivationOS instances.
263 * @see #with()
264 * @see #newBuilder()
265 */
266 @NotThreadSafe
267 public static class Builder
268 {
269 ActivationOS base;
270 String name;
271 String family;
272 String arch;
273 String version;
274 Map<Object, InputLocation> locations;
275 InputLocation importedFrom;
276
277 protected Builder(boolean withDefaults) {
278 if (withDefaults) {
279 }
280 }
281
282 protected Builder(ActivationOS base, boolean forceCopy) {
283 if (forceCopy) {
284 this.name = base.name;
285 this.family = base.family;
286 this.arch = base.arch;
287 this.version = base.version;
288 this.locations = base.locations;
289 this.importedFrom = base.importedFrom;
290 } else {
291 this.base = base;
292 }
293 }
294
295 @Nonnull
296 public Builder name(String name) {
297 this.name = name;
298 return this;
299 }
300
301 @Nonnull
302 public Builder family(String family) {
303 this.family = family;
304 return this;
305 }
306
307 @Nonnull
308 public Builder arch(String arch) {
309 this.arch = arch;
310 return this;
311 }
312
313 @Nonnull
314 public Builder version(String version) {
315 this.version = version;
316 return this;
317 }
318
319
320 @Nonnull
321 public Builder location(Object key, InputLocation location) {
322 if (location != null) {
323 if (!(this.locations instanceof HashMap)) {
324 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
325 }
326 this.locations.put(key, location);
327 }
328 return this;
329 }
330
331 @Nonnull
332 public Builder importedFrom(InputLocation importedFrom) {
333 this.importedFrom = importedFrom;
334 return this;
335 }
336
337 @Nonnull
338 public ActivationOS build() {
339 // this method should not contain any logic other than creating (or reusing) an object in order to ease subclassing
340 if (base != null
341 && (name == null || name == base.name)
342 && (family == null || family == base.family)
343 && (arch == null || arch == base.arch)
344 && (version == null || version == base.version)
345 ) {
346 return base;
347 }
348 return new ActivationOS(this);
349 }
350
351 Map<Object, InputLocation> computeLocations() {
352 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
353 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
354 if (newlocs.isEmpty()) {
355 return Map.copyOf(oldlocs);
356 }
357 if (oldlocs.isEmpty()) {
358 return Map.copyOf(newlocs);
359 }
360 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
361 // Keep value from newlocs in case of duplicates
362 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
363 }
364 }
365
366 }