KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > versioning > DefaultArtifactVersion


1 package org.apache.maven.artifact.versioning;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.StringTokenizer JavaDoc;
20
21 /**
22  * Default implementation of artifact versioning.
23  *
24  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
25  * @version $Id: DefaultArtifactVersion.java 377583 2006-02-14 02:34:18Z brett $
26  */

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