View Javadoc

1   package org.apache.maven.index.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * An immutable value class representing unique artifact coordinates.
24   * 
25   * @author cstamas
26   * @author jvanzyl
27   */
28  public class Gav
29  {
30      /**
31       * Enumeration representing Maven artifact hash types
32       */
33      public enum HashType
34      {
35          sha1, md5
36      }
37  
38      /**
39       * Enumeration representing Maven artifact signature types
40       */
41      public enum SignatureType
42      {
43          gpg;
44  
45          @Override
46          public String toString()
47          {
48              switch ( this )
49              {
50                  case gpg:
51                  {
52                      return "asc";
53                  }
54  
55                  default:
56                  {
57                      return "unknown-signature-type";
58                  }
59              }
60          }
61      }
62  
63      private final String groupId;
64  
65      private final String artifactId;
66  
67      private final String version;
68  
69      private final String baseVersion;
70  
71      private final String classifier;
72  
73      private final String extension;
74  
75      private final Integer snapshotBuildNumber;
76  
77      private final Long snapshotTimeStamp;
78  
79      private final String name;
80  
81      private final boolean snapshot;
82  
83      private final boolean hash;
84  
85      private final HashType hashType;
86  
87      private final boolean signature;
88  
89      private final SignatureType signatureType;
90  
91      public Gav( String groupId, String artifactId, String version )
92      {
93          this( groupId, artifactId, version, null, null, null, null, null, false, null, false, null );
94      }
95  
96      /**
97       * Deprecated constructor, left here for backward compatibility. It simply delegates to other constructor and
98       * neglects the snapshot redundant parameter.
99       * 
100      * @deprecated The <code>boolean snapshot</code> parameter is simply neglected. Use the constructor without it.
101      * @param groupId
102      * @param artifactId
103      * @param version
104      * @param classifier
105      * @param extension
106      * @param snapshotBuildNumber
107      * @param snapshotTimeStamp
108      * @param name
109      * @param snapshot
110      * @param hash
111      * @param hashType
112      * @param signature
113      * @param signatureType
114      */
115     public Gav( String groupId, String artifactId, String version, String classifier, String extension,
116                 Integer snapshotBuildNumber, Long snapshotTimeStamp, String name, boolean snapshot, boolean hash,
117                 HashType hashType, boolean signature, SignatureType signatureType )
118     {
119         this( groupId, artifactId, version, classifier, extension, snapshotBuildNumber, snapshotTimeStamp, name, hash,
120             hashType, signature, signatureType );
121     }
122 
123     public Gav( String groupId, String artifactId, String version, String classifier, String extension,
124                 Integer snapshotBuildNumber, Long snapshotTimeStamp, String name, boolean hash, HashType hashType,
125                 boolean signature, SignatureType signatureType )
126     {
127         this.groupId = groupId;
128         this.artifactId = artifactId;
129         this.version = version;
130         this.snapshot = VersionUtils.isSnapshot( version );
131 
132         if ( !snapshot )
133         {
134             this.baseVersion = null;
135         }
136         else
137         {
138             if ( version.contains( "SNAPSHOT" ) )
139             {
140                 // this is not a timestamped version
141                 this.baseVersion = null;
142             }
143             else
144             {
145                 // this is a timestamped version (verified against pattern, see above)
146                 // we have XXXXXX-YYYYMMDD.HHMMSS-B
147                 // but XXXXXX may contain "-" too!
148 
149                 // if ( new DefaultNexusEnforcer().isStrict() )
150                 // {
151                 // this.baseVersion = version.substring( 0, version.lastIndexOf( '-' ) );
152                 // this.baseVersion = baseVersion.substring( 0, baseVersion.lastIndexOf( '-' ) ) + "-SNAPSHOT";
153                 // }
154                 // also there may be no XXXXXX (i.e. when version is strictly named SNAPSHOT
155                 // BUT this is not the proper scheme, we will simply loosen up here if requested
156                 // else
157                 // {
158                 // trim the part of 'YYYYMMDD.HHMMSS-BN
159                 String tempBaseVersion = version.substring( 0, version.lastIndexOf( '-' ) );
160                 tempBaseVersion = tempBaseVersion.substring( 0, tempBaseVersion.length() - 15 );
161 
162                 if ( tempBaseVersion.length() > 0 )
163                 {
164                     this.baseVersion = tempBaseVersion + "SNAPSHOT";
165                 }
166                 else
167                 {
168                     this.baseVersion = "SNAPSHOT";
169                 }
170                 // }
171             }
172         }
173 
174         this.classifier = classifier;
175         this.extension = extension;
176         this.snapshotBuildNumber = snapshotBuildNumber;
177         this.snapshotTimeStamp = snapshotTimeStamp;
178         this.name = name;
179         this.hash = hash;
180         this.hashType = hashType;
181         this.signature = signature;
182         this.signatureType = signatureType;
183     }
184 
185     public String getGroupId()
186     {
187         return groupId;
188     }
189 
190     public String getArtifactId()
191     {
192         return artifactId;
193     }
194 
195     public String getVersion()
196     {
197         return version;
198     }
199 
200     public String getBaseVersion()
201     {
202         if ( baseVersion == null )
203         {
204             return getVersion();
205         }
206         else
207         {
208             return baseVersion;
209         }
210     }
211 
212     public String getClassifier()
213     {
214         return classifier;
215     }
216 
217     public String getExtension()
218     {
219         return extension;
220     }
221 
222     public String getName()
223     {
224         return name;
225     }
226 
227     public boolean isSnapshot()
228     {
229         return snapshot;
230     }
231 
232     public Integer getSnapshotBuildNumber()
233     {
234         return snapshotBuildNumber;
235     }
236 
237     public Long getSnapshotTimeStamp()
238     {
239         return snapshotTimeStamp;
240     }
241 
242     public boolean isHash()
243     {
244         return hash;
245     }
246 
247     public HashType getHashType()
248     {
249         return hashType;
250     }
251 
252     public boolean isSignature()
253     {
254         return signature;
255     }
256 
257     public SignatureType getSignatureType()
258     {
259         return signatureType;
260     }
261 
262     @Override
263     public int hashCode()
264     {
265         int result = 1;
266         result = 31 * result + ( groupId == null ? 0 : groupId.hashCode() );
267         result = 31 * result + ( artifactId == null ? 0 : artifactId.hashCode() );
268         result = 31 * result + ( version == null ? 0 : version.hashCode() );
269         result = 31 * result + ( baseVersion == null ? 0 : baseVersion.hashCode() );
270         result = 31 * result + ( classifier == null ? 0 : classifier.hashCode() );
271         result = 31 * result + ( extension == null ? 0 : extension.hashCode() );
272         result = 31 * result + ( name == null ? 0 : name.hashCode() );
273         result = 31 * result + ( snapshot ? 1231 : 1237 );
274         result = 31 * result + ( snapshotBuildNumber == null ? 0 : snapshotBuildNumber.hashCode() );
275         result = 31 * result + ( snapshotTimeStamp == null ? 0 : snapshotTimeStamp.hashCode() );
276         result = 31 * result + ( hash ? 1231 : 1237 );
277         result = 31 * result + ( hashType == null ? 0 : hashType.hashCode() );
278         result = 31 * result + ( signature ? 1231 : 1237 );
279         result = 31 * result + ( signatureType == null ? 0 : signatureType.hashCode() );
280         return result;
281     }
282 
283     @Override
284     public boolean equals( Object obj )
285     {
286         if ( this == obj )
287         {
288             return true;
289         }
290         if ( obj == null )
291         {
292             return false;
293         }
294         if ( getClass() != obj.getClass() )
295         {
296             return false;
297         }
298 
299         Gav other = (Gav) obj;
300 
301         if ( groupId == null )
302         {
303             if ( other.groupId != null )
304             {
305                 return false;
306             }
307         }
308         else if ( !groupId.equals( other.groupId ) )
309         {
310             return false;
311         }
312 
313         if ( artifactId == null )
314         {
315             if ( other.artifactId != null )
316             {
317                 return false;
318             }
319         }
320         else if ( !artifactId.equals( other.artifactId ) )
321         {
322             return false;
323         }
324 
325         if ( version == null )
326         {
327             if ( other.version != null )
328             {
329                 return false;
330             }
331         }
332         else if ( !version.equals( other.version ) )
333         {
334             return false;
335         }
336 
337         if ( baseVersion == null )
338         {
339             if ( other.baseVersion != null )
340             {
341                 return false;
342             }
343         }
344         else if ( !baseVersion.equals( other.baseVersion ) )
345         {
346             return false;
347         }
348 
349         if ( classifier == null )
350         {
351             if ( other.classifier != null )
352             {
353                 return false;
354             }
355         }
356         else if ( !classifier.equals( other.classifier ) )
357         {
358             return false;
359         }
360 
361         if ( extension == null )
362         {
363             if ( other.extension != null )
364             {
365                 return false;
366             }
367         }
368         else if ( !extension.equals( other.extension ) )
369         {
370             return false;
371         }
372 
373         if ( name == null )
374         {
375             if ( other.name != null )
376             {
377                 return false;
378             }
379         }
380         else if ( !name.equals( other.name ) )
381         {
382             return false;
383         }
384 
385         if ( snapshot != other.snapshot )
386         {
387             return false;
388         }
389 
390         if ( snapshotBuildNumber == null )
391         {
392             if ( other.snapshotBuildNumber != null )
393             {
394                 return false;
395             }
396         }
397         else if ( !snapshotBuildNumber.equals( other.snapshotBuildNumber ) )
398         {
399             return false;
400         }
401 
402         if ( snapshotTimeStamp == null )
403         {
404             if ( other.snapshotTimeStamp != null )
405             {
406                 return false;
407             }
408         }
409         else if ( !snapshotTimeStamp.equals( other.snapshotTimeStamp ) )
410         {
411             return false;
412         }
413 
414         if ( hash != other.hash )
415         {
416             return false;
417         }
418 
419         if ( hashType == null )
420         {
421             if ( other.hashType != null )
422             {
423                 return false;
424             }
425         }
426         else if ( !hashType.equals( other.hashType ) )
427         {
428             return false;
429         }
430 
431         if ( signature != other.signature )
432         {
433             return false;
434         }
435 
436         if ( signatureType == null )
437         {
438             if ( other.signatureType != null )
439             {
440                 return false;
441             }
442         }
443         else if ( !signatureType.equals( other.signatureType ) )
444         {
445             return false;
446         }
447 
448         return true;
449     }
450 }