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 return importedFrom;
259 }
260
261
262
263
264
265
266 @Nonnull
267 public Builder with() {
268 return newBuilder(this);
269 }
270
271
272
273
274
275
276 @Nonnull
277 public Dependency withGroupId(String groupId) {
278 return newBuilder(this, true).groupId(groupId).build();
279 }
280
281
282
283
284
285
286 @Nonnull
287 public Dependency withArtifactId(String artifactId) {
288 return newBuilder(this, true).artifactId(artifactId).build();
289 }
290
291
292
293
294
295
296 @Nonnull
297 public Dependency withVersion(String version) {
298 return newBuilder(this, true).version(version).build();
299 }
300
301
302
303
304
305
306 @Nonnull
307 public Dependency withType(String type) {
308 return newBuilder(this, true).type(type).build();
309 }
310
311
312
313
314
315
316 @Nonnull
317 public Dependency withClassifier(String classifier) {
318 return newBuilder(this, true).classifier(classifier).build();
319 }
320
321
322
323
324
325
326 @Nonnull
327 public Dependency withScope(String scope) {
328 return newBuilder(this, true).scope(scope).build();
329 }
330
331
332
333
334
335
336 @Nonnull
337 public Dependency withSystemPath(String systemPath) {
338 return newBuilder(this, true).systemPath(systemPath).build();
339 }
340
341
342
343
344
345
346 @Nonnull
347 public Dependency withExclusions(Collection<Exclusion> exclusions) {
348 return newBuilder(this, true).exclusions(exclusions).build();
349 }
350
351
352
353
354
355
356 @Nonnull
357 public Dependency withOptional(String optional) {
358 return newBuilder(this, true).optional(optional).build();
359 }
360
361
362
363
364
365
366
367
368 @Nonnull
369 public static Dependency newInstance() {
370 return newInstance(true);
371 }
372
373
374
375
376
377
378
379
380 @Nonnull
381 public static Dependency newInstance(boolean withDefaults) {
382 return newBuilder(withDefaults).build();
383 }
384
385
386
387
388
389
390
391
392 @Nonnull
393 public static Builder newBuilder() {
394 return newBuilder(true);
395 }
396
397
398
399
400
401
402
403 @Nonnull
404 public static Builder newBuilder(boolean withDefaults) {
405 return new Builder(withDefaults);
406 }
407
408
409
410
411
412
413
414
415 @Nonnull
416 public static Builder newBuilder(Dependency from) {
417 return newBuilder(from, false);
418 }
419
420
421
422
423
424
425
426
427 @Nonnull
428 public static Builder newBuilder(Dependency from, boolean forceCopy) {
429 return new Builder(from, forceCopy);
430 }
431
432
433
434
435
436
437 @NotThreadSafe
438 public static class Builder
439 {
440 Dependency base;
441 String groupId;
442 String artifactId;
443 String version;
444 String type;
445 String classifier;
446 String scope;
447 String systemPath;
448 Collection<Exclusion> exclusions;
449 String optional;
450 Map<Object, InputLocation> locations;
451 InputLocation importedFrom;
452
453 protected Builder(boolean withDefaults) {
454 if (withDefaults) {
455 this.type = "jar";
456 }
457 }
458
459 protected Builder(Dependency base, boolean forceCopy) {
460 if (forceCopy) {
461 this.groupId = base.groupId;
462 this.artifactId = base.artifactId;
463 this.version = base.version;
464 this.type = base.type;
465 this.classifier = base.classifier;
466 this.scope = base.scope;
467 this.systemPath = base.systemPath;
468 this.exclusions = base.exclusions;
469 this.optional = base.optional;
470 this.locations = base.locations;
471 this.importedFrom = base.importedFrom;
472 } else {
473 this.base = base;
474 }
475 }
476
477 @Nonnull
478 public Builder groupId(String groupId) {
479 this.groupId = groupId;
480 return this;
481 }
482
483 @Nonnull
484 public Builder artifactId(String artifactId) {
485 this.artifactId = artifactId;
486 return this;
487 }
488
489 @Nonnull
490 public Builder version(String version) {
491 this.version = version;
492 return this;
493 }
494
495 @Nonnull
496 public Builder type(String type) {
497 this.type = type;
498 return this;
499 }
500
501 @Nonnull
502 public Builder classifier(String classifier) {
503 this.classifier = classifier;
504 return this;
505 }
506
507 @Nonnull
508 public Builder scope(String scope) {
509 this.scope = scope;
510 return this;
511 }
512
513 @Nonnull
514 public Builder systemPath(String systemPath) {
515 this.systemPath = systemPath;
516 return this;
517 }
518
519 @Nonnull
520 public Builder exclusions(Collection<Exclusion> exclusions) {
521 this.exclusions = exclusions;
522 return this;
523 }
524
525 @Nonnull
526 public Builder optional(String optional) {
527 this.optional = optional;
528 return this;
529 }
530
531
532 @Nonnull
533 public Builder location(Object key, InputLocation location) {
534 if (location != null) {
535 if (!(this.locations instanceof HashMap)) {
536 this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
537 }
538 this.locations.put(key, location);
539 }
540 return this;
541 }
542
543 @Nonnull
544 public Builder importedFrom(InputLocation importedFrom) {
545 this.importedFrom = importedFrom;
546 return this;
547 }
548
549 @Nonnull
550 public Dependency build() {
551
552 if (base != null
553 && (groupId == null || groupId == base.groupId)
554 && (artifactId == null || artifactId == base.artifactId)
555 && (version == null || version == base.version)
556 && (type == null || type == base.type)
557 && (classifier == null || classifier == base.classifier)
558 && (scope == null || scope == base.scope)
559 && (systemPath == null || systemPath == base.systemPath)
560 && (exclusions == null || exclusions == base.exclusions)
561 && (optional == null || optional == base.optional)
562 ) {
563 return base;
564 }
565 return new Dependency(this);
566 }
567
568 Map<Object, InputLocation> computeLocations() {
569 Map<Object, InputLocation> newlocs = locations != null ? locations : Map.of();
570 Map<Object, InputLocation> oldlocs = base != null ? base.locations : Map.of();
571 if (newlocs.isEmpty()) {
572 return Map.copyOf(oldlocs);
573 }
574 if (oldlocs.isEmpty()) {
575 return Map.copyOf(newlocs);
576 }
577 return Stream.concat(newlocs.entrySet().stream(), oldlocs.entrySet().stream())
578
579 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1));
580 }
581 }
582
583
584
585 public boolean isOptional() {
586 return (getOptional() != null) ? Boolean.parseBoolean(getOptional()) : false;
587 }
588
589
590
591
592
593
594
595
596 public String toString() {
597 return "Dependency {groupId=" + getGroupId() + ", artifactId=" + getArtifactId() + ", version=" + getVersion() + ", type=" + getType() + "}";
598 }
599
600
601
602
603 private volatile String managementKey;
604
605
606
607
608 public String getManagementKey() {
609 if (managementKey == null) {
610 managementKey = (getGroupId() + ":" + getArtifactId() + ":" + getType() + (getClassifier() != null ? ":" + getClassifier() : "")).intern();
611 }
612 return managementKey;
613 }
614
615
616 }