1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  package org.eclipse.aether.artifact;
12  
13  import java.io.File;
14  import java.util.Collections;
15  import java.util.HashMap;
16  import java.util.Map;
17  import java.util.regex.Matcher;
18  import java.util.regex.Pattern;
19  
20  
21  
22  
23  public abstract class AbstractArtifact
24      implements Artifact
25  {
26  
27      private static final String SNAPSHOT = "SNAPSHOT";
28  
29      private static final Pattern SNAPSHOT_TIMESTAMP = Pattern.compile( "^(.*-)?([0-9]{8}\\.[0-9]{6}-[0-9]+)$" );
30  
31      public boolean isSnapshot()
32      {
33          return isSnapshot( getVersion() );
34      }
35  
36      private static boolean isSnapshot( String version )
37      {
38          return version.endsWith( SNAPSHOT ) || SNAPSHOT_TIMESTAMP.matcher( version ).matches();
39      }
40  
41      public String getBaseVersion()
42      {
43          return toBaseVersion( getVersion() );
44      }
45  
46      private static String toBaseVersion( String version )
47      {
48          String baseVersion;
49  
50          if ( version == null )
51          {
52              baseVersion = version;
53          }
54          else if ( version.startsWith( "[" ) || version.startsWith( "(" ) )
55          {
56              baseVersion = version;
57          }
58          else
59          {
60              Matcher m = SNAPSHOT_TIMESTAMP.matcher( version );
61              if ( m.matches() )
62              {
63                  if ( m.group( 1 ) != null )
64                  {
65                      baseVersion = m.group( 1 ) + SNAPSHOT;
66                  }
67                  else
68                  {
69                      baseVersion = SNAPSHOT;
70                  }
71              }
72              else
73              {
74                  baseVersion = version;
75              }
76          }
77  
78          return baseVersion;
79      }
80  
81      
82  
83  
84  
85  
86  
87  
88  
89  
90      private Artifact newInstance( String version, Map<String, String> properties, File file )
91      {
92          return new DefaultArtifact( getGroupId(), getArtifactId(), getClassifier(), getExtension(), version, file,
93                                      properties );
94      }
95  
96      public Artifact setVersion( String version )
97      {
98          String current = getVersion();
99          if ( current.equals( version ) || ( version == null && current.length() <= 0 ) )
100         {
101             return this;
102         }
103         return newInstance( version, getProperties(), getFile() );
104     }
105 
106     public Artifact setFile( File file )
107     {
108         File current = getFile();
109         if ( ( current == null ) ? file == null : current.equals( file ) )
110         {
111             return this;
112         }
113         return newInstance( getVersion(), getProperties(), file );
114     }
115 
116     public Artifact setProperties( Map<String, String> properties )
117     {
118         Map<String, String> current = getProperties();
119         if ( current.equals( properties ) || ( properties == null && current.isEmpty() ) )
120         {
121             return this;
122         }
123         return newInstance( getVersion(), copyProperties( properties ), getFile() );
124     }
125 
126     public String getProperty( String key, String defaultValue )
127     {
128         String value = getProperties().get( key );
129         return ( value != null ) ? value : defaultValue;
130     }
131 
132     
133 
134 
135 
136 
137 
138 
139     protected static Map<String, String> copyProperties( Map<String, String> properties )
140     {
141         if ( properties != null && !properties.isEmpty() )
142         {
143             return Collections.unmodifiableMap( new HashMap<String, String>( properties ) );
144         }
145         else
146         {
147             return Collections.emptyMap();
148         }
149     }
150 
151     @Override
152     public String toString()
153     {
154         StringBuilder buffer = new StringBuilder( 128 );
155         buffer.append( getGroupId() );
156         buffer.append( ':' ).append( getArtifactId() );
157         buffer.append( ':' ).append( getExtension() );
158         if ( getClassifier().length() > 0 )
159         {
160             buffer.append( ':' ).append( getClassifier() );
161         }
162         buffer.append( ':' ).append( getVersion() );
163         return buffer.toString();
164     }
165 
166     
167 
168 
169 
170 
171 
172 
173     @Override
174     public boolean equals( Object obj )
175     {
176         if ( obj == this )
177         {
178             return true;
179         }
180         else if ( !( obj instanceof Artifact ) )
181         {
182             return false;
183         }
184 
185         Artifact that = (Artifact) obj;
186 
187         return getArtifactId().equals( that.getArtifactId() ) && getGroupId().equals( that.getGroupId() )
188             && getVersion().equals( that.getVersion() ) && getExtension().equals( that.getExtension() )
189             && getClassifier().equals( that.getClassifier() ) && eq( getFile(), that.getFile() )
190             && getProperties().equals( that.getProperties() );
191     }
192 
193     private static <T> boolean eq( T s1, T s2 )
194     {
195         return s1 != null ? s1.equals( s2 ) : s2 == null;
196     }
197 
198     
199 
200 
201 
202 
203     @Override
204     public int hashCode()
205     {
206         int hash = 17;
207         hash = hash * 31 + getGroupId().hashCode();
208         hash = hash * 31 + getArtifactId().hashCode();
209         hash = hash * 31 + getExtension().hashCode();
210         hash = hash * 31 + getClassifier().hashCode();
211         hash = hash * 31 + getVersion().hashCode();
212         hash = hash * 31 + hash( getFile() );
213         return hash;
214     }
215 
216     private static int hash( Object obj )
217     {
218         return ( obj != null ) ? obj.hashCode() : 0;
219     }
220 
221 }