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