KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > util > VersionInfo


1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2006, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7
8 package com.ibm.icu.util;
9
10 import java.util.HashMap JavaDoc;
11
12 /**
13  * Class to store version numbers of the form major.minor.milli.micro.
14  * @author synwee
15  * @stable ICU 2.6
16  */

17 public final class VersionInfo
18 {
19     // public data members -------------------------------------------------
20

21     /**
22      * Unicode 1.0 version
23      * @stable ICU 2.6
24      */

25     public static final VersionInfo UNICODE_1_0;
26     /**
27      * Unicode 1.0.1 version
28      * @stable ICU 2.6
29      */

30     public static final VersionInfo UNICODE_1_0_1;
31     /**
32      * Unicode 1.1.0 version
33      * @stable ICU 2.6
34      */

35     public static final VersionInfo UNICODE_1_1_0;
36     /**
37      * Unicode 1.1.5 version
38      * @stable ICU 2.6
39      */

40     public static final VersionInfo UNICODE_1_1_5;
41     /**
42      * Unicode 2.0 version
43      * @stable ICU 2.6
44      */

45     public static final VersionInfo UNICODE_2_0;
46     /**
47      * Unicode 2.1.2 version
48      * @stable ICU 2.6
49      */

50     public static final VersionInfo UNICODE_2_1_2;
51     /**
52      * Unicode 2.1.5 version
53      * @stable ICU 2.6
54      */

55     public static final VersionInfo UNICODE_2_1_5;
56     /**
57      * Unicode 2.1.8 version
58      * @stable ICU 2.6
59      */

60     public static final VersionInfo UNICODE_2_1_8;
61     /**
62      * Unicode 2.1.9 version
63      * @stable ICU 2.6
64      */

65     public static final VersionInfo UNICODE_2_1_9;
66     /**
67      * Unicode 3.0 version
68      * @stable ICU 2.6
69      */

70     public static final VersionInfo UNICODE_3_0;
71     /**
72      * Unicode 3.0.1 version
73      * @stable ICU 2.6
74      */

75     public static final VersionInfo UNICODE_3_0_1;
76     /**
77      * Unicode 3.1.0 version
78      * @stable ICU 2.6
79      */

80     public static final VersionInfo UNICODE_3_1_0;
81     /**
82      * Unicode 3.1.1 version
83      * @stable ICU 2.6
84      */

85     public static final VersionInfo UNICODE_3_1_1;
86     /**
87      * Unicode 3.2 version
88      * @stable ICU 2.6
89      */

90     public static final VersionInfo UNICODE_3_2;
91
92     /**
93      * Unicode 4.0 version
94      * @stable ICU 2.6
95      */

96     public static final VersionInfo UNICODE_4_0;
97
98     /**
99      * Unicode 4.0.1 version
100      * @draft ICU 3.4
101      * @provisional This API might change or be removed in a future release.
102      */

103     public static final VersionInfo UNICODE_4_0_1;
104
105     /**
106      * Unicode 4.1 version
107      * @draft ICU 3.4
108      * @provisional This API might change or be removed in a future release.
109      */

110     public static final VersionInfo UNICODE_4_1;
111
112     /**
113      * Unicode 5.0 version
114      * @draft ICU 3.4
115      * @provisional This API might change or be removed in a future release.
116      */

117     public static final VersionInfo UNICODE_5_0;
118
119     /**
120      * ICU4J current release version
121      * @stable ICU 2.8
122      */

123     public static final VersionInfo ICU_VERSION;
124
125     /**
126      * Data version string for ICU's internal data
127      * @internal
128      * @deprecated This API is ICU internal only.
129      */

130     public static final String JavaDoc ICU_DATA_VERSION = "36b";
131
132     /**
133      * ICU4J collator runtime version
134      * @stable ICU 2.8
135      */

136     public static final VersionInfo UCOL_RUNTIME_VERSION;
137
138     /**
139      * ICU4J collator builder version
140      * @stable ICU 2.8
141      */

142     public static final VersionInfo UCOL_BUILDER_VERSION;
143
144     /**
145      * ICU4J collator tailorings version
146      * @stable ICU 2.8
147      */

148     public static final VersionInfo UCOL_TAILORINGS_VERSION;
149
150
151     // public methods ------------------------------------------------------
152

153     /**
154      * Returns an instance of VersionInfo with the argument version.
155      * @param version version String in the format of "major.minor.milli.micro"
156      * or "major.minor.milli" or "major.minor" or "major",
157      * where major, minor, milli, micro are non-negative numbers
158      * <= 255. If the trailing version numbers are
159      * not specified they are taken as 0s. E.g. Version "3.1" is
160      * equivalent to "3.1.0.0".
161      * @return an instance of VersionInfo with the argument version.
162      * @exception throws an IllegalArgumentException when the argument version
163      * is not in the right format
164      * @stable ICU 2.6
165      */

166     public static VersionInfo getInstance(String JavaDoc version)
167     {
168         int length = version.length();
169         int array[] = {0, 0, 0, 0};
170         int count = 0;
171         int index = 0;
172
173         while (count < 4 && index < length) {
174             char c = version.charAt(index);
175             if (c == '.') {
176                 count ++;
177             }
178             else {
179                 c -= '0';
180                 if (c < 0 || c > 9) {
181                     throw new IllegalArgumentException JavaDoc(INVALID_VERSION_NUMBER_);
182                 }
183                 array[count] *= 10;
184                 array[count] += c;
185             }
186             index ++;
187         }
188         if (index != length) {
189             throw new IllegalArgumentException JavaDoc(
190                                                "Invalid version number: String '" + version + "' exceeds version format");
191         }
192         for (int i = 0; i < 4; i ++) {
193             if (array[i] < 0 || array[i] > 255) {
194                 throw new IllegalArgumentException JavaDoc(INVALID_VERSION_NUMBER_);
195             }
196         }
197
198         return getInstance(array[0], array[1], array[2], array[3]);
199     }
200
201     /**
202      * Returns an instance of VersionInfo with the argument version.
203      * @param major major version, non-negative number <= 255.
204      * @param minor minor version, non-negative number <= 255.
205      * @param milli milli version, non-negative number <= 255.
206      * @param micro micro version, non-negative number <= 255.
207      * @exception throws an IllegalArgumentException when either arguments are
208      * negative or > 255
209      * @stable ICU 2.6
210      */

211     public static VersionInfo getInstance(int major, int minor, int milli,
212                                           int micro)
213     {
214         // checks if it is in the hashmap
215
// else
216
if (major < 0 || major > 255 || minor < 0 || minor > 255 ||
217             milli < 0 || milli > 255 || micro < 0 || micro > 255) {
218             throw new IllegalArgumentException JavaDoc(INVALID_VERSION_NUMBER_);
219         }
220         int version = getInt(major, minor, milli, micro);
221         Integer JavaDoc key = new Integer JavaDoc(version);
222         Object JavaDoc result = MAP_.get(key);
223         if (result == null) {
224             result = new VersionInfo(version);
225             MAP_.put(key, result);
226         }
227         return (VersionInfo)result;
228     }
229
230     /**
231      * Returns an instance of VersionInfo with the argument version.
232      * Equivalent to getInstance(major, minor, milli, 0).
233      * @param major major version, non-negative number <= 255.
234      * @param minor minor version, non-negative number <= 255.
235      * @param milli milli version, non-negative number <= 255.
236      * @exception throws an IllegalArgumentException when either arguments are
237      * negative or > 255
238      * @stable ICU 2.6
239      */

240     public static VersionInfo getInstance(int major, int minor, int milli)
241     {
242         return getInstance(major, minor, milli, 0);
243     }
244
245     /**
246      * Returns an instance of VersionInfo with the argument version.
247      * Equivalent to getInstance(major, minor, 0, 0).
248      * @param major major version, non-negative number <= 255.
249      * @param minor minor version, non-negative number <= 255.
250      * @exception throws an IllegalArgumentException when either arguments are
251      * negative or > 255
252      * @stable ICU 2.6
253      */

254     public static VersionInfo getInstance(int major, int minor)
255     {
256         return getInstance(major, minor, 0, 0);
257     }
258
259     /**
260      * Returns an instance of VersionInfo with the argument version.
261      * Equivalent to getInstance(major, 0, 0, 0).
262      * @param major major version, non-negative number <= 255.
263      * @exception throws an IllegalArgumentException when either arguments are
264      * negative or > 255
265      * @stable ICU 2.6
266      */

267     public static VersionInfo getInstance(int major)
268     {
269         return getInstance(major, 0, 0, 0);
270     }
271
272     private static VersionInfo javaVersion;
273
274     /**
275      * @internal
276      * @deprecated This API is ICU internal only.
277      */

278     public static VersionInfo javaVersion() {
279         if (javaVersion == null) {
280             String JavaDoc s = System.getProperty("java.version");
281             // clean string
282
// preserve only digits, separated by single '.'
283
// ignore over 4 digit sequences
284
// does not test < 255, very odd...
285

286             char[] chars = s.toCharArray();
287             int r = 0, w = 0, count = 0;
288             boolean numeric = false; // ignore leading non-numerics
289
while (r < chars.length) {
290                 char c = chars[r++];
291                 if (c < '0' || c > '9') {
292                     if (numeric) {
293                         if (count == 3) {
294                             // only four digit strings allowed
295
break;
296                         }
297                         numeric = false;
298                         chars[w++] = '.';
299                         ++count;
300                     }
301                 } else {
302                     numeric = true;
303                     chars[w++] = c;
304                 }
305             }
306             while (w > 0 && chars[w-1] == '.') {
307                 --w;
308             }
309
310             String JavaDoc vs = new String JavaDoc(chars, 0, w);
311
312             javaVersion = VersionInfo.getInstance(vs);
313         }
314         return javaVersion;
315     }
316
317     /**
318      * Returns the String representative of VersionInfo in the format of
319      * "major.minor.milli.micro"
320      * @return String representative of VersionInfo
321      * @stable ICU 2.6
322      */

323     public String JavaDoc toString()
324     {
325         StringBuffer JavaDoc result = new StringBuffer JavaDoc(7);
326         result.append(getMajor());
327         result.append('.');
328         result.append(getMinor());
329         result.append('.');
330         result.append(getMilli());
331         result.append('.');
332         result.append(getMicro());
333         return result.toString();
334     }
335
336     /**
337      * Returns the major version number
338      * @return the major version number
339      * @stable ICU 2.6
340      */

341     public int getMajor()
342     {
343         return (m_version_ >> 24) & LAST_BYTE_MASK_ ;
344     }
345
346     /**
347      * Returns the minor version number
348      * @return the minor version number
349      * @stable ICU 2.6
350      */

351     public int getMinor()
352     {
353         return (m_version_ >> 16) & LAST_BYTE_MASK_ ;
354     }
355
356     /**
357      * Returns the milli version number
358      * @return the milli version number
359      * @stable ICU 2.6
360      */

361     public int getMilli()
362     {
363         return (m_version_ >> 8) & LAST_BYTE_MASK_ ;
364     }
365
366     /**
367      * Returns the micro version number
368      * @return the micro version number
369      * @stable ICU 2.6
370      */

371     public int getMicro()
372     {
373         return m_version_ & LAST_BYTE_MASK_ ;
374     }
375
376     /**
377      * Checks if this version information is equals to the argument version
378      * @param other object to be compared
379      * @return true if other is equals to this object's version information,
380      * false otherwise
381      * @stable ICU 2.6
382      */

383     public boolean equals(Object JavaDoc other)
384     {
385         return other == this;
386     }
387
388     /**
389      * Compares other with this VersionInfo.
390      * @param other VersionInfo to be compared
391      * @return 0 if the argument is a VersionInfo object that has version
392      * information equals to this object.
393      * Less than 0 if the argument is a VersionInfo object that has
394      * version information greater than this object.
395      * Greater than 0 if the argument is a VersionInfo object that
396      * has version information less than this object.
397      * @stable ICU 2.6
398      */

399     public int compareTo(VersionInfo other)
400     {
401         return m_version_ - other.m_version_;
402     }
403
404     // private data members ----------------------------------------------
405

406     /**
407      * Version number stored as a byte for each of the major, minor, milli and
408      * micro numbers in the 32 bit int.
409      * Most significant for the major and the least significant contains the
410      * micro numbers.
411      */

412     private int m_version_;
413     /**
414      * Map of singletons
415      */

416     private static final HashMap JavaDoc MAP_ = new HashMap JavaDoc();
417     /**
418      * Last byte mask
419      */

420     private static final int LAST_BYTE_MASK_ = 0xFF;
421     /**
422      * Error statement string
423      */

424     private static final String JavaDoc INVALID_VERSION_NUMBER_ =
425         "Invalid version number: Version number may be negative or greater than 255";
426
427     // static declaration ------------------------------------------------
428

429     /**
430      * Initialize versions only after MAP_ has been created
431      */

432     static {
433         UNICODE_1_0 = getInstance(1, 0, 0, 0);
434         UNICODE_1_0_1 = getInstance(1, 0, 1, 0);
435         UNICODE_1_1_0 = getInstance(1, 1, 0, 0);
436         UNICODE_1_1_5 = getInstance(1, 1, 5, 0);
437         UNICODE_2_0 = getInstance(2, 0, 0, 0);
438         UNICODE_2_1_2 = getInstance(2, 1, 2, 0);
439         UNICODE_2_1_5 = getInstance(2, 1, 5, 0);
440         UNICODE_2_1_8 = getInstance(2, 1, 8, 0);
441         UNICODE_2_1_9 = getInstance(2, 1, 9, 0);
442         UNICODE_3_0 = getInstance(3, 0, 0, 0);
443         UNICODE_3_0_1 = getInstance(3, 0, 1, 0);
444         UNICODE_3_1_0 = getInstance(3, 1, 0, 0);
445         UNICODE_3_1_1 = getInstance(3, 1, 1, 0);
446         UNICODE_3_2 = getInstance(3, 2, 0, 0);
447         UNICODE_4_0 = getInstance(4, 0, 0, 0);
448         UNICODE_4_0_1 = getInstance(4, 0, 1, 0);
449         UNICODE_4_1 = getInstance(4, 1, 0, 0);
450         UNICODE_5_0 = getInstance(4, 1, 0, 0);
451         ICU_VERSION = getInstance(3, 6, 1, 0);
452         UCOL_RUNTIME_VERSION = getInstance(6);
453         UCOL_BUILDER_VERSION = getInstance(7);
454         UCOL_TAILORINGS_VERSION = getInstance(1);
455     }
456
457     // private constructor -----------------------------------------------
458

459     /**
460      * Constructor with int
461      * @param compactversion a 32 bit int with each byte representing a number
462      */

463     private VersionInfo(int compactversion)
464     {
465         m_version_ = compactversion;
466     }
467
468     /**
469      * Gets the int from the version numbers
470      * @param major non-negative version number
471      * @param minor non-negativeversion number
472      * @param milli non-negativeversion number
473      * @param micro non-negativeversion number
474      */

475     private static int getInt(int major, int minor, int milli, int micro)
476     {
477         return (major << 24) | (minor << 16) | (milli << 8) | micro;
478     }
479 }
480
Popular Tags