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