View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.eclipse.aether.artifact.ArtifactProperties;
34  
35  import static org.apache.maven.internal.impl.Utils.nonNull;
36  
37  public class DefaultDependency implements Dependency {
38      private final InternalSession session;
39      private final org.eclipse.aether.graph.Dependency dependency;
40      private final DependencyProperties dependencyProperties;
41      private final String key;
42  
43      public DefaultDependency(
44              @Nonnull InternalSession session, @Nonnull org.eclipse.aether.graph.Dependency dependency) {
45          this.session = nonNull(session, "session");
46          this.dependency = nonNull(dependency, "dependency");
47          this.dependencyProperties =
48                  new DefaultDependencyProperties(dependency.getArtifact().getProperties());
49          this.key = getGroupId()
50                  + ':'
51                  + getArtifactId()
52                  + ':'
53                  + getExtension()
54                  + (!getClassifier().isEmpty() ? ":" + getClassifier() : "")
55                  + ':'
56                  + getVersion();
57      }
58  
59      @Override
60      public String key() {
61          return key;
62      }
63  
64      @Nonnull
65      public org.eclipse.aether.graph.Dependency getDependency() {
66          return dependency;
67      }
68  
69      @Override
70      public String getGroupId() {
71          return dependency.getArtifact().getGroupId();
72      }
73  
74      @Override
75      public String getArtifactId() {
76          return dependency.getArtifact().getArtifactId();
77      }
78  
79      @Override
80      public String getClassifier() {
81          return dependency.getArtifact().getClassifier();
82      }
83  
84      @Override
85      public Version getVersion() {
86          return session.parseVersion(dependency.getArtifact().getVersion());
87      }
88  
89      @Override
90      public Version getBaseVersion() {
91          return session.parseVersion(dependency.getArtifact().getBaseVersion());
92      }
93  
94      @Override
95      public String getExtension() {
96          return dependency.getArtifact().getExtension();
97      }
98  
99      @Override
100     public Type getType() {
101         String type = dependency
102                 .getArtifact()
103                 .getProperty(ArtifactProperties.TYPE, dependency.getArtifact().getExtension());
104         return session.getService(TypeRegistry.class).getType(type);
105     }
106 
107     @Override
108     public DependencyProperties getDependencyProperties() {
109         return dependencyProperties;
110     }
111 
112     @Override
113     public boolean isSnapshot() {
114         return DefaultVersionParser.checkSnapshot(dependency.getArtifact().getVersion());
115     }
116 
117     @Nonnull
118     @Override
119     public Scope getScope() {
120         return Scope.get(dependency.getScope());
121     }
122 
123     @Nullable
124     @Override
125     public boolean isOptional() {
126         return dependency.isOptional();
127     }
128 
129     @Nonnull
130     @Override
131     public DependencyCoordinate toCoordinate() {
132         return session.createDependencyCoordinate(this);
133     }
134 
135     @Override
136     public boolean equals(Object o) {
137         return o instanceof Artifact && Objects.equals(key(), ((Artifact) o).key());
138     }
139 
140     @Override
141     public int hashCode() {
142         return key.hashCode();
143     }
144 
145     @Override
146     public String toString() {
147         return dependency.toString();
148     }
149 }