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.DependencyProperties;
27 import org.apache.maven.api.Scope;
28 import org.apache.maven.api.Type;
29 import org.apache.maven.api.Version;
30 import org.apache.maven.api.annotations.Nonnull;
31 import org.apache.maven.api.annotations.Nullable;
32 import org.apache.maven.api.services.TypeRegistry;
33 import org.apache.maven.repository.internal.DefaultModelVersionParser;
34 import org.eclipse.aether.artifact.ArtifactProperties;
35
36 import static org.apache.maven.internal.impl.Utils.nonNull;
37
38 public class DefaultDependency implements Dependency {
39 private final InternalSession session;
40 private final org.eclipse.aether.graph.Dependency dependency;
41 private final DependencyProperties dependencyProperties;
42 private final String key;
43
44 public DefaultDependency(
45 @Nonnull InternalSession session, @Nonnull org.eclipse.aether.graph.Dependency dependency) {
46 this.session = nonNull(session, "session");
47 this.dependency = nonNull(dependency, "dependency");
48 this.dependencyProperties =
49 new DefaultDependencyProperties(dependency.getArtifact().getProperties());
50 this.key = getGroupId()
51 + ':'
52 + getArtifactId()
53 + ':'
54 + getExtension()
55 + (!getClassifier().isEmpty() ? ":" + getClassifier() : "")
56 + ':'
57 + getVersion();
58 }
59
60 @Override
61 public String key() {
62 return key;
63 }
64
65 @Nonnull
66 public org.eclipse.aether.graph.Dependency getDependency() {
67 return dependency;
68 }
69
70 @Override
71 public String getGroupId() {
72 return dependency.getArtifact().getGroupId();
73 }
74
75 @Override
76 public String getArtifactId() {
77 return dependency.getArtifact().getArtifactId();
78 }
79
80 @Override
81 public String getClassifier() {
82 return dependency.getArtifact().getClassifier();
83 }
84
85 @Override
86 public Version getVersion() {
87 return session.parseVersion(dependency.getArtifact().getVersion());
88 }
89
90 @Override
91 public Version getBaseVersion() {
92 return session.parseVersion(dependency.getArtifact().getBaseVersion());
93 }
94
95 @Override
96 public String getExtension() {
97 return dependency.getArtifact().getExtension();
98 }
99
100 @Override
101 public Type getType() {
102 String type = dependency
103 .getArtifact()
104 .getProperty(ArtifactProperties.TYPE, dependency.getArtifact().getExtension());
105 return session.getService(TypeRegistry.class).getType(type);
106 }
107
108 @Override
109 public DependencyProperties getDependencyProperties() {
110 return dependencyProperties;
111 }
112
113 @Override
114 public boolean isSnapshot() {
115 return DefaultModelVersionParser.checkSnapshot(dependency.getArtifact().getVersion());
116 }
117
118 @Nonnull
119 @Override
120 public Scope getScope() {
121 return Scope.get(dependency.getScope());
122 }
123
124 @Nullable
125 @Override
126 public boolean isOptional() {
127 return dependency.isOptional();
128 }
129
130 @Nonnull
131 @Override
132 public DependencyCoordinate toCoordinate() {
133 return session.createDependencyCoordinate(this);
134 }
135
136 @Override
137 public boolean equals(Object o) {
138 return o instanceof Artifact && Objects.equals(key(), ((Artifact) o).key());
139 }
140
141 @Override
142 public int hashCode() {
143 return key.hashCode();
144 }
145
146 @Override
147 public String toString() {
148 return dependency.toString();
149 }
150 }