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 InternalSession session;
35      private final @Nonnull org.eclipse.aether.artifact.Artifact artifact;
36      private final String key;
37  
38      public DefaultArtifact(@Nonnull InternalSession session, @Nonnull org.eclipse.aether.artifact.Artifact artifact) {
39          this.session = nonNull(session, "session");
40          this.artifact = nonNull(artifact, "artifact");
41          this.key = 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 key;
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      @Override
79      public Version getBaseVersion() {
80          return session.parseVersion(artifact.getBaseVersion());
81      }
82  
83      @Nonnull
84      @Override
85      public String getExtension() {
86          return artifact.getExtension();
87      }
88  
89      @Nonnull
90      @Override
91      public String getClassifier() {
92          return artifact.getClassifier();
93      }
94  
95      @Override
96      public boolean isSnapshot() {
97          return DefaultVersionParser.checkSnapshot(artifact.getVersion());
98      }
99  
100     @Nonnull
101     @Override
102     public ArtifactCoordinate toCoordinate() {
103         return session.createArtifactCoordinate(this);
104     }
105 
106     @Override
107     public boolean equals(Object o) {
108         return o instanceof Artifact && Objects.equals(key(), ((Artifact) o).key());
109     }
110 
111     @Override
112     public int hashCode() {
113         return key.hashCode();
114     }
115 
116     @Override
117     public String toString() {
118         return artifact.toString();
119     }
120 }