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