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.Collection;
23  
24  import org.apache.maven.api.DependencyCoordinate;
25  import org.apache.maven.api.Exclusion;
26  import org.apache.maven.api.Scope;
27  import org.apache.maven.api.Type;
28  import org.apache.maven.api.VersionRange;
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 DefaultDependencyCoordinate implements DependencyCoordinate
37  {
38      private final AbstractSession session;
39      private final org.eclipse.aether.graph.Dependency dependency;
40  
41      public DefaultDependencyCoordinate( @Nonnull AbstractSession session,
42                                          @Nonnull org.eclipse.aether.graph.Dependency dependency )
43      {
44          this.session = nonNull( session, "session" );
45          this.dependency = nonNull( dependency, "dependency" );
46      }
47  
48      @Nonnull
49      public org.eclipse.aether.graph.Dependency getDependency()
50      {
51          return dependency;
52      }
53  
54      @Override
55      public String getGroupId()
56      {
57          return dependency.getArtifact().getGroupId();
58      }
59  
60      @Override
61      public String getArtifactId()
62      {
63          return dependency.getArtifact().getArtifactId();
64      }
65  
66      @Override
67      public String getClassifier()
68      {
69          return dependency.getArtifact().getClassifier();
70      }
71  
72      @Override
73      public VersionRange getVersion()
74      {
75          return session.parseVersionRange( dependency.getArtifact().getVersion() );
76      }
77  
78      @Override
79      public String getExtension()
80      {
81          return dependency.getArtifact().getExtension();
82      }
83  
84      @Override
85      public Type getType()
86      {
87          String type = dependency.getArtifact().getProperty( ArtifactProperties.TYPE,
88                              dependency.getArtifact().getExtension() );
89          return session.getService( TypeRegistry.class ).getType( type );
90      }
91  
92      @Nonnull
93      @Override
94      public Scope getScope()
95      {
96          return Scope.get( dependency.getScope() );
97      }
98  
99      @Nullable
100     @Override
101     public Boolean getOptional()
102     {
103         return dependency.getOptional();
104     }
105 
106     @Nonnull
107     @Override
108     public Collection<Exclusion> getExclusions()
109     {
110         return new MappedCollection<>( dependency.getExclusions(), this::toExclusion );
111     }
112 
113     private Exclusion toExclusion( org.eclipse.aether.graph.Exclusion exclusion )
114     {
115         return new Exclusion()
116         {
117             @Nullable
118             @Override
119             public String getGroupId()
120             {
121                 return exclusion.getGroupId();
122             }
123 
124             @Nullable
125             @Override
126             public String getArtifactId()
127             {
128                 return exclusion.getArtifactId();
129             }
130         };
131     }
132 }