Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultArtifactVersion |
|
| 3.25;3.25 |
1 | package org.apache.maven.artifact.versioning; | |
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.StringTokenizer; | |
23 | ||
24 | /** | |
25 | * Default implementation of artifact versioning. | |
26 | * | |
27 | * @author <a href="mailto:brett@apache.org">Brett Porter</a> | |
28 | * @version $Id: DefaultArtifactVersion.java 688932 2008-08-26 01:24:27Z jdcasey $ | |
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 | 0 | { |
47 | 0 | parseVersion( version ); |
48 | 0 | } |
49 | ||
50 | public int compareTo( Object o ) | |
51 | { | |
52 | 0 | ArtifactVersion otherVersion = (ArtifactVersion) o; |
53 | ||
54 | 0 | int result = getMajorVersion() - otherVersion.getMajorVersion(); |
55 | 0 | if ( result == 0 ) |
56 | { | |
57 | 0 | result = getMinorVersion() - otherVersion.getMinorVersion(); |
58 | } | |
59 | 0 | if ( result == 0 ) |
60 | { | |
61 | 0 | result = getIncrementalVersion() - otherVersion.getIncrementalVersion(); |
62 | } | |
63 | 0 | if ( result == 0 ) |
64 | { | |
65 | 0 | if ( qualifier != null ) |
66 | { | |
67 | 0 | String otherQualifier = otherVersion.getQualifier(); |
68 | ||
69 | 0 | if ( otherQualifier != null ) |
70 | { | |
71 | 0 | if ( ( qualifier.length() > otherQualifier.length() ) |
72 | && qualifier.startsWith( otherQualifier ) ) | |
73 | { | |
74 | // here, the longer one that otherwise match is considered older | |
75 | 0 | result = -1; |
76 | } | |
77 | 0 | else if ( ( qualifier.length() < otherQualifier.length() ) |
78 | && otherQualifier.startsWith( qualifier ) ) | |
79 | { | |
80 | // here, the longer one that otherwise match is considered older | |
81 | 0 | result = 1; |
82 | } | |
83 | else | |
84 | { | |
85 | 0 | result = qualifier.compareTo( otherQualifier ); |
86 | } | |
87 | } | |
88 | else | |
89 | { | |
90 | // otherVersion has no qualifier but we do - that's newer | |
91 | 0 | result = -1; |
92 | } | |
93 | 0 | } |
94 | 0 | else if ( otherVersion.getQualifier() != null ) |
95 | { | |
96 | // otherVersion has a qualifier but we don't, we're newer | |
97 | 0 | result = 1; |
98 | } | |
99 | else | |
100 | { | |
101 | 0 | result = getBuildNumber() - otherVersion.getBuildNumber(); |
102 | } | |
103 | } | |
104 | 0 | return result; |
105 | } | |
106 | ||
107 | public int getMajorVersion() | |
108 | { | |
109 | 0 | return majorVersion != null ? majorVersion.intValue() : 0; |
110 | } | |
111 | ||
112 | public int getMinorVersion() | |
113 | { | |
114 | 0 | return minorVersion != null ? minorVersion.intValue() : 0; |
115 | } | |
116 | ||
117 | public int getIncrementalVersion() | |
118 | { | |
119 | 0 | return incrementalVersion != null ? incrementalVersion.intValue() : 0; |
120 | } | |
121 | ||
122 | public int getBuildNumber() | |
123 | { | |
124 | 0 | return buildNumber != null ? buildNumber.intValue() : 0; |
125 | } | |
126 | ||
127 | public String getQualifier() | |
128 | { | |
129 | 0 | return qualifier; |
130 | } | |
131 | ||
132 | public final void parseVersion( String version ) | |
133 | { | |
134 | 0 | unparsed = version; |
135 | ||
136 | 0 | int index = version.indexOf( "-" ); |
137 | ||
138 | String part1; | |
139 | 0 | String part2 = null; |
140 | ||
141 | 0 | if ( index < 0 ) |
142 | { | |
143 | 0 | part1 = version; |
144 | } | |
145 | else | |
146 | { | |
147 | 0 | part1 = version.substring( 0, index ); |
148 | 0 | part2 = version.substring( index + 1 ); |
149 | } | |
150 | ||
151 | 0 | if ( part2 != null ) |
152 | { | |
153 | try | |
154 | { | |
155 | 0 | if ( ( part2.length() == 1 ) || !part2.startsWith( "0" ) ) |
156 | { | |
157 | 0 | buildNumber = Integer.valueOf( part2 ); |
158 | } | |
159 | else | |
160 | { | |
161 | 0 | qualifier = part2; |
162 | } | |
163 | } | |
164 | 0 | catch ( NumberFormatException e ) |
165 | { | |
166 | 0 | qualifier = part2; |
167 | 0 | } |
168 | } | |
169 | ||
170 | 0 | if ( ( part1.indexOf( "." ) < 0 ) && !part1.startsWith( "0" ) ) |
171 | { | |
172 | try | |
173 | { | |
174 | 0 | majorVersion = Integer.valueOf( part1 ); |
175 | } | |
176 | 0 | catch ( NumberFormatException e ) |
177 | { | |
178 | // qualifier is the whole version, including "-" | |
179 | 0 | qualifier = version; |
180 | 0 | buildNumber = null; |
181 | 0 | } |
182 | } | |
183 | else | |
184 | { | |
185 | 0 | boolean fallback = false; |
186 | ||
187 | 0 | StringTokenizer tok = new StringTokenizer( part1, "." ); |
188 | try | |
189 | { | |
190 | 0 | majorVersion = getNextIntegerToken( tok ); |
191 | 0 | if ( tok.hasMoreTokens() ) |
192 | { | |
193 | 0 | minorVersion = getNextIntegerToken( tok ); |
194 | } | |
195 | 0 | if ( tok.hasMoreTokens() ) |
196 | { | |
197 | 0 | incrementalVersion = getNextIntegerToken( tok ); |
198 | } | |
199 | 0 | if ( tok.hasMoreTokens() ) |
200 | { | |
201 | 0 | fallback = true; |
202 | } | |
203 | ||
204 | // string tokenzier won't detect these and ignores them | |
205 | 0 | if ( part1.indexOf( ".." ) >= 0 || part1.startsWith( "." ) || part1.endsWith( "." ) ) |
206 | { | |
207 | 0 | fallback = true; |
208 | } | |
209 | } | |
210 | 0 | catch ( NumberFormatException e ) |
211 | { | |
212 | 0 | fallback = true; |
213 | 0 | } |
214 | ||
215 | 0 | if ( fallback ) |
216 | { | |
217 | // qualifier is the whole version, including "-" | |
218 | 0 | qualifier = version; |
219 | 0 | majorVersion = null; |
220 | 0 | minorVersion = null; |
221 | 0 | incrementalVersion = null; |
222 | 0 | buildNumber = null; |
223 | } | |
224 | } | |
225 | 0 | } |
226 | ||
227 | private static Integer getNextIntegerToken( StringTokenizer tok ) | |
228 | { | |
229 | 0 | String s = tok.nextToken(); |
230 | 0 | if ( ( s.length() > 1 ) && s.startsWith( "0" ) ) |
231 | { | |
232 | 0 | throw new NumberFormatException( "Number part has a leading 0: '" + s + "'" ); |
233 | } | |
234 | 0 | return Integer.valueOf( s ); |
235 | } | |
236 | ||
237 | public String toString() | |
238 | { | |
239 | 0 | return unparsed; |
240 | } | |
241 | ||
242 | public boolean equals( Object other ) | |
243 | { | |
244 | 0 | if ( this == other ) |
245 | { | |
246 | 0 | return true; |
247 | } | |
248 | ||
249 | 0 | if ( false == ( other instanceof ArtifactVersion ) ) |
250 | { | |
251 | 0 | return false; |
252 | } | |
253 | ||
254 | 0 | return 0 == compareTo( other ); |
255 | } | |
256 | ||
257 | public int hashCode() | |
258 | { | |
259 | 0 | int result = 1229; |
260 | ||
261 | 0 | result = 1223 * result + getMajorVersion(); |
262 | 0 | result = 1223 * result + getMinorVersion(); |
263 | 0 | result = 1223 * result + getIncrementalVersion(); |
264 | 0 | result = 1223 * result + getBuildNumber(); |
265 | ||
266 | 0 | if ( null != getQualifier() ) |
267 | { | |
268 | 0 | result = 1223 * result + getQualifier().hashCode(); |
269 | } | |
270 | ||
271 | 0 | return result; |
272 | } | |
273 | } |