KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > repository > Version


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

17 package org.apache.geronimo.kernel.repository;
18
19 import java.util.StringTokenizer JavaDoc;
20 import java.io.Serializable JavaDoc;
21
22 /**
23  * Default implementation of artifact versioning.
24  *
25  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
26  * @version $Id: Version.java 476049 2006-11-17 04:35:17Z kevan $
27  */

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