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
30 @Experimental
31 @Generated @ThreadSafe @Immutable
32 public class Dependency
33 implements Serializable, InputLocationTracker
34 {
35
36
37
38
39 final String groupId;
40
41
42
43
44 final String artifactId;
45
46
47
48
49 final String version;
50
51
52
53
54
55
56 final String type;
57
58
59
60
61
62
63
64
65
66
67
68 final String classifier;
69
70
71
72
73
74
75
76
77
78 final String scope;
79
80
81
82
83
84
85
86
87 final String systemPath;
88
89
90
91
92 final List<Exclusion> exclusions;
93
94
95
96
97
98
99
100 final String optional;
101
102 final Map<Object, InputLocation> locations;
103
104 final InputLocation importedFrom;
105
106
107
108
109
110 protected Dependency(Builder builder) {
111 this.groupId = builder.groupId != null ? builder.groupId : (builder.base != null ? builder.base.groupId : null);
112 this.artifactId = builder.artifactId != null ? builder.artifactId : (builder.base != null ? builder.base.artifactId : null);
113 this.version = builder.version != null ? builder.version : (builder.base != null ? builder.base.version : null);
114 this.type = builder.type != null ? builder.type : (builder.base != null ? builder.base.type : null);
115 this.classifier = builder.classifier != null ? builder.classifier : (builder.base != null ? builder.base.classifier : null);
116 this.scope = builder.scope != null ? builder.scope : (builder.base != null ? builder.base.scope : null);
117 this.systemPath = builder.systemPath != null ? builder.systemPath : (builder.base != null ? builder.base.systemPath : null);
118 this.exclusions = ImmutableCollections.copy(builder.exclusions != null ? builder.exclusions : (builder.base != null ? builder.base.exclusions : null));
119 this.optional = builder.optional != null ? builder.optional : (builder.base != null ? builder.base.optional : null);
120 this.locations = builder.computeLocations();
121 this.importedFrom = builder.importedFrom;
122 }
123
124
125
126
127
128
129
130 public String getGroupId() {
131 return this.groupId;
132 }
133
134
135
136
137
138
139
140 public String getArtifactId() {
141 return this.artifactId;
142 }
143
144
145
146
147
148
149
150 public String getVersion() {
151 return this.version;
152 }
153
154
155
156
157
158
159
160
161
162 public String getType() {
163 return this.type;
164 }
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179 public String getClassifier() {
180 return this.classifier;
181 }
182
183
184
185
186
187
188
189
190
191
192
193
194 public String getScope() {
195 return this.scope;
196 }
197
198
199
200
201
202
203
204
205
206
207
208 public String getSystemPath() {
209 return this.systemPath;
210 }
211
212
213
214
215
216
217
218 @Nonnull
219 public List<Exclusion> getExclusions() {
220 return this.exclusions;
221 }
222
223
224
225
226
227
228
229
230
231
232 public String getOptional() {
233 return this.optional;
234 }
235
236
237
238
239 public InputLocation getLocation(Object key) {
240 return locations.get(key);
241 }
242
243
244
245
246 public Set<Object> getLocationKeys() {
247 return locations.keySet();
248 }
249
250 protected Stream<Object> getLocationKeyStream() {
251 return locations.keySet().stream();
252 }
253
254
255
256
257 public InputLocation getImportedFrom()
258 {
259 return importedFrom;
260 }
261
262
263
264
265
266
267 @Nonnull
268 public Builder with() {
269 return newBuilder(this);
270 }
271
272
273
274
275
276
277 @Nonnull
278 public Dependency withGroupId(String groupId) {
279 return newBuilder(this, true).groupId(groupId).build();
280 }
281
282
283
284
285
286
287 @Nonnull
288 public Dependency withArtifactId(String artifactId) {
289 return newBuilder(this, true).artifactId(artifactId).build();
290 }
291
292
293
294
295
296
297 @Nonnull
298 public Dependency withVersion(String version) {
299 return newBuilder(this, true).version(version).build();
300 }
301
302
303
304
305
306
307 @Nonnull
308 public Dependency withType(String type) {
309 return newBuilder(this, true).type(type).build();
310 }
311
312
313
314
315
316
317 @Nonnull
318 public Dependency withClassifier(String classifier) {
319 return newBuilder(this, true).classifier(classifier).build();
320 }
321
322
323
324
325
326
327 @Nonnull
328 public Dependency withScope(String scope) {
329 return newBuilder(this, true).scope(scope).build();
330 }
331
332
333
334
335
336
337 @Nonnull
338 public Dependency withSystemPath(String systemPath) {
339 return newBuilder(this, true).systemPath(systemPath).build();
340 }
341
342
343
344
345
346
347 @Nonnull
348 public Dependency withExclusions(Collection<Exclusion> exclusions) {
349 return newBuilder(this, true).exclusions(exclusions).build();
350 }
351
352
353
354
355
356
357 @Nonnull
358 public Dependency withOptional(String optional) {
359 return newBuilder(this, true).optional(optional).build();
360 }
361
362
363
364
365
366
367
368
369 @Nonnull
370 public static Dependency newInstance() {
371 return newInstance(true);
372 }
373
374
375
376
377
378
379
380
381 @Nonnull
382 public static Dependency newInstance(boolean withDefaults) {
383 return newBuilder(withDefaults).build();
384 }
385
386
387
388
389
390
391
392
393 @Nonnull
394 public static Builder newBuilder() {
395 return newBuilder(true);
396 }
397
398
399
400
401
402
403
404 @Nonnull
405 public static Builder newBuilder(boolean withDefaults) {
406 return new Builder(withDefaults);
407 }
408
409
410
411
412
413
414
415
416 @Nonnull
417 public static Builder newBuilder(Dependency from) {
418 return newBuilder(from, false);
419 }
420
421
422
423
424
425
426
427
428 @Nonnull
429 public static Builder newBuilder(Dependency from, boolean forceCopy) {
430 return new Builder(from, forceCopy);
431 }
432
433
434
435
436
437
438 @NotThreadSafe
439 public static class Builder
440 {
441 Dependency base;
442 String groupId;
443 String artifactId;
444 String version;
445 String type;
446 String classifier;
447 String scope;
448 String systemPath;
449 Collection<Exclusion> exclusions;
450 String optional;
451 Map<Object, InputLocation> locations;
452 InputLocation importedFrom;
453
454 protected Builder(boolean withDefaults) {
455 if (withDefaults) {
456 this.type = "jar";
457 }
458 }
459
460 protected Builder(Dependency base, boolean forceCopy) {
461 if (forceCopy) {
462 this.groupId = base.groupId;
463 this.artifactId = base.artifactId;
464 this.version = base.version;
465 this.type = base.type;
466 this.classifier = base.classifier;
467 this.scope = base.scope;
468 this.systemPath = base.systemPath;
469 this.exclusions = base.exclusions;
470 this.optional = base.optional;
471 this.locations = base.locations;
472 this.importedFrom = base.importedFrom;
473 } else {
474 this.base = base;
475 }
476 }
477
478 @Nonnull
479 public Builder groupId(String groupId) {
480 this.groupId = groupId;
481 return this;
482 }
483
484 @Nonnull
485 public Builder artifactId(String artifactId) {
486 this.artifactId = artifactId;
487 return this;
488 }
489
490 @Nonnull
491 public Builder version(String version) {
492 this.version = version;
493 return this;
494 }
495
496 @Nonnull
497 public Builder type(String type) {
498 this.type = type;
499 return this;
500 }
501
502 @Nonnull
503 public Builder classifier(String classifier) {
504 this.classifier = classifier;
505 return this;
506 }
507
508 @Nonnull
509 public Builder scope(String scope) {
510 this.scope = scope;
511 return this;
512 }
513
514 @Nonnull
515 public Builder systemPath(String systemPath) {
516 this.systemPath = systemPath;
517 return this;
518 }
519
520 @Nonnull
521 public Builder exclusions(Collection<Exclusion> exclusions) {
522 this.exclusions = exclusions;
523 return this;
524 }
525
526 @Nonnull
527 public Builder optional(String optional) {
528 this.optional = optional;
529 return this;
530 }
531
532
533 @Nonnull
534 public Builder location(Object key, InputLocation location) {
535 if (location != null) {
536 if (!(this.locations instanceof HashMap)) {
537 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
538 }
539 this.locations.put(key, location);
540 }
541 return this;
542 }
543
544 @Nonnull
545 public Builder importedFrom(InputLocation importedFrom) {
546 this.importedFrom = importedFrom;
547 return this;
548 }
549
550 @Nonnull
551 public Dependency build() {
552
553 if (base != null
554 && (groupId == null || groupId == base.groupId)
555 && (artifactId == null || artifactId == base.artifactId)
556 && (version == null || version == base.version)
557 && (type == null || type == base.type)
558 && (classifier == null || classifier == base.classifier)
559 && (scope == null || scope == base.scope)
560 && (systemPath == null || systemPath == base.systemPath)
561 && (exclusions == null || exclusions == base.exclusions)
562 && (optional == null || optional == base.optional)
563 ) {
564 return base;
565 }
566 return new Dependency(this);
567 }
568
569 Map<Object, InputLocation> computeLocations() {
570 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
571 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
572 if (newlocs.isEmpty()) {
573 return Map.copyOf(oldlocs);
574 }
575 if (oldlocs.isEmpty()) {
576 return Map.copyOf(newlocs);
577 }
578 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
579
580 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
581 }
582 }
583
584
585
586 public boolean isOptional() {
587 return (getOptional() != null) ? Boolean.parseBoolean(getOptional()) : false;
588 }
589
590
591
592
593
594
595
596
597 public String toString() {
598 return "Dependency {groupId=" + getGroupId() + ", artifactId=" + getArtifactId() + ", version=" + getVersion() + ", type=" + getType() + "}";
599 }
600
601
602
603
604 private volatile String managementKey;
605
606
607
608
609 public String getManagementKey() {
610 if (managementKey == null) {
611 managementKey = getGroupId() + ":" + getArtifactId() + ":" + getType() + (getClassifier() != null ? ":" + getClassifier() : "");
612 }
613 return managementKey;
614 }
615
616
617 }