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.Artifact;
25  import org.apache.maven.api.ArtifactCoordinate;
26  import org.apache.maven.api.Version;
27  import org.apache.maven.api.annotations.Nonnull;
28  
29  import static org.apache.maven.internal.impl.Utils.nonNull;
30  
31  /**
32   * A wrapper class around a maven resolver artifact.
33   */
34  public class DefaultArtifact implements Artifact
35  {
36      private final @Nonnull AbstractSession session;
37      private final @Nonnull org.eclipse.aether.artifact.Artifact artifact;
38      private final String id;
39  
40      public DefaultArtifact( @Nonnull AbstractSession session, @Nonnull org.eclipse.aether.artifact.Artifact artifact )
41      {
42          this.session = nonNull( session, "session can not be null" );
43          this.artifact = nonNull( artifact, "artifact can not be null" );
44          this.id = getGroupId()
45                  + ':' + getArtifactId()
46                  + ':' + getExtension()
47                  + ( getClassifier().length() > 0 ? ":" + getClassifier() : "" )
48                  + ':' + getVersion();
49      }
50  
51      public org.eclipse.aether.artifact.Artifact getArtifact()
52      {
53          return artifact;
54      }
55  
56      @Override
57      public String key()
58      {
59          return id;
60      }
61  
62      @Nonnull
63      @Override
64      public String getGroupId()
65      {
66          return artifact.getGroupId();
67      }
68  
69      @Nonnull
70      @Override
71      public String getArtifactId()
72      {
73          return artifact.getArtifactId();
74      }
75  
76      @Nonnull
77      @Override
78      public Version getVersion()
79      {
80          return session.parseVersion( artifact.getVersion() );
81      }
82  
83      @Nonnull
84      @Override
85      public String getExtension()
86      {
87          return artifact.getExtension();
88      }
89  
90      @Nonnull
91      @Override
92      public String getClassifier()
93      {
94          return artifact.getClassifier();
95      }
96  
97      @Override
98      public boolean isSnapshot()
99      {
100         return DefaultVersionParser.checkSnapshot( artifact.getVersion() );
101     }
102 
103     @Nonnull
104     @Override
105     public ArtifactCoordinate toCoordinate()
106     {
107         return session.createArtifactCoordinate( this );
108     }
109 
110     @Override
111     public boolean equals( Object o )
112     {
113         return o instanceof DefaultArtifact
114                 && Objects.equals( id, ( (DefaultArtifact) o ).id );
115     }
116 
117     @Override
118     public int hashCode()
119     {
120         return id.hashCode();
121     }
122 
123     @Override
124     public String toString()
125     {
126         return artifact.toString();
127     }
128 }