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.index.artifact;
20  
21  import java.util.Objects;
22  
23  /**
24   * An immutable value class representing unique artifact coordinates.
25   *
26   * @author cstamas
27   * @author jvanzyl
28   */
29  public class Gav {
30      /**
31       * Enumeration representing Maven artifact hash types
32       */
33      public enum HashType {
34          sha1,
35          md5,
36          sha256,
37          sha512
38      }
39  
40      /**
41       * Enumeration representing Maven artifact signature types
42       */
43      public enum SignatureType {
44          gpg;
45  
46          @Override
47          public String toString() {
48              if (this == SignatureType.gpg) {
49                  return "asc";
50              }
51              return "unknown-signature-type";
52          }
53      }
54  
55      private final String groupId;
56  
57      private final String artifactId;
58  
59      private final String version;
60  
61      private final String baseVersion;
62  
63      private final String classifier;
64  
65      private final String extension;
66  
67      private final Integer snapshotBuildNumber;
68  
69      private final Long snapshotTimeStamp;
70  
71      private final String name;
72  
73      private final boolean snapshot;
74  
75      private final boolean hash;
76  
77      private final HashType hashType;
78  
79      private final boolean signature;
80  
81      private final SignatureType signatureType;
82  
83      public Gav(String groupId, String artifactId, String version) {
84          this(groupId, artifactId, version, null, null, null, null, null, false, null, false, null);
85      }
86  
87      public Gav(
88              String groupId,
89              String artifactId,
90              String version,
91              String classifier,
92              String extension,
93              Integer snapshotBuildNumber,
94              Long snapshotTimeStamp,
95              String name,
96              boolean hash,
97              HashType hashType,
98              boolean signature,
99              SignatureType signatureType) {
100         this.groupId = groupId;
101         this.artifactId = artifactId;
102         this.version = version;
103         this.snapshot = VersionUtils.isSnapshot(version);
104 
105         if (!snapshot) {
106             this.baseVersion = null;
107         } else {
108             if (version.contains("SNAPSHOT")) {
109                 // this is not a timestamped version
110                 this.baseVersion = null;
111             } else {
112                 // this is a timestamped version (verified against pattern, see above)
113                 // we have XXXXXX-YYYYMMDD.HHMMSS-B
114                 // but XXXXXX may contain "-" too!
115 
116                 // if ( new DefaultNexusEnforcer().isStrict() )
117                 // {
118                 // this.baseVersion = version.substring( 0, version.lastIndexOf( '-' ) );
119                 // this.baseVersion = baseVersion.substring( 0, baseVersion.lastIndexOf( '-' ) ) + "-SNAPSHOT";
120                 // }
121                 // also there may be no XXXXXX (i.e. when version is strictly named SNAPSHOT
122                 // BUT this is not the proper scheme, we will simply loosen up here if requested
123                 // else
124                 // {
125                 // trim the part of 'YYYYMMDD.HHMMSS-BN
126                 String tempBaseVersion = version.substring(0, version.lastIndexOf('-'));
127                 tempBaseVersion = tempBaseVersion.substring(0, tempBaseVersion.length() - 15);
128 
129                 if (tempBaseVersion.length() > 0) {
130                     this.baseVersion = tempBaseVersion + "SNAPSHOT";
131                 } else {
132                     this.baseVersion = "SNAPSHOT";
133                 }
134                 // }
135             }
136         }
137 
138         this.classifier = classifier;
139         this.extension = extension;
140         this.snapshotBuildNumber = snapshotBuildNumber;
141         this.snapshotTimeStamp = snapshotTimeStamp;
142         this.name = name;
143         this.hash = hash;
144         this.hashType = hashType;
145         this.signature = signature;
146         this.signatureType = signatureType;
147     }
148 
149     public String getGroupId() {
150         return groupId;
151     }
152 
153     public String getArtifactId() {
154         return artifactId;
155     }
156 
157     public String getVersion() {
158         return version;
159     }
160 
161     public String getBaseVersion() {
162         if (baseVersion == null) {
163             return getVersion();
164         } else {
165             return baseVersion;
166         }
167     }
168 
169     public String getClassifier() {
170         return classifier;
171     }
172 
173     public String getExtension() {
174         return extension;
175     }
176 
177     public String getName() {
178         return name;
179     }
180 
181     public boolean isSnapshot() {
182         return snapshot;
183     }
184 
185     public Integer getSnapshotBuildNumber() {
186         return snapshotBuildNumber;
187     }
188 
189     public Long getSnapshotTimeStamp() {
190         return snapshotTimeStamp;
191     }
192 
193     public boolean isHash() {
194         return hash;
195     }
196 
197     public HashType getHashType() {
198         return hashType;
199     }
200 
201     public boolean isSignature() {
202         return signature;
203     }
204 
205     public SignatureType getSignatureType() {
206         return signatureType;
207     }
208 
209     @Override
210     public boolean equals(Object o) {
211         if (this == o) {
212             return true;
213         }
214         if (o == null || getClass() != o.getClass()) {
215             return false;
216         }
217         Gav gav = (Gav) o;
218         return snapshot == gav.snapshot
219                 && hash == gav.hash
220                 && signature == gav.signature
221                 && Objects.equals(groupId, gav.groupId)
222                 && Objects.equals(artifactId, gav.artifactId)
223                 && Objects.equals(version, gav.version)
224                 && Objects.equals(baseVersion, gav.baseVersion)
225                 && Objects.equals(classifier, gav.classifier)
226                 && Objects.equals(extension, gav.extension)
227                 && Objects.equals(snapshotBuildNumber, gav.snapshotBuildNumber)
228                 && Objects.equals(snapshotTimeStamp, gav.snapshotTimeStamp)
229                 && Objects.equals(name, gav.name)
230                 && hashType == gav.hashType
231                 && signatureType == gav.signatureType;
232     }
233 
234     @Override
235     public int hashCode() {
236         return Objects.hash(
237                 groupId,
238                 artifactId,
239                 version,
240                 baseVersion,
241                 classifier,
242                 extension,
243                 snapshotBuildNumber,
244                 snapshotTimeStamp,
245                 name,
246                 snapshot,
247                 hash,
248                 hashType,
249                 signature,
250                 signatureType);
251     }
252 }