1
2
3
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
25
26
27 @Experimental
28 @Generated @ThreadSafe @Immutable
29 public class Activation
30 implements Serializable, InputLocationTracker
31 {
32
33
34
35 final boolean activeByDefault;
36
37
38
39 final String jdk;
40
41
42
43 final ActivationOS os;
44
45
46
47 final ActivationProperty property;
48
49
50
51 final ActivationFile file;
52
53
54
55 final String packaging;
56
57
58
59 final String condition;
60
61 final Map<Object, InputLocation> locations;
62
63 final InputLocation importedFrom;
64
65
66
67
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
83
84
85
86 public boolean isActiveByDefault() {
87 return this.activeByDefault;
88 }
89
90
91
92
93
94
95 public String getJdk() {
96 return this.jdk;
97 }
98
99
100
101
102
103
104 public ActivationOS getOs() {
105 return this.os;
106 }
107
108
109
110
111
112
113 public ActivationProperty getProperty() {
114 return this.property;
115 }
116
117
118
119
120
121
122 public ActivationFile getFile() {
123 return this.file;
124 }
125
126
127
128
129
130
131 public String getPackaging() {
132 return this.packaging;
133 }
134
135
136
137
138
139
140 public String getCondition() {
141 return this.condition;
142 }
143
144
145
146
147 public InputLocation getLocation(Object key) {
148 return locations.get(key);
149 }
150
151
152
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
164
165 public InputLocation getImportedFrom() {
166 return importedFrom;
167 }
168
169
170
171
172
173
174 @Nonnull
175 public Builder with() {
176 return newBuilder(this);
177 }
178
179
180
181
182
183
184 @Nonnull
185 public Activation withActiveByDefault(boolean activeByDefault) {
186 return newBuilder(this, true).activeByDefault(activeByDefault).build();
187 }
188
189
190
191
192
193
194 @Nonnull
195 public Activation withJdk(String jdk) {
196 return newBuilder(this, true).jdk(jdk).build();
197 }
198
199
200
201
202
203
204 @Nonnull
205 public Activation withOs(ActivationOS os) {
206 return newBuilder(this, true).os(os).build();
207 }
208
209
210
211
212
213
214 @Nonnull
215 public Activation withProperty(ActivationProperty property) {
216 return newBuilder(this, true).property(property).build();
217 }
218
219
220
221
222
223
224 @Nonnull
225 public Activation withFile(ActivationFile file) {
226 return newBuilder(this, true).file(file).build();
227 }
228
229
230
231
232
233
234 @Nonnull
235 public Activation withPackaging(String packaging) {
236 return newBuilder(this, true).packaging(packaging).build();
237 }
238
239
240
241
242
243
244 @Nonnull
245 public Activation withCondition(String condition) {
246 return newBuilder(this, true).condition(condition).build();
247 }
248
249
250
251
252
253
254
255
256 @Nonnull
257 public static Activation newInstance() {
258 return newInstance(true);
259 }
260
261
262
263
264
265
266
267
268 @Nonnull
269 public static Activation newInstance(boolean withDefaults) {
270 return newBuilder(withDefaults).build();
271 }
272
273
274
275
276
277
278
279
280 @Nonnull
281 public static Builder newBuilder() {
282 return newBuilder(true);
283 }
284
285
286
287
288
289
290
291 @Nonnull
292 public static Builder newBuilder(boolean withDefaults) {
293 return new Builder(withDefaults);
294 }
295
296
297
298
299
300
301
302
303 @Nonnull
304 public static Builder newBuilder(Activation from) {
305 return newBuilder(from, false);
306 }
307
308
309
310
311
312
313
314
315 @Nonnull
316 public static Builder newBuilder(Activation from, boolean forceCopy) {
317 return new Builder(from, forceCopy);
318 }
319
320
321
322
323
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
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
449 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
450 }
451 }
452
453 }