KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > utils > Version


1 package org.jahia.utils;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.StringTokenizer JavaDoc;
5
6 /**
7  * This class represents a product version, and can be initialized by a String.
8  * This is a utility object to compare versions easily.
9  *
10  * Currently it recognized Strings of the form :
11  *
12  * major.minor.build.other1.other2.*Bbetanumber
13  * major.minor.build.other1.other2.*RCreleasecandidatenumber
14  *
15  * "B" and "RC" can be uppercase or minor case, the comparison is case insensitive
16  * for the moment.
17  *
18  * <p>Copyright: Copyright (c) 2002</p>
19  * <p>Company: Jahia Inc.</p>
20  * @author Serge Huber
21  * @version 3.0
22  */

23
24 public class Version implements Comparable JavaDoc {
25
26     private static org.apache.log4j.Logger logger =
27         org.apache.log4j.Logger.getLogger(Version.class);
28
29     private ArrayList JavaDoc orderedVersionNumbers = new ArrayList JavaDoc();
30     private int betaNumber = -1;
31     private int releaseCandidateNumber = -1;
32
33     /**
34      * Constructor. See class definition for syntax of the version string
35      * @param versionString the String containing the version to analyze. See
36      * class description for more details.
37      * @throws NumberFormatException if there was a problem parsing the string
38      * containing the version.
39      */

40     public Version(String JavaDoc versionString)
41     throws NumberFormatException JavaDoc {
42         String JavaDoc workString = versionString.toLowerCase();
43         int betaPos = workString.indexOf("b");
44         int rcPos = workString.indexOf("rc");
45
46         String JavaDoc betaString = null;
47         String JavaDoc rcString = null;
48         if (betaPos != -1) {
49             betaString = workString.substring(betaPos + 1).trim();
50             workString = workString.substring(0, betaPos);
51             betaNumber = Integer.parseInt(betaString);
52         } else if (rcPos != -1) {
53             rcString = workString.substring(rcPos + 2).trim();
54             workString = workString.substring(0, rcPos);
55             releaseCandidateNumber = Integer.parseInt(rcString);
56         }
57
58         int underscorePos = workString.indexOf("_");
59         if (underscorePos != -1) {
60             workString = workString.substring(0, underscorePos).trim();
61         }
62
63         int hyphenPos = workString.indexOf("-");
64         if (hyphenPos != -1) {
65             workString = workString.substring(0, hyphenPos).trim();
66         }
67
68         StringTokenizer JavaDoc versionTokenizer = new StringTokenizer JavaDoc(workString, ".");
69         while (versionTokenizer.hasMoreTokens()) {
70             String JavaDoc curToken = versionTokenizer.nextToken().trim();
71             int curVersionNumber = Integer.parseInt(curToken);
72             orderedVersionNumbers.add(new Integer JavaDoc(curVersionNumber));
73         }
74         // JahiaConsole.println("Version.constructor",
75
// "Version=" + this.toString());
76
}
77
78     /**
79      * Returns true if the version represents a beta version
80      * @return true if a beta version.
81      */

82     public boolean isBeta() {
83         if (betaNumber != -1) {
84             return true;
85         } else {
86             return false;
87         }
88     }
89
90     /**
91      * Returns true if the version represents a release candidate version.
92      * @return true is release candidate
93      */

94     public boolean isReleaseCandidate() {
95         if (releaseCandidateNumber != -1) {
96             return true;
97         } else {
98             return false;
99         }
100     }
101
102     /**
103      * Returns true if the version represents final (that is to say non-beta
104      * and non-release candidate) version.
105      * @return true if version is neither a beta or a release candidate version.
106      */

107     public boolean isFinal() {
108         if ( (betaNumber == -1) &&
109              (releaseCandidateNumber == -1) ) {
110             return true;
111         } else {
112             return false;
113         }
114     }
115
116     /**
117      * Generates a String from the internal data structure. This is not
118      * necessarily equals to the String passed to the constructor, especially
119      * since here we return a lower case string.
120      * @return a lower case String representing the version
121      */

122     public String JavaDoc toString() {
123         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
124         for (int i=0; i < orderedVersionNumbers.size(); i++) {
125             Integer JavaDoc curVersionNumber = (Integer JavaDoc) orderedVersionNumbers.get(i);
126             result.append(curVersionNumber.intValue());
127             if (i < (orderedVersionNumbers.size() -1) ) {
128                 result.append(".");
129             }
130         }
131         if (betaNumber != -1) {
132             result.append("b");
133             result.append(betaNumber);
134         } else if (releaseCandidateNumber != -1) {
135             result.append("rc");
136             result.append(releaseCandidateNumber);
137         }
138         return result.toString();
139     }
140
141     /**
142      * Implements the compareTo method from the Comparable interface. This
143      * allows this class to be sorted by version number.
144      *
145      * Comparison is done the following way :
146      * 1. compares the version number until there is no more to compare
147      * 2. compares the "state" (beta, release candidate, final)
148      *
149      * Examples :
150      * 4.0, 4.0.1 returns -1
151      * 4.0B1, 4.0.1B1 returns -1
152      * 4.1.0, 4.0.1 returns 1
153      * 4.0.0, 4.0.0 return 0
154      * 4.0.1B1, 4.0.1RC2 returns -1
155      * ...
156      *
157      * @param o a Version object to compare to. If this is not a Version class
158      * object, then a ClassCastException will be raised
159      * @return -1 if this version is "smaller" than the one specified. 0 if
160      * it is equal, or 1 if it bigger.
161      * @throws ClassCastException if the passed parameter (o) is not a Version
162      * class object.
163      */

164     public int compareTo(Object JavaDoc o)
165     throws ClassCastException JavaDoc {
166         Version rightVersion = (Version) o;
167         ArrayList JavaDoc rightOrderedVersionNumbers = rightVersion.getOrderedVersionNumbers();
168
169         if (this.equals(rightVersion)) {
170             return 0;
171         }
172
173         if (orderedVersionNumbers.size() == rightOrderedVersionNumbers.size()) {
174             for (int i = 0; i < orderedVersionNumbers.size(); i++) {
175                 Integer JavaDoc versionNumber = (Integer JavaDoc) orderedVersionNumbers.get(i);
176                 Integer JavaDoc rightVersionNumber = (Integer JavaDoc) rightOrderedVersionNumbers.get(i);
177                 if (versionNumber.intValue() != rightVersionNumber.intValue()) {
178                     return versionNumber.compareTo(rightVersionNumber);
179                 }
180             }
181             // now we must compare beta numbers, release candidate number and regular versions
182
// to determine which is higher.
183
if (isBeta() && rightVersion.isBeta()) {
184                 if (betaNumber < rightVersion.getBetaNumber()) {
185                     return -1;
186                 } else {
187                     return 1;
188                 }
189             }
190
191             if (isReleaseCandidate() && rightVersion.isReleaseCandidate()) {
192                 if (releaseCandidateNumber < rightVersion.getReleaseCandidateNumber()) {
193                     return -1;
194                 } else {
195                     return 1;
196                 }
197             }
198
199             if (isBeta() && rightVersion.isReleaseCandidate()) { return -1; }
200             if (isBeta() && rightVersion.isFinal()) { return -1; }
201             if (isReleaseCandidate() && rightVersion.isBeta()) { return 1; }
202             if (isReleaseCandidate() && rightVersion.isFinal()) { return -1; }
203             if (isFinal() && rightVersion.isBeta()) { return 1; }
204             if (isFinal() && rightVersion.isReleaseCandidate()) { return 1; }
205
206             logger.debug("Unable to compare two versions " +
207                                  this.toString() + " and " +
208                                  rightVersion.toString() +
209                                  " returning equality...");
210             return 0;
211
212         } else if (orderedVersionNumbers.size() < rightOrderedVersionNumbers.size()) {
213             // this version has less numbers that the right one.
214
for (int i = 0; i < orderedVersionNumbers.size(); i++) {
215                 Integer JavaDoc versionNumber = (Integer JavaDoc) orderedVersionNumbers.get(i);
216                 Integer JavaDoc rightVersionNumber = (Integer JavaDoc) rightOrderedVersionNumbers.get(i);
217                 if (versionNumber.intValue() != rightVersionNumber.intValue()) {
218                     return versionNumber.compareTo(rightVersionNumber);
219                 }
220             }
221             return -1;
222         } else {
223             // the right version has less number than this one.
224
for (int i = 0; i < rightOrderedVersionNumbers.size() ; i++) {
225                 Integer JavaDoc versionNumber = (Integer JavaDoc) orderedVersionNumbers.get(i);
226                 Integer JavaDoc rightVersionNumber = (Integer JavaDoc) rightOrderedVersionNumbers.get(i);
227                 if (versionNumber.intValue() != rightVersionNumber.intValue()) {
228                     return versionNumber.compareTo(rightVersionNumber);
229                 }
230             }
231             return 1;
232         }
233     }
234
235     /**
236      * Returns an array list of Integer objects containing the version number.
237      * index 0 is the major version number, index 1 is the minor, etc... This
238      * method does not return beta or release candidate versions.
239      * @return an ArrayList containing Integers that represent the version
240      * number. The ordered of these are significant
241      */

242     public ArrayList JavaDoc getOrderedVersionNumbers() {
243         return orderedVersionNumbers;
244     }
245
246     /**
247      * Returns the beta number part of the version number
248      * @return an integer representing the beta number, or -1 if this is not
249      * a beta version.
250      */

251     public int getBetaNumber() {
252         return betaNumber;
253     }
254
255     /**
256      * Returns the release candidate number part of the version number
257      * @return an integer representing the release candidate number, or -1
258      * if this is not a release candidate version.
259      */

260     public int getReleaseCandidateNumber() {
261         return releaseCandidateNumber;
262     }
263
264     public boolean equals(Object JavaDoc obj) {
265         if (obj instanceof Version) {
266             Version rightVersion = (Version) obj;
267             ArrayList JavaDoc rightOrderedVersionNumbers = rightVersion.getOrderedVersionNumbers();
268             if (orderedVersionNumbers.size() != rightOrderedVersionNumbers.size()) {
269                 return false;
270             }
271             if (betaNumber != rightVersion.getBetaNumber()) {
272                 return false;
273             }
274             if (releaseCandidateNumber != rightVersion.getReleaseCandidateNumber()) {
275                 return false;
276             }
277             for (int i=0; i < orderedVersionNumbers.size(); i++) {
278                 Integer JavaDoc leftVersionNumber = (Integer JavaDoc) orderedVersionNumbers.get(i);
279                 Integer JavaDoc rightVersionNumber = (Integer JavaDoc) rightOrderedVersionNumbers.get(i);
280                 if (leftVersionNumber.intValue() != rightVersionNumber.intValue()) {
281                     return false;
282                 }
283             }
284             // if we got here it means the version are equal.
285
return true;
286         } else {
287             return false;
288         }
289     }
290
291 }
Popular Tags