1 package org.apache.maven.artifact.versioning;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.StringTokenizer;
23
24
25
26
27
28
29
30 public class DefaultArtifactVersion
31 implements ArtifactVersion
32 {
33 private Integer majorVersion;
34
35 private Integer minorVersion;
36
37 private Integer incrementalVersion;
38
39 private Integer buildNumber;
40
41 private String qualifier;
42
43 private String unparsed;
44
45 public DefaultArtifactVersion( String version )
46 {
47 parseVersion( version );
48 }
49
50 public int compareTo( Object o )
51 {
52 ArtifactVersion otherVersion = (ArtifactVersion) o;
53
54 int result = getMajorVersion() - otherVersion.getMajorVersion();
55 if ( result == 0 )
56 {
57 result = getMinorVersion() - otherVersion.getMinorVersion();
58 }
59 if ( result == 0 )
60 {
61 result = getIncrementalVersion() - otherVersion.getIncrementalVersion();
62 }
63 if ( result == 0 )
64 {
65 if ( qualifier != null )
66 {
67 String otherQualifier = otherVersion.getQualifier();
68
69 if ( otherQualifier != null )
70 {
71 if ( ( qualifier.length() > otherQualifier.length() )
72 && qualifier.startsWith( otherQualifier ) )
73 {
74
75 result = -1;
76 }
77 else if ( ( qualifier.length() < otherQualifier.length() )
78 && otherQualifier.startsWith( qualifier ) )
79 {
80
81 result = 1;
82 }
83 else
84 {
85 result = qualifier.compareTo( otherQualifier );
86 }
87 }
88 else
89 {
90
91 result = -1;
92 }
93 }
94 else if ( otherVersion.getQualifier() != null )
95 {
96
97 result = 1;
98 }
99 else
100 {
101 result = getBuildNumber() - otherVersion.getBuildNumber();
102 }
103 }
104 return result;
105 }
106
107 public int getMajorVersion()
108 {
109 return majorVersion != null ? majorVersion.intValue() : 0;
110 }
111
112 public int getMinorVersion()
113 {
114 return minorVersion != null ? minorVersion.intValue() : 0;
115 }
116
117 public int getIncrementalVersion()
118 {
119 return incrementalVersion != null ? incrementalVersion.intValue() : 0;
120 }
121
122 public int getBuildNumber()
123 {
124 return buildNumber != null ? buildNumber.intValue() : 0;
125 }
126
127 public String getQualifier()
128 {
129 return qualifier;
130 }
131
132 public final void parseVersion( String version )
133 {
134 unparsed = version;
135
136 int index = version.indexOf( "-" );
137
138 String part1;
139 String part2 = null;
140
141 if ( index < 0 )
142 {
143 part1 = version;
144 }
145 else
146 {
147 part1 = version.substring( 0, index );
148 part2 = version.substring( index + 1 );
149 }
150
151 if ( part2 != null )
152 {
153 try
154 {
155 if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) )
156 {
157 buildNumber = Integer.valueOf( part2 );
158 }
159 else
160 {
161 qualifier = part2;
162 }
163 }
164 catch ( NumberFormatException e )
165 {
166 qualifier = part2;
167 }
168 }
169
170 if ( ( part1.indexOf( "." ) < 0 ) && !part1.startsWith( "0" ) )
171 {
172 try
173 {
174 majorVersion = Integer.valueOf( part1 );
175 }
176 catch ( NumberFormatException e )
177 {
178
179 qualifier = version;
180 buildNumber = null;
181 }
182 }
183 else
184 {
185 boolean fallback = false;
186
187 StringTokenizer tok = new StringTokenizer( part1, "." );
188 try
189 {
190 majorVersion = getNextIntegerToken( tok );
191 if ( tok.hasMoreTokens() )
192 {
193 minorVersion = getNextIntegerToken( tok );
194 }
195 if ( tok.hasMoreTokens() )
196 {
197 incrementalVersion = getNextIntegerToken( tok );
198 }
199 if ( tok.hasMoreTokens() )
200 {
201 fallback = true;
202 }
203
204
205 if ( part1.indexOf( ".." ) >= 0 || part1.startsWith( "." ) || part1.endsWith( "." ) )
206 {
207 fallback = true;
208 }
209 }
210 catch ( NumberFormatException e )
211 {
212 fallback = true;
213 }
214
215 if ( fallback )
216 {
217
218 qualifier = version;
219 majorVersion = null;
220 minorVersion = null;
221 incrementalVersion = null;
222 buildNumber = null;
223 }
224 }
225 }
226
227 private static Integer getNextIntegerToken( StringTokenizer tok )
228 {
229 String s = tok.nextToken();
230 if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
231 {
232 throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
233 }
234 return Integer.valueOf( s );
235 }
236
237 public String toString()
238 {
239 return unparsed;
240 }
241
242 public boolean equals( Object other )
243 {
244 if ( this == other )
245 {
246 return true;
247 }
248
249 if ( false == ( other instanceof ArtifactVersion ) )
250 {
251 return false;
252 }
253
254 return 0 == compareTo( other );
255 }
256
257 public int hashCode()
258 {
259 int result = 1229;
260
261 result = 1223 * result + getMajorVersion();
262 result = 1223 * result + getMinorVersion();
263 result = 1223 * result + getIncrementalVersion();
264 result = 1223 * result + getBuildNumber();
265
266 if ( null != getQualifier() )
267 {
268 result = 1223 * result + getQualifier().hashCode();
269 }
270
271 return result;
272 }
273 }