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 String getExtension() {
91          return dependency.getArtifact().getExtension();
92      }
93  
94      @Override
95      public Type getType() {
96          String type = dependency
97                  .getArtifact()
98                  .getProperty(ArtifactProperties.TYPE, dependency.getArtifact().getExtension());
99          return session.getService(TypeRegistry.class).getType(type);
100     }
101 
102     @Override
103     public DependencyProperties getDependencyProperties() {
104         return dependencyProperties;
105     }
106 
107     @Override
108     public boolean isSnapshot() {
109         return DefaultVersionParser.checkSnapshot(dependency.getArtifact().getVersion());
110     }
111 
112     @Nonnull
113     @Override
114     public Scope getScope() {
115         return Scope.get(dependency.getScope());
116     }
117 
118     @Nullable
119     @Override
120     public boolean isOptional() {
121         return dependency.isOptional();
122     }
123 
124     @Nonnull
125     @Override
126     public DependencyCoordinate toCoordinate() {
127         return session.createDependencyCoordinate(this);
128     }
129 
130     @Override
131     public boolean equals(Object o) {
132         return o instanceof Artifact && Objects.equals(key(), ((Artifact) o).key());
133     }
134 
135     @Override
136     public int hashCode() {
137         return key.hashCode();
138     }
139 
140     @Override
141     public String toString() {
142         return dependency.toString();
143     }
144 }