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