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 ComparableVersion comparable;
44
45 public DefaultArtifactVersion( String version )
46 {
47 parseVersion( version );
48 }
49
50 @Override
51 public int hashCode()
52 {
53 return 11 + comparable.hashCode();
54 }
55
56 @Override
57 public boolean equals( Object other )
58 {
59 if ( this == other )
60 {
61 return true;
62 }
63
64 if ( !( other instanceof ArtifactVersion ) )
65 {
66 return false;
67 }
68
69 return compareTo( (ArtifactVersion) other ) == 0;
70 }
71
72 public int compareTo( ArtifactVersion otherVersion )
73 {
74 if ( otherVersion instanceof DefaultArtifactVersion )
75 {
76 return this.comparable.compareTo( ( (DefaultArtifactVersion) otherVersion).comparable );
77 }
78 else
79 {
80 return compareTo( new DefaultArtifactVersion( otherVersion.toString() ) );
81 }
82 }
83
84 public int getMajorVersion()
85 {
86 return majorVersion != null ? majorVersion : 0;
87 }
88
89 public int getMinorVersion()
90 {
91 return minorVersion != null ? minorVersion : 0;
92 }
93
94 public int getIncrementalVersion()
95 {
96 return incrementalVersion != null ? incrementalVersion : 0;
97 }
98
99 public int getBuildNumber()
100 {
101 return buildNumber != null ? buildNumber : 0;
102 }
103
104 public String getQualifier()
105 {
106 return qualifier;
107 }
108
109 public final void parseVersion( String version )
110 {
111 comparable = new ComparableVersion( version );
112
113 int index = version.indexOf( "-" );
114
115 String part1;
116 String part2 = null;
117
118 if ( index < 0 )
119 {
120 part1 = version;
121 }
122 else
123 {
124 part1 = version.substring( 0, index );
125 part2 = version.substring( index + 1 );
126 }
127
128 if ( part2 != null )
129 {
130 try
131 {
132 if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) )
133 {
134 buildNumber = Integer.valueOf( part2 );
135 }
136 else
137 {
138 qualifier = part2;
139 }
140 }
141 catch ( NumberFormatException e )
142 {
143 qualifier = part2;
144 }
145 }
146
147 if ( ( part1.indexOf( "." ) < 0 ) && !part1.startsWith( "0" ) )
148 {
149 try
150 {
151 majorVersion = Integer.valueOf( part1 );
152 }
153 catch ( NumberFormatException e )
154 {
155
156 qualifier = version;
157 buildNumber = null;
158 }
159 }
160 else
161 {
162 boolean fallback = false;
163
164 StringTokenizer tok = new StringTokenizer( part1, "." );
165 try
166 {
167 majorVersion = getNextIntegerToken( tok );
168 if ( tok.hasMoreTokens() )
169 {
170 minorVersion = getNextIntegerToken( tok );
171 }
172 if ( tok.hasMoreTokens() )
173 {
174 incrementalVersion = getNextIntegerToken( tok );
175 }
176 if ( tok.hasMoreTokens() )
177 {
178 fallback = true;
179 }
180
181
182 if ( part1.indexOf( ".." ) >= 0 || part1.startsWith( "." ) || part1.endsWith( "." ) )
183 {
184 fallback = true;
185 }
186 }
187 catch ( NumberFormatException e )
188 {
189 fallback = true;
190 }
191
192 if ( fallback )
193 {
194
195 qualifier = version;
196 majorVersion = null;
197 minorVersion = null;
198 incrementalVersion = null;
199 buildNumber = null;
200 }
201 }
202 }
203
204 private static Integer getNextIntegerToken( StringTokenizer tok )
205 {
206 String s = tok.nextToken();
207 if ( ( s.length() > 1 ) && s.startsWith( "0" ) )
208 {
209 throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" );
210 }
211 return Integer.valueOf( s );
212 }
213
214 @Override
215 public String toString()
216 {
217 StringBuilder buf = new StringBuilder();
218 if ( majorVersion != null )
219 {
220 buf.append( majorVersion );
221 }
222 if ( minorVersion != null )
223 {
224 buf.append( "." );
225 buf.append( minorVersion );
226 }
227 if ( incrementalVersion != null )
228 {
229 buf.append( "." );
230 buf.append( incrementalVersion );
231 }
232 if ( buildNumber != null )
233 {
234 buf.append( "-" );
235 buf.append( buildNumber );
236 }
237 else if ( qualifier != null )
238 {
239 if ( buf.length() > 0 )
240 {
241 buf.append( "-" );
242 }
243 buf.append( qualifier );
244 }
245 return buf.toString();
246 }
247 }