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  import org.apache.maven.repository.internal.DefaultModelVersionParser;
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      private final @Nonnull InternalSession session;
36      private final @Nonnull org.eclipse.aether.artifact.Artifact artifact;
37      private final String key;
38  
39      public DefaultArtifact(@Nonnull InternalSession session, @Nonnull org.eclipse.aether.artifact.Artifact artifact) {
40          this.session = nonNull(session, "session");
41          this.artifact = nonNull(artifact, "artifact");
42          this.key = getGroupId()
43                  + ':'
44                  + getArtifactId()
45                  + ':'
46                  + getExtension()
47                  + (getClassifier().isEmpty() ? "" : ":" + getClassifier())
48                  + ':'
49                  + getVersion();
50      }
51  
52      public org.eclipse.aether.artifact.Artifact getArtifact() {
53          return artifact;
54      }
55  
56      @Override
57      public String key() {
58          return key;
59      }
60  
61      @Nonnull
62      @Override
63      public String getGroupId() {
64          return artifact.getGroupId();
65      }
66  
67      @Nonnull
68      @Override
69      public String getArtifactId() {
70          return artifact.getArtifactId();
71      }
72  
73      @Nonnull
74      @Override
75      public Version getVersion() {
76          return session.parseVersion(artifact.getVersion());
77      }
78  
79      @Override
80      public Version getBaseVersion() {
81          return session.parseVersion(artifact.getBaseVersion());
82      }
83  
84      @Nonnull
85      @Override
86      public String getExtension() {
87          return artifact.getExtension();
88      }
89  
90      @Nonnull
91      @Override
92      public String getClassifier() {
93          return artifact.getClassifier();
94      }
95  
96      @Override
97      public boolean isSnapshot() {
98          return DefaultModelVersionParser.checkSnapshot(artifact.getVersion());
99      }
100 
101     @Nonnull
102     @Override
103     public ArtifactCoordinate toCoordinate() {
104         return session.createArtifactCoordinate(this);
105     }
106 
107     @Override
108     public boolean equals(Object o) {
109         return o instanceof Artifact && Objects.equals(key(), ((Artifact) o).key());
110     }
111 
112     @Override
113     public int hashCode() {
114         return key.hashCode();
115     }
116 
117     @Override
118     public String toString() {
119         return artifact.toString();
120     }
121 }