View Javadoc
1   package org.apache.maven.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Objects;
23  
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  import static org.apache.maven.internal.impl.Utils.nonNull;
35  
36  public class DefaultDependency implements Dependency
37  {
38      private final AbstractSession session;
39      private final org.eclipse.aether.graph.Dependency dependency;
40      private final String key;
41  
42      public DefaultDependency( @Nonnull AbstractSession session,
43                                @Nonnull org.eclipse.aether.graph.Dependency dependency )
44      {
45          this.session = nonNull( session, "session" );
46          this.dependency = nonNull( dependency, "dependency" );
47          this.key = getGroupId()
48                  + ':' + getArtifactId()
49                  + ':' + getExtension()
50                  + ( getClassifier().length() > 0 ? ":" + getClassifier() : "" )
51                  + ':' + getVersion();
52      }
53  
54      @Override
55      public String key()
56      {
57          return key;
58      }
59  
60      @Nonnull
61      public org.eclipse.aether.graph.Dependency getDependency()
62      {
63          return dependency;
64      }
65  
66      @Override
67      public String getGroupId()
68      {
69          return dependency.getArtifact().getGroupId();
70      }
71  
72      @Override
73      public String getArtifactId()
74      {
75          return dependency.getArtifact().getArtifactId();
76      }
77  
78      @Override
79      public String getClassifier()
80      {
81          return dependency.getArtifact().getClassifier();
82      }
83  
84      @Override
85      public Version getVersion()
86      {
87          return session.parseVersion( dependency.getArtifact().getVersion() );
88      }
89  
90      @Override
91      public String getExtension()
92      {
93          return dependency.getArtifact().getExtension();
94      }
95  
96      @Override
97      public Type getType()
98      {
99          String type = dependency.getArtifact().getProperty( ArtifactProperties.TYPE,
100                 dependency.getArtifact().getExtension() );
101         return session.getService( TypeRegistry.class ).getType( type );
102     }
103 
104     @Override
105     public boolean isSnapshot()
106     {
107         return DefaultVersionParser.checkSnapshot( dependency.getArtifact().getVersion() );
108     }
109 
110     @Nonnull
111     @Override
112     public Scope getScope()
113     {
114         return Scope.get( dependency.getScope() );
115     }
116 
117     @Nullable
118     @Override
119     public boolean isOptional()
120     {
121         return dependency.isOptional();
122     }
123 
124     @Nonnull
125     @Override
126     public DependencyCoordinate toCoordinate()
127     {
128         return session.createDependencyCoordinate( this );
129     }
130 
131     @Override
132     public boolean equals( Object o )
133     {
134         return o instanceof DefaultDependency
135                 && Objects.equals( key, ( (DefaultDependency) o ).key );
136     }
137 
138     @Override
139     public int hashCode()
140     {
141         return key.hashCode();
142     }
143 
144     @Override
145     public String toString()
146     {
147         return dependency.toString();
148     }
149 }