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