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.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 }