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