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 ActivationOS
30 implements Serializable, InputLocationTracker
31 {
32
33
34
35 final String name;
36
37
38
39 final String family;
40
41
42
43 final String arch;
44
45
46
47 final String version;
48
49 final Map<Object, InputLocation> locations;
50
51 final InputLocation importedFrom;
52
53
54
55
56
57 protected ActivationOS(Builder builder) {
58 this.name = builder.name != null ? builder.name : (builder.base != null ? builder.base.name : null);
59 this.family = builder.family != null ? builder.family : (builder.base != null ? builder.base.family : null);
60 this.arch = builder.arch != null ? builder.arch : (builder.base != null ? builder.base.arch : null);
61 this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
62 this.locations = builder.computeLocations();
63 this.importedFrom = builder.importedFrom;
64 }
65
66
67
68
69
70
71 public String getName() {
72 return this.name;
73 }
74
75
76
77
78
79
80 public String getFamily() {
81 return this.family;
82 }
83
84
85
86
87
88
89 public String getArch() {
90 return this.arch;
91 }
92
93
94
95
96
97
98 public String getVersion() {
99 return this.version;
100 }
101
102
103
104
105 public InputLocation getLocation(Object key) {
106 return locations.get(key);
107 }
108
109
110
111
112 public Set<Object> getLocationKeys() {
113 return locations.keySet();
114 }
115
116 protected Stream<Object> getLocationKeyStream() {
117 return locations.keySet().stream();
118 }
119
120
121
122
123 public InputLocation getImportedFrom()
124 {
125 return importedFrom;
126 }
127
128
129
130
131
132
133 @Nonnull
134 public Builder with() {
135 return newBuilder(this);
136 }
137
138
139
140
141
142
143 @Nonnull
144 public ActivationOS withName(String name) {
145 return newBuilder(this, true).name(name).build();
146 }
147
148
149
150
151
152
153 @Nonnull
154 public ActivationOS withFamily(String family) {
155 return newBuilder(this, true).family(family).build();
156 }
157
158
159
160
161
162
163 @Nonnull
164 public ActivationOS withArch(String arch) {
165 return newBuilder(this, true).arch(arch).build();
166 }
167
168
169
170
171
172
173 @Nonnull
174 public ActivationOS withVersion(String version) {
175 return newBuilder(this, true).version(version).build();
176 }
177
178
179
180
181
182
183
184
185 @Nonnull
186 public static ActivationOS newInstance() {
187 return newInstance(true);
188 }
189
190
191
192
193
194
195
196
197 @Nonnull
198 public static ActivationOS newInstance(boolean withDefaults) {
199 return newBuilder(withDefaults).build();
200 }
201
202
203
204
205
206
207
208
209 @Nonnull
210 public static Builder newBuilder() {
211 return newBuilder(true);
212 }
213
214
215
216
217
218
219
220 @Nonnull
221 public static Builder newBuilder(boolean withDefaults) {
222 return new Builder(withDefaults);
223 }
224
225
226
227
228
229
230
231
232 @Nonnull
233 public static Builder newBuilder(ActivationOS from) {
234 return newBuilder(from, false);
235 }
236
237
238
239
240
241
242
243
244 @Nonnull
245 public static Builder newBuilder(ActivationOS from, boolean forceCopy) {
246 return new Builder(from, forceCopy);
247 }
248
249
250
251
252
253
254 @NotThreadSafe
255 public static class Builder
256 {
257 ActivationOS base;
258 String name;
259 String family;
260 String arch;
261 String version;
262 Map<Object, InputLocation> locations;
263 InputLocation importedFrom;
264
265 protected Builder(boolean withDefaults) {
266 if (withDefaults) {
267 }
268 }
269
270 protected Builder(ActivationOS base, boolean forceCopy) {
271 if (forceCopy) {
272 this.name = base.name;
273 this.family = base.family;
274 this.arch = base.arch;
275 this.version = base.version;
276 this.locations = base.locations;
277 this.importedFrom = base.importedFrom;
278 } else {
279 this.base = base;
280 }
281 }
282
283 @Nonnull
284 public Builder name(String name) {
285 this.name = name;
286 return this;
287 }
288
289 @Nonnull
290 public Builder family(String family) {
291 this.family = family;
292 return this;
293 }
294
295 @Nonnull
296 public Builder arch(String arch) {
297 this.arch = arch;
298 return this;
299 }
300
301 @Nonnull
302 public Builder version(String version) {
303 this.version = version;
304 return this;
305 }
306
307
308 @Nonnull
309 public Builder location(Object key, InputLocation location) {
310 if (location != null) {
311 if (!(this.locations instanceof HashMap)) {
312 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
313 }
314 this.locations.put(key, location);
315 }
316 return this;
317 }
318
319 @Nonnull
320 public Builder importedFrom(InputLocation importedFrom) {
321 this.importedFrom = importedFrom;
322 return this;
323 }
324
325 @Nonnull
326 public ActivationOS build() {
327
328 if (base != null
329 && (name == null || name == base.name)
330 && (family == null || family == base.family)
331 && (arch == null || arch == base.arch)
332 && (version == null || version == base.version)
333 ) {
334 return base;
335 }
336 return new ActivationOS(this);
337 }
338
339 Map<Object, InputLocation> computeLocations() {
340 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
341 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
342 if (newlocs.isEmpty()) {
343 return Map.copyOf(oldlocs);
344 }
345 if (oldlocs.isEmpty()) {
346 return Map.copyOf(newlocs);
347 }
348 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
349
350 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
351 }
352 }
353
354 }