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 {
167 return importedFrom;
168 }
169
170
171
172
173
174
175 @Nonnull
176 public Builder with() {
177 return newBuilder(this);
178 }
179
180
181
182
183
184
185 @Nonnull
186 public Activation withActiveByDefault(boolean activeByDefault) {
187 return newBuilder(this, true).activeByDefault(activeByDefault).build();
188 }
189
190
191
192
193
194
195 @Nonnull
196 public Activation withJdk(String jdk) {
197 return newBuilder(this, true).jdk(jdk).build();
198 }
199
200
201
202
203
204
205 @Nonnull
206 public Activation withOs(ActivationOS os) {
207 return newBuilder(this, true).os(os).build();
208 }
209
210
211
212
213
214
215 @Nonnull
216 public Activation withProperty(ActivationProperty property) {
217 return newBuilder(this, true).property(property).build();
218 }
219
220
221
222
223
224
225 @Nonnull
226 public Activation withFile(ActivationFile file) {
227 return newBuilder(this, true).file(file).build();
228 }
229
230
231
232
233
234
235 @Nonnull
236 public Activation withPackaging(String packaging) {
237 return newBuilder(this, true).packaging(packaging).build();
238 }
239
240
241
242
243
244
245 @Nonnull
246 public Activation withCondition(String condition) {
247 return newBuilder(this, true).condition(condition).build();
248 }
249
250
251
252
253
254
255
256
257 @Nonnull
258 public static Activation newInstance() {
259 return newInstance(true);
260 }
261
262
263
264
265
266
267
268
269 @Nonnull
270 public static Activation newInstance(boolean withDefaults) {
271 return newBuilder(withDefaults).build();
272 }
273
274
275
276
277
278
279
280
281 @Nonnull
282 public static Builder newBuilder() {
283 return newBuilder(true);
284 }
285
286
287
288
289
290
291
292 @Nonnull
293 public static Builder newBuilder(boolean withDefaults) {
294 return new Builder(withDefaults);
295 }
296
297
298
299
300
301
302
303
304 @Nonnull
305 public static Builder newBuilder(Activation from) {
306 return newBuilder(from, false);
307 }
308
309
310
311
312
313
314
315
316 @Nonnull
317 public static Builder newBuilder(Activation from, boolean forceCopy) {
318 return new Builder(from, forceCopy);
319 }
320
321
322
323
324
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
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
450 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
451 }
452 }
453
454 }