KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > java > plugin > registry > Version


1 /*****************************************************************************
2  * Java Plug-in Framework (JPF)
3  * Copyright (C) 2004-2006 Dmitry Olshansky
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *****************************************************************************/

19 package org.java.plugin.registry;
20
21 import java.io.IOException JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.io.Serializable JavaDoc;
25 import java.util.Locale JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * This class represents a plug-in version identifier.
30  * <br>
31  * @version $Id: Version.java,v 1.2 2006/02/22 18:40:50 ddimon Exp $
32  */

33 public final class Version implements Serializable JavaDoc, Comparable JavaDoc {
34     private static final long serialVersionUID = -3054349171116917643L;
35
36     /**
37      * Version identifier parts separator.
38      */

39     public static final char SEPARATOR = '.';
40     
41     /**
42      * Parses given string as version identifier. All missing parts will be
43      * initialized to 0 or empty string. Parsing starts from left side of the
44      * string.
45      * @param str version identifier as string
46      * @return version identifier object
47      */

48     public static Version parse(final String JavaDoc str) {
49         Version result = new Version();
50         result.parseString(str);
51         return result;
52     }
53
54     private transient int major;
55     private transient int minor;
56     private transient int build;
57     private transient String JavaDoc name;
58     private transient String JavaDoc asString;
59     
60     private Version() {
61         // no-op
62
}
63     
64     private void parseString(final String JavaDoc str) {
65         major = 0;
66         minor = 0;
67         build = 0;
68         name = ""; //$NON-NLS-1$
69
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(str, "" + SEPARATOR, false); //$NON-NLS-1$
70
// major segment
71
if (!st.hasMoreTokens()) {
72             return;
73         }
74         String JavaDoc token = st.nextToken();
75         try {
76             major = Integer.parseInt(token, 10);
77         } catch (NumberFormatException JavaDoc nfe) {
78             name = token;
79             while (st.hasMoreTokens()) {
80                 name += st.nextToken();
81             }
82             return;
83         }
84         // minor segment
85
if (!st.hasMoreTokens()) {
86             return;
87         }
88         token = st.nextToken();
89         try {
90             minor = Integer.parseInt(token, 10);
91         } catch (NumberFormatException JavaDoc nfe) {
92             name = token;
93             while (st.hasMoreTokens()) {
94                 name += st.nextToken();
95             }
96             return;
97         }
98         // build segment
99
if (!st.hasMoreTokens()) {
100             return;
101         }
102         token = st.nextToken();
103         try {
104             build = Integer.parseInt(token, 10);
105         } catch (NumberFormatException JavaDoc nfe) {
106             name = token;
107             while (st.hasMoreTokens()) {
108                 name += st.nextToken();
109             }
110             return;
111         }
112         // name segment
113
if (st.hasMoreTokens()) {
114             name = st.nextToken();
115             while (st.hasMoreTokens()) {
116                 name += st.nextToken();
117             }
118         }
119     }
120     
121     /**
122      * Creates version identifier object from given parts. No validation
123      * performed during object instantiation, all values become parts of
124      * version identifier as they are.
125      * @param aMajor major version number
126      * @param aMinor minor version number
127      * @param aBuild build number
128      * @param aName build name, <code>null</code> value becomes empty string
129      */

130     public Version(final int aMajor, final int aMinor, final int aBuild,
131             final String JavaDoc aName) {
132         major = aMajor;
133         minor = aMinor;
134         build = aBuild;
135         name = (aName == null) ? "" : aName; //$NON-NLS-1$
136
}
137
138     /**
139      * @return build number
140      */

141     public int getBuild() {
142         return build;
143     }
144
145     /**
146      * @return major version number
147      */

148     public int getMajor() {
149         return major;
150     }
151
152     /**
153      * @return minor version number
154      */

155     public int getMinor() {
156         return minor;
157     }
158     
159     /**
160      * @return build name
161      */

162     public String JavaDoc getName() {
163         return name;
164     }
165
166     /**
167      * Compares two version identifiers to see if this one is
168      * greater than or equal to the argument.
169      * <p>
170      * A version identifier is considered to be greater than or equal
171      * if its major component is greater than the argument major
172      * component, or the major components are equal and its minor component
173      * is greater than the argument minor component, or the
174      * major and minor components are equal and its build component is
175      * greater than the argument build component, or all components are equal.
176      * </p>
177      *
178      * @param other the other version identifier
179      * @return <code>true</code> if this version identifier
180      * is compatible with the given version identifier, and
181      * <code>false</code> otherwise
182      */

183     public boolean isGreaterOrEqualTo(final Version other) {
184         if (other == null) {
185             return false;
186         }
187         if (major > other.major) {
188             return true;
189         }
190         if ((major == other.major) && (minor > other.minor)) {
191             return true;
192         }
193         if ((major == other.major) && (minor == other.minor)
194                 && (build > other.build)) {
195             return true;
196         }
197         if ((major == other.major) && (minor == other.minor)
198                 && (build == other.build)
199                 && name.equalsIgnoreCase(other.name)) {
200             return true;
201         }
202         return false;
203     }
204
205     /**
206      * Compares two version identifiers for compatibility.
207      * <p>
208      * A version identifier is considered to be compatible if its major
209      * component equals to the argument major component, and its minor component
210      * is greater than or equal to the argument minor component.
211      * If the minor components are equal, than the build component of the
212      * version identifier must be greater than or equal to the build component
213      * of the argument identifier.
214      * </p>
215      *
216      * @param other the other version identifier
217      * @return <code>true</code> if this version identifier
218      * is compatible with the given version identifier, and
219      * <code>false</code> otherwise
220      */

221     public boolean isCompatibleWith(final Version other) {
222         if (other == null) {
223             return false;
224         }
225         if (major != other.major) {
226             return false;
227         }
228         if (minor > other.minor) {
229             return true;
230         }
231         if (minor < other.minor) {
232             return false;
233         }
234         if (build >= other.build) {
235             return true;
236         }
237         return false;
238     }
239
240     /**
241      * Compares two version identifiers for equivalency.
242      * <p>
243      * Two version identifiers are considered to be equivalent if their major
244      * and minor components equal and are at least at the same build level
245      * as the argument.
246      * </p>
247      *
248      * @param other the other version identifier
249      * @return <code>true</code> if this version identifier
250      * is equivalent to the given version identifier, and
251      * <code>false</code> otherwise
252      */

253     public boolean isEquivalentTo(final Version other) {
254         if (other == null) {
255             return false;
256         }
257         if (major != other.major) {
258             return false;
259         }
260         if (minor != other.minor) {
261             return false;
262         }
263         if (build >= other.build) {
264             return true;
265         }
266         return false;
267     }
268
269     /**
270      * Compares two version identifiers for order using multi-decimal
271      * comparison.
272      *
273      * @param other the other version identifier
274      * @return <code>true</code> if this version identifier
275      * is greater than the given version identifier, and
276      * <code>false</code> otherwise
277      */

278     public boolean isGreaterThan(final Version other) {
279         if (other == null) {
280             return false;
281         }
282         if (major > other.major) {
283             return true;
284         }
285         if (major < other.major) {
286             return false;
287         }
288         if (minor > other.minor) {
289             return true;
290         }
291         if (minor < other.minor) {
292             return false;
293         }
294         if (build > other.build) {
295             return true;
296         }
297         return false;
298
299     }
300
301     /**
302      * @see java.lang.Object#hashCode()
303      */

304     public int hashCode() {
305         return toString().hashCode();
306     }
307     
308     /**
309      * @see java.lang.Object#equals(java.lang.Object)
310      */

311     public boolean equals(final Object JavaDoc obj) {
312         if (this == obj) {
313             return true;
314         }
315         if (!(obj instanceof Version)) {
316             return false;
317         }
318         Version other = (Version) obj;
319         if ((major != other.major) || (minor != other.minor)
320                 || (build != other.build)
321                 || !name.equalsIgnoreCase(other.name)) {
322             return false;
323         }
324         return true;
325     }
326
327     /**
328      * Returns the string representation of this version identifier.
329      * The result satisfies
330      * <code>version.equals(new Version(version.toString()))</code>.
331      * @return the string representation of this version identifier
332      */

333     public String JavaDoc toString() {
334         if (asString == null) {
335             asString = "" + major + SEPARATOR + minor + SEPARATOR + build //$NON-NLS-1$
336
+ (name.length() == 0 ? "" : SEPARATOR + name); //$NON-NLS-1$
337
}
338         return asString;
339     }
340
341     /**
342      * @see java.lang.Comparable#compareTo(java.lang.Object)
343      */

344     public int compareTo(final Object JavaDoc obj) {
345         if (equals(obj)) {
346             return 0;
347         }
348         if (!(obj instanceof Version)) {
349             throw new ClassCastException JavaDoc();
350         }
351         Version other = (Version) obj;
352         if (major != other.major) {
353             return major - other.major;
354         }
355         if (minor != other.minor) {
356             return minor - other.minor;
357         }
358         if (build != other.build) {
359             return build - other.build;
360         }
361         return name.toLowerCase(Locale.ENGLISH).compareTo(
362                 other.name.toLowerCase(Locale.ENGLISH));
363     }
364     
365     // Serialization related stuff.
366

367     private void writeObject(final ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
368         out.writeUTF(toString());
369     }
370     
371     private void readObject(final ObjectInputStream JavaDoc in) throws IOException JavaDoc {
372         parseString(in.readUTF());
373     }
374 }
375
Popular Tags