1
2
3
4
5 package org.apache.maven.api.model;
6
7 import java.io.Serializable;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.Set;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19 import org.apache.maven.api.annotations.Experimental;
20 import org.apache.maven.api.annotations.Generated;
21 import org.apache.maven.api.annotations.Immutable;
22 import org.apache.maven.api.annotations.Nonnull;
23 import org.apache.maven.api.annotations.NotThreadSafe;
24 import org.apache.maven.api.annotations.ThreadSafe;
25
26
27
28
29 @Experimental
30 @Generated @ThreadSafe @Immutable
31 public class ModelBase
32 implements Serializable, InputLocationTracker
33 {
34
35
36
37 @Deprecated(since = "4.0.0")
38 final List<String> modules;
39
40
41
42
43
44
45 final List<String> subprojects;
46
47
48
49
50 final DistributionManagement distributionManagement;
51
52
53
54
55
56 final Map<String, String> properties;
57
58
59
60
61
62
63
64 final DependencyManagement dependencyManagement;
65
66
67
68
69
70
71
72
73 final List<Dependency> dependencies;
74
75
76
77
78 final List<Repository> repositories;
79
80
81
82
83 final List<Repository> pluginRepositories;
84
85
86
87
88
89
90 final Reporting reporting;
91
92 final Map<Object, InputLocation> locations;
93
94 final InputLocation importedFrom;
95
96
97
98
99
100 protected ModelBase(Builder builder) {
101 this.modules = ImmutableCollections.copy(builder.modules != null ? builder.modules : (builder.base != null ? builder.base.modules : null));
102 this.subprojects = ImmutableCollections.copy(builder.subprojects != null ? builder.subprojects : (builder.base != null ? builder.base.subprojects : null));
103 this.distributionManagement = builder.distributionManagement != null ? builder.distributionManagement : (builder.base != null ? builder.base.distributionManagement : null);
104 this.properties = ImmutableCollections.copy(builder.properties != null ? builder.properties : (builder.base != null ? builder.base.properties : null));
105 this.dependencyManagement = builder.dependencyManagement != null ? builder.dependencyManagement : (builder.base != null ? builder.base.dependencyManagement : null);
106 this.dependencies = ImmutableCollections.copy(builder.dependencies != null ? builder.dependencies : (builder.base != null ? builder.base.dependencies : null));
107 this.repositories = ImmutableCollections.copy(builder.repositories != null ? builder.repositories : (builder.base != null ? builder.base.repositories : null));
108 this.pluginRepositories = ImmutableCollections.copy(builder.pluginRepositories != null ? builder.pluginRepositories : (builder.base != null ? builder.base.pluginRepositories : null));
109 this.reporting = builder.reporting != null ? builder.reporting : (builder.base != null ? builder.base.reporting : null);
110 this.locations = builder.computeLocations();
111 this.importedFrom = builder.importedFrom;
112 }
113
114
115
116
117
118
119 @Deprecated(since = "4.0.0")
120 @Nonnull
121 public List<String> getModules() {
122 return this.modules;
123 }
124
125
126
127
128
129
130
131
132
133 @Nonnull
134 public List<String> getSubprojects() {
135 return this.subprojects;
136 }
137
138
139
140
141
142
143
144 public DistributionManagement getDistributionManagement() {
145 return this.distributionManagement;
146 }
147
148
149
150
151
152
153
154
155 @Nonnull
156 public Map<String, String> getProperties() {
157 return this.properties;
158 }
159
160
161
162
163
164
165
166
167
168
169 public DependencyManagement getDependencyManagement() {
170 return this.dependencyManagement;
171 }
172
173
174
175
176
177
178
179
180
181
182
183 @Nonnull
184 public List<Dependency> getDependencies() {
185 return this.dependencies;
186 }
187
188
189
190
191
192
193
194 @Nonnull
195 public List<Repository> getRepositories() {
196 return this.repositories;
197 }
198
199
200
201
202
203
204
205 @Nonnull
206 public List<Repository> getPluginRepositories() {
207 return this.pluginRepositories;
208 }
209
210
211
212
213
214
215
216
217
218 public Reporting getReporting() {
219 return this.reporting;
220 }
221
222
223
224
225 public InputLocation getLocation(Object key) {
226 return locations.get(key);
227 }
228
229
230
231
232 public Set<Object> getLocationKeys() {
233 return locations.keySet();
234 }
235
236 protected Stream<Object> getLocationKeyStream() {
237 return locations.keySet().stream();
238 }
239
240
241
242
243 public InputLocation getImportedFrom() {
244 return importedFrom;
245 }
246
247
248
249
250
251
252 @Nonnull
253 public Builder with() {
254 return newBuilder(this);
255 }
256
257
258
259
260
261
262 @Deprecated(since = "4.0.0")
263 @Nonnull
264 public ModelBase withModules(Collection<String> modules) {
265 return newBuilder(this, true).modules(modules).build();
266 }
267
268
269
270
271
272
273 @Nonnull
274 public ModelBase withSubprojects(Collection<String> subprojects) {
275 return newBuilder(this, true).subprojects(subprojects).build();
276 }
277
278
279
280
281
282
283 @Nonnull
284 public ModelBase withDistributionManagement(DistributionManagement distributionManagement) {
285 return newBuilder(this, true).distributionManagement(distributionManagement).build();
286 }
287
288
289
290
291
292
293 @Nonnull
294 public ModelBase withProperties(Map<String, String> properties) {
295 return newBuilder(this, true).properties(properties).build();
296 }
297
298
299
300
301
302
303 @Nonnull
304 public ModelBase withDependencyManagement(DependencyManagement dependencyManagement) {
305 return newBuilder(this, true).dependencyManagement(dependencyManagement).build();
306 }
307
308
309
310
311
312
313 @Nonnull
314 public ModelBase withDependencies(Collection<Dependency> dependencies) {
315 return newBuilder(this, true).dependencies(dependencies).build();
316 }
317
318
319
320
321
322
323 @Nonnull
324 public ModelBase withRepositories(Collection<Repository> repositories) {
325 return newBuilder(this, true).repositories(repositories).build();
326 }
327
328
329
330
331
332
333 @Nonnull
334 public ModelBase withPluginRepositories(Collection<Repository> pluginRepositories) {
335 return newBuilder(this, true).pluginRepositories(pluginRepositories).build();
336 }
337
338
339
340
341
342
343 @Nonnull
344 public ModelBase withReporting(Reporting reporting) {
345 return newBuilder(this, true).reporting(reporting).build();
346 }
347
348
349
350
351
352
353
354
355 @Nonnull
356 public static ModelBase newInstance() {
357 return newInstance(true);
358 }
359
360
361
362
363
364
365
366
367 @Nonnull
368 public static ModelBase newInstance(boolean withDefaults) {
369 return newBuilder(withDefaults).build();
370 }
371
372
373
374
375
376
377
378
379 @Nonnull
380 public static Builder newBuilder() {
381 return newBuilder(true);
382 }
383
384
385
386
387
388
389
390 @Nonnull
391 public static Builder newBuilder(boolean withDefaults) {
392 return new Builder(withDefaults);
393 }
394
395
396
397
398
399
400
401
402 @Nonnull
403 public static Builder newBuilder(ModelBase from) {
404 return newBuilder(from, false);
405 }
406
407
408
409
410
411
412
413
414 @Nonnull
415 public static Builder newBuilder(ModelBase from, boolean forceCopy) {
416 return new Builder(from, forceCopy);
417 }
418
419
420
421
422
423
424 @NotThreadSafe
425 public static class Builder
426 {
427 ModelBase base;
428 Collection<String> modules;
429 Collection<String> subprojects;
430 DistributionManagement distributionManagement;
431 Map<String, String> properties;
432 DependencyManagement dependencyManagement;
433 Collection<Dependency> dependencies;
434 Collection<Repository> repositories;
435 Collection<Repository> pluginRepositories;
436 Reporting reporting;
437 Map<Object, InputLocation> locations;
438 InputLocation importedFrom;
439
440 protected Builder(boolean withDefaults) {
441 if (withDefaults) {
442 }
443 }
444
445 protected Builder(ModelBase base, boolean forceCopy) {
446 if (forceCopy) {
447 this.modules = base.modules;
448 this.subprojects = base.subprojects;
449 this.distributionManagement = base.distributionManagement;
450 this.properties = base.properties;
451 this.dependencyManagement = base.dependencyManagement;
452 this.dependencies = base.dependencies;
453 this.repositories = base.repositories;
454 this.pluginRepositories = base.pluginRepositories;
455 this.reporting = base.reporting;
456 this.locations = base.locations;
457 this.importedFrom = base.importedFrom;
458 } else {
459 this.base = base;
460 }
461 }
462
463 @Deprecated(since = "4.0.0")
464 @Nonnull
465 public Builder modules(Collection<String> modules) {
466 this.modules = modules;
467 return this;
468 }
469
470 @Nonnull
471 public Builder subprojects(Collection<String> subprojects) {
472 this.subprojects = subprojects;
473 return this;
474 }
475
476 @Nonnull
477 public Builder distributionManagement(DistributionManagement distributionManagement) {
478 this.distributionManagement = distributionManagement;
479 return this;
480 }
481
482 @Nonnull
483 public Builder properties(Map<String, String> properties) {
484 this.properties = properties;
485 return this;
486 }
487
488 @Nonnull
489 public Builder dependencyManagement(DependencyManagement dependencyManagement) {
490 this.dependencyManagement = dependencyManagement;
491 return this;
492 }
493
494 @Nonnull
495 public Builder dependencies(Collection<Dependency> dependencies) {
496 this.dependencies = dependencies;
497 return this;
498 }
499
500 @Nonnull
501 public Builder repositories(Collection<Repository> repositories) {
502 this.repositories = repositories;
503 return this;
504 }
505
506 @Nonnull
507 public Builder pluginRepositories(Collection<Repository> pluginRepositories) {
508 this.pluginRepositories = pluginRepositories;
509 return this;
510 }
511
512 @Nonnull
513 public Builder reporting(Reporting reporting) {
514 this.reporting = reporting;
515 return this;
516 }
517
518
519 @Nonnull
520 public Builder location(Object key, InputLocation location) {
521 if (location != null) {
522 if (!(this.locations instanceof HashMap)) {
523 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
524 }
525 this.locations.put(key, location);
526 }
527 return this;
528 }
529
530 @Nonnull
531 public Builder importedFrom(InputLocation importedFrom) {
532 this.importedFrom = importedFrom;
533 return this;
534 }
535
536 @Nonnull
537 public ModelBase build() {
538
539 if (base != null
540 && (modules == null || modules == base.modules)
541 && (subprojects == null || subprojects == base.subprojects)
542 && (distributionManagement == null || distributionManagement == base.distributionManagement)
543 && (properties == null || properties == base.properties)
544 && (dependencyManagement == null || dependencyManagement == base.dependencyManagement)
545 && (dependencies == null || dependencies == base.dependencies)
546 && (repositories == null || repositories == base.repositories)
547 && (pluginRepositories == null || pluginRepositories == base.pluginRepositories)
548 && (reporting == null || reporting == base.reporting)
549 ) {
550 return base;
551 }
552 return new ModelBase(this);
553 }
554
555 Map<Object, InputLocation> computeLocations() {
556 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
557 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
558 if (newlocs.isEmpty()) {
559 return Map.copyOf(oldlocs);
560 }
561 if (oldlocs.isEmpty()) {
562 return Map.copyOf(newlocs);
563 }
564 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
565
566 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
567 }
568 }
569
570 }