1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.util.Objects;
22
23 import org.apache.maven.api.Artifact;
24 import org.apache.maven.api.Dependency;
25 import org.apache.maven.api.DependencyCoordinate;
26 import org.apache.maven.api.DependencyScope;
27 import org.apache.maven.api.Type;
28 import org.apache.maven.api.Version;
29 import org.apache.maven.api.annotations.Nonnull;
30 import org.apache.maven.api.annotations.Nullable;
31 import org.eclipse.aether.artifact.ArtifactProperties;
32
33 import static org.apache.maven.internal.impl.Utils.nonNull;
34
35 public class DefaultDependency implements Dependency {
36
37 private final InternalSession session;
38 private final org.eclipse.aether.graph.Dependency dependency;
39 private final String key;
40
41 public DefaultDependency(
42 @Nonnull InternalSession session, @Nonnull org.eclipse.aether.graph.Dependency dependency) {
43 this.session = nonNull(session, "session");
44 this.dependency = nonNull(dependency, "dependency");
45 this.key = getGroupId()
46 + ':'
47 + getArtifactId()
48 + ':'
49 + getExtension()
50 + (!getClassifier().isEmpty() ? ":" + getClassifier() : "")
51 + ':'
52 + getVersion();
53 }
54
55 @Override
56 public String key() {
57 return key;
58 }
59
60 @Nonnull
61 public org.eclipse.aether.graph.Dependency getDependency() {
62 return dependency;
63 }
64
65 @Override
66 public String getGroupId() {
67 return dependency.getArtifact().getGroupId();
68 }
69
70 @Override
71 public String getArtifactId() {
72 return dependency.getArtifact().getArtifactId();
73 }
74
75 @Override
76 public String getClassifier() {
77 return dependency.getArtifact().getClassifier();
78 }
79
80 @Override
81 public Version getVersion() {
82 return session.parseVersion(dependency.getArtifact().getVersion());
83 }
84
85 @Override
86 public Version getBaseVersion() {
87 return session.parseVersion(dependency.getArtifact().getBaseVersion());
88 }
89
90 @Override
91 public String getExtension() {
92 return dependency.getArtifact().getExtension();
93 }
94
95 @Override
96 public Type getType() {
97 String type = dependency
98 .getArtifact()
99 .getProperty(ArtifactProperties.TYPE, dependency.getArtifact().getExtension());
100 return session.requireType(type);
101 }
102
103 @Override
104 public boolean isSnapshot() {
105 return DefaultModelVersionParser.checkSnapshot(dependency.getArtifact().getVersion());
106 }
107
108 @Nonnull
109 @Override
110 public DependencyScope getScope() {
111 return session.requireDependencyScope(dependency.getScope());
112 }
113
114 @Nullable
115 @Override
116 public boolean isOptional() {
117 return dependency.isOptional();
118 }
119
120 @Nonnull
121 @Override
122 public DependencyCoordinate toCoordinate() {
123 return session.createDependencyCoordinate(this);
124 }
125
126 @Override
127 public boolean equals(Object o) {
128 return o instanceof Artifact && Objects.equals(key(), ((Artifact) o).key());
129 }
130
131 @Override
132 public int hashCode() {
133 return key.hashCode();
134 }
135
136 @Override
137 public String toString() {
138 return dependency.toString();
139 }
140 }