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