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