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.Dependency;
24 import org.apache.maven.api.DependencyCoordinate;
25 import org.apache.maven.api.Scope;
26 import org.apache.maven.api.Type;
27 import org.apache.maven.api.Version;
28 import org.apache.maven.api.annotations.Nonnull;
29 import org.apache.maven.api.annotations.Nullable;
30 import org.apache.maven.api.services.TypeRegistry;
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 private final AbstractSession session;
37 private final org.eclipse.aether.graph.Dependency dependency;
38 private final String key;
39
40 public DefaultDependency(
41 @Nonnull AbstractSession session, @Nonnull org.eclipse.aether.graph.Dependency dependency) {
42 this.session = nonNull(session, "session");
43 this.dependency = nonNull(dependency, "dependency");
44 this.key = getGroupId()
45 + ':'
46 + getArtifactId()
47 + ':'
48 + getExtension()
49 + (getClassifier().length() > 0 ? ":" + getClassifier() : "")
50 + ':'
51 + getVersion();
52 }
53
54 @Override
55 public String key() {
56 return key;
57 }
58
59 @Nonnull
60 public org.eclipse.aether.graph.Dependency getDependency() {
61 return dependency;
62 }
63
64 @Override
65 public String getGroupId() {
66 return dependency.getArtifact().getGroupId();
67 }
68
69 @Override
70 public String getArtifactId() {
71 return dependency.getArtifact().getArtifactId();
72 }
73
74 @Override
75 public String getClassifier() {
76 return dependency.getArtifact().getClassifier();
77 }
78
79 @Override
80 public Version getVersion() {
81 return session.parseVersion(dependency.getArtifact().getVersion());
82 }
83
84 @Override
85 public String getExtension() {
86 return dependency.getArtifact().getExtension();
87 }
88
89 @Override
90 public Type getType() {
91 String type = dependency
92 .getArtifact()
93 .getProperty(ArtifactProperties.TYPE, dependency.getArtifact().getExtension());
94 return session.getService(TypeRegistry.class).getType(type);
95 }
96
97 @Override
98 public boolean isSnapshot() {
99 return DefaultVersionParser.checkSnapshot(dependency.getArtifact().getVersion());
100 }
101
102 @Nonnull
103 @Override
104 public Scope getScope() {
105 return Scope.get(dependency.getScope());
106 }
107
108 @Nullable
109 @Override
110 public boolean isOptional() {
111 return dependency.isOptional();
112 }
113
114 @Nonnull
115 @Override
116 public DependencyCoordinate toCoordinate() {
117 return session.createDependencyCoordinate(this);
118 }
119
120 @Override
121 public boolean equals(Object o) {
122 return o instanceof DefaultDependency && Objects.equals(key, ((DefaultDependency) o).key);
123 }
124
125 @Override
126 public int hashCode() {
127 return key.hashCode();
128 }
129
130 @Override
131 public String toString() {
132 return dependency.toString();
133 }
134 }