]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/classes/core/src/com/ibm/icu/util/VersionInfo.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / classes / core / src / com / ibm / icu / util / VersionInfo.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 package com.ibm.icu.util;\r
9 \r
10 import java.util.HashMap;\r
11 \r
12 /**\r
13  * Class to store version numbers of the form major.minor.milli.micro.\r
14  * @author synwee\r
15  * @stable ICU 2.6\r
16  */\r
17 public final class VersionInfo implements Comparable<VersionInfo>\r
18 {\r
19     // public data members -------------------------------------------------\r
20 \r
21     /**\r
22      * Unicode 1.0 version\r
23      * @stable ICU 2.6\r
24      */\r
25     public static final VersionInfo UNICODE_1_0;\r
26     /**\r
27      * Unicode 1.0.1 version\r
28      * @stable ICU 2.6\r
29      */\r
30     public static final VersionInfo UNICODE_1_0_1;\r
31     /**\r
32      * Unicode 1.1.0 version\r
33      * @stable ICU 2.6\r
34      */\r
35     public static final VersionInfo UNICODE_1_1_0;\r
36     /**\r
37      * Unicode 1.1.5 version\r
38      * @stable ICU 2.6\r
39      */\r
40     public static final VersionInfo UNICODE_1_1_5;\r
41     /**\r
42      * Unicode 2.0 version\r
43      * @stable ICU 2.6\r
44      */\r
45     public static final VersionInfo UNICODE_2_0;\r
46     /**\r
47      * Unicode 2.1.2 version\r
48      * @stable ICU 2.6\r
49      */\r
50     public static final VersionInfo UNICODE_2_1_2;\r
51     /**\r
52      * Unicode 2.1.5 version\r
53      * @stable ICU 2.6\r
54      */\r
55     public static final VersionInfo UNICODE_2_1_5;\r
56     /**\r
57      * Unicode 2.1.8 version\r
58      * @stable ICU 2.6\r
59      */\r
60     public static final VersionInfo UNICODE_2_1_8;\r
61     /**\r
62      * Unicode 2.1.9 version\r
63      * @stable ICU 2.6\r
64      */\r
65     public static final VersionInfo UNICODE_2_1_9;\r
66     /**\r
67      * Unicode 3.0 version\r
68      * @stable ICU 2.6\r
69      */\r
70     public static final VersionInfo UNICODE_3_0;\r
71     /**\r
72      * Unicode 3.0.1 version\r
73      * @stable ICU 2.6\r
74      */\r
75     public static final VersionInfo UNICODE_3_0_1;\r
76     /**\r
77      * Unicode 3.1.0 version\r
78      * @stable ICU 2.6\r
79      */\r
80     public static final VersionInfo UNICODE_3_1_0;\r
81     /**\r
82      * Unicode 3.1.1 version\r
83      * @stable ICU 2.6\r
84      */\r
85     public static final VersionInfo UNICODE_3_1_1;\r
86     /**\r
87      * Unicode 3.2 version\r
88      * @stable ICU 2.6\r
89      */\r
90     public static final VersionInfo UNICODE_3_2;\r
91 \r
92     /**\r
93      * Unicode 4.0 version\r
94      * @stable ICU 2.6\r
95      */\r
96     public static final VersionInfo UNICODE_4_0;\r
97 \r
98     /**\r
99      * Unicode 4.0.1 version\r
100      * @stable ICU 3.4\r
101      */\r
102     public static final VersionInfo UNICODE_4_0_1;\r
103 \r
104     /**\r
105      * Unicode 4.1 version\r
106      * @stable ICU 3.4\r
107      */\r
108     public static final VersionInfo UNICODE_4_1;\r
109 \r
110     /**\r
111      * Unicode 5.0 version\r
112      * @stable ICU 3.4\r
113      */\r
114     public static final VersionInfo UNICODE_5_0;\r
115 \r
116     /**\r
117      * Unicode 5.1 version\r
118      * @stable ICU 4.2\r
119      */\r
120     public static final VersionInfo UNICODE_5_1;\r
121 \r
122     /**\r
123      * Unicode 5.2 version\r
124      * @stable ICU 4.4\r
125      */\r
126     public static final VersionInfo UNICODE_5_2;\r
127 \r
128     /**\r
129      * ICU4J current release version\r
130      * @stable ICU 2.8\r
131      */\r
132     public static final VersionInfo ICU_VERSION;\r
133 \r
134     /**\r
135      * Data version string for ICU's internal data.\r
136      * Used for appending to data path (e.g. icudt43b)\r
137      * @internal\r
138      * @deprecated This API is ICU internal only.\r
139      */\r
140     public static final String ICU_DATA_VERSION_PATH = "44b";\r
141     \r
142     /**\r
143      * Data version in ICU4J.\r
144      * @internal\r
145      * @deprecated This API is ICU internal only.\r
146      */\r
147     public static final VersionInfo ICU_DATA_VERSION;\r
148 \r
149     /**\r
150      * ICU4J collator runtime version\r
151      * @stable ICU 2.8\r
152      */\r
153     public static final VersionInfo UCOL_RUNTIME_VERSION;\r
154 \r
155     /**\r
156      * ICU4J collator builder version\r
157      * @stable ICU 2.8\r
158      */\r
159     public static final VersionInfo UCOL_BUILDER_VERSION;\r
160 \r
161     /**\r
162      * ICU4J collator tailorings version\r
163      * @stable ICU 2.8\r
164      */\r
165     public static final VersionInfo UCOL_TAILORINGS_VERSION;\r
166 \r
167 \r
168     // public methods ------------------------------------------------------\r
169 \r
170     /**\r
171      * Returns an instance of VersionInfo with the argument version.\r
172      * @param version version String in the format of "major.minor.milli.micro"\r
173      *                or "major.minor.milli" or "major.minor" or "major",\r
174      *                where major, minor, milli, micro are non-negative numbers\r
175      *                <= 255. If the trailing version numbers are\r
176      *                not specified they are taken as 0s. E.g. Version "3.1" is\r
177      *                equivalent to "3.1.0.0".\r
178      * @return an instance of VersionInfo with the argument version.\r
179      * @exception IllegalArgumentException when the argument version\r
180      *                is not in the right format\r
181      * @stable ICU 2.6\r
182      */\r
183     public static VersionInfo getInstance(String version)\r
184     {\r
185         int length  = version.length();\r
186         int array[] = {0, 0, 0, 0};\r
187         int count   = 0;\r
188         int index   = 0;\r
189 \r
190         while (count < 4 && index < length) {\r
191             char c = version.charAt(index);\r
192             if (c == '.') {\r
193                 count ++;\r
194             }\r
195             else {\r
196                 c -= '0';\r
197                 if (c < 0 || c > 9) {\r
198                     throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);\r
199                 }\r
200                 array[count] *= 10;\r
201                 array[count] += c;\r
202             }\r
203             index ++;\r
204         }\r
205         if (index != length) {\r
206             throw new IllegalArgumentException(\r
207                                                "Invalid version number: String '" + version + "' exceeds version format");\r
208         }\r
209         for (int i = 0; i < 4; i ++) {\r
210             if (array[i] < 0 || array[i] > 255) {\r
211                 throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);\r
212             }\r
213         }\r
214 \r
215         return getInstance(array[0], array[1], array[2], array[3]);\r
216     }\r
217 \r
218     /**\r
219      * Returns an instance of VersionInfo with the argument version.\r
220      * @param major major version, non-negative number <= 255.\r
221      * @param minor minor version, non-negative number <= 255.\r
222      * @param milli milli version, non-negative number <= 255.\r
223      * @param micro micro version, non-negative number <= 255.\r
224      * @exception IllegalArgumentException when either arguments are negative or > 255\r
225      * @stable ICU 2.6\r
226      */\r
227     public static VersionInfo getInstance(int major, int minor, int milli,\r
228                                           int micro)\r
229     {\r
230         // checks if it is in the hashmap\r
231         // else\r
232         if (major < 0 || major > 255 || minor < 0 || minor > 255 ||\r
233             milli < 0 || milli > 255 || micro < 0 || micro > 255) {\r
234             throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);\r
235         }\r
236         int     version = getInt(major, minor, milli, micro);\r
237         Integer key     = new Integer(version);\r
238         VersionInfo  result  = MAP_.get(key);\r
239         if (result == null) {\r
240             result = new VersionInfo(version);\r
241             MAP_.put(key, result);\r
242         }\r
243         return result;\r
244     }\r
245 \r
246     /**\r
247      * Returns an instance of VersionInfo with the argument version.\r
248      * Equivalent to getInstance(major, minor, milli, 0).\r
249      * @param major major version, non-negative number <= 255.\r
250      * @param minor minor version, non-negative number <= 255.\r
251      * @param milli milli version, non-negative number <= 255.\r
252      * @exception IllegalArgumentException when either arguments are\r
253      *                                     negative or > 255\r
254      * @stable ICU 2.6\r
255      */\r
256     public static VersionInfo getInstance(int major, int minor, int milli)\r
257     {\r
258         return getInstance(major, minor, milli, 0);\r
259     }\r
260 \r
261     /**\r
262      * Returns an instance of VersionInfo with the argument version.\r
263      * Equivalent to getInstance(major, minor, 0, 0).\r
264      * @param major major version, non-negative number <= 255.\r
265      * @param minor minor version, non-negative number <= 255.\r
266      * @exception IllegalArgumentException when either arguments are\r
267      *                                     negative or > 255\r
268      * @stable ICU 2.6\r
269      */\r
270     public static VersionInfo getInstance(int major, int minor)\r
271     {\r
272         return getInstance(major, minor, 0, 0);\r
273     }\r
274 \r
275     /**\r
276      * Returns an instance of VersionInfo with the argument version.\r
277      * Equivalent to getInstance(major, 0, 0, 0).\r
278      * @param major major version, non-negative number <= 255.\r
279      * @exception IllegalArgumentException when either arguments are\r
280      *                                     negative or > 255\r
281      * @stable ICU 2.6\r
282      */\r
283     public static VersionInfo getInstance(int major)\r
284     {\r
285         return getInstance(major, 0, 0, 0);\r
286     }\r
287 \r
288     private static VersionInfo javaVersion;\r
289 \r
290     /**\r
291      * @internal\r
292      * @deprecated This API is ICU internal only.\r
293      */\r
294     public static VersionInfo javaVersion() {\r
295         if (javaVersion == null) {\r
296             String s = System.getProperty("java.version");\r
297             // clean string\r
298             // preserve only digits, separated by single '.'\r
299             // ignore over 4 digit sequences\r
300             // does not test < 255, very odd...\r
301 \r
302             char[] chars = s.toCharArray();\r
303             int r = 0, w = 0, count = 0;\r
304             boolean numeric = false; // ignore leading non-numerics\r
305             while (r < chars.length) {\r
306                 char c = chars[r++];\r
307                 if (c < '0' || c > '9') {\r
308                     if (numeric) {\r
309                         if (count == 3) {\r
310                             // only four digit strings allowed\r
311                             break;\r
312                         }\r
313                         numeric = false;\r
314                         chars[w++] = '.';\r
315                         ++count;\r
316                     }\r
317                 } else {\r
318                     numeric = true;\r
319                     chars[w++] = c;\r
320                 }\r
321             }\r
322             while (w > 0 && chars[w-1] == '.') {\r
323                 --w;\r
324             }\r
325 \r
326             String vs = new String(chars, 0, w);\r
327 \r
328             javaVersion = VersionInfo.getInstance(vs);\r
329         }\r
330         return javaVersion;\r
331     }\r
332 \r
333     /**\r
334      * Returns the String representative of VersionInfo in the format of\r
335      * "major.minor.milli.micro"\r
336      * @return String representative of VersionInfo\r
337      * @stable ICU 2.6\r
338      */\r
339     public String toString()\r
340     {\r
341         StringBuilder result = new StringBuilder(7);\r
342         result.append(getMajor());\r
343         result.append('.');\r
344         result.append(getMinor());\r
345         result.append('.');\r
346         result.append(getMilli());\r
347         result.append('.');\r
348         result.append(getMicro());\r
349         return result.toString();\r
350     }\r
351 \r
352     /**\r
353      * Returns the major version number\r
354      * @return the major version number\r
355      * @stable ICU 2.6\r
356      */\r
357     public int getMajor()\r
358     {\r
359         return (m_version_ >> 24) & LAST_BYTE_MASK_ ;\r
360     }\r
361 \r
362     /**\r
363      * Returns the minor version number\r
364      * @return the minor version number\r
365      * @stable ICU 2.6\r
366      */\r
367     public int getMinor()\r
368     {\r
369         return (m_version_ >> 16) & LAST_BYTE_MASK_ ;\r
370     }\r
371 \r
372     /**\r
373      * Returns the milli version number\r
374      * @return the milli version number\r
375      * @stable ICU 2.6\r
376      */\r
377     public int getMilli()\r
378     {\r
379         return (m_version_ >> 8) & LAST_BYTE_MASK_ ;\r
380     }\r
381 \r
382     /**\r
383      * Returns the micro version number\r
384      * @return the micro version number\r
385      * @stable ICU 2.6\r
386      */\r
387     public int getMicro()\r
388     {\r
389         return m_version_ & LAST_BYTE_MASK_ ;\r
390     }\r
391 \r
392     /**\r
393      * Checks if this version information is equals to the argument version\r
394      * @param other object to be compared\r
395      * @return true if other is equals to this object's version information,\r
396      *         false otherwise\r
397      * @stable ICU 2.6\r
398      */\r
399     public boolean equals(Object other)\r
400     {\r
401         return other == this;\r
402     }\r
403 \r
404     /**\r
405      * Compares other with this VersionInfo.\r
406      * @param other VersionInfo to be compared\r
407      * @return 0 if the argument is a VersionInfo object that has version\r
408      *           information equals to this object.\r
409      *           Less than 0 if the argument is a VersionInfo object that has\r
410      *           version information greater than this object.\r
411      *           Greater than 0 if the argument is a VersionInfo object that\r
412      *           has version information less than this object.\r
413      * @stable ICU 2.6\r
414      */\r
415     public int compareTo(VersionInfo other)\r
416     {\r
417         return m_version_ - other.m_version_;\r
418     }\r
419 \r
420     // private data members ----------------------------------------------\r
421 \r
422     /**\r
423      * Version number stored as a byte for each of the major, minor, milli and\r
424      * micro numbers in the 32 bit int.\r
425      * Most significant for the major and the least significant contains the\r
426      * micro numbers.\r
427      */\r
428     private int m_version_;\r
429     /**\r
430      * Map of singletons\r
431      */\r
432     private static final HashMap<Integer, VersionInfo> MAP_ = new HashMap<Integer, VersionInfo>();\r
433     /**\r
434      * Last byte mask\r
435      */\r
436     private static final int LAST_BYTE_MASK_ = 0xFF;\r
437     /**\r
438      * Error statement string\r
439      */\r
440     private static final String INVALID_VERSION_NUMBER_ =\r
441         "Invalid version number: Version number may be negative or greater than 255";\r
442 \r
443     // static declaration ------------------------------------------------\r
444 \r
445     /**\r
446      * Initialize versions only after MAP_ has been created\r
447      */\r
448     static {\r
449         UNICODE_1_0   = getInstance(1, 0, 0, 0);\r
450         UNICODE_1_0_1 = getInstance(1, 0, 1, 0);\r
451         UNICODE_1_1_0 = getInstance(1, 1, 0, 0);\r
452         UNICODE_1_1_5 = getInstance(1, 1, 5, 0);\r
453         UNICODE_2_0   = getInstance(2, 0, 0, 0);\r
454         UNICODE_2_1_2 = getInstance(2, 1, 2, 0);\r
455         UNICODE_2_1_5 = getInstance(2, 1, 5, 0);\r
456         UNICODE_2_1_8 = getInstance(2, 1, 8, 0);\r
457         UNICODE_2_1_9 = getInstance(2, 1, 9, 0);\r
458         UNICODE_3_0   = getInstance(3, 0, 0, 0);\r
459         UNICODE_3_0_1 = getInstance(3, 0, 1, 0);\r
460         UNICODE_3_1_0 = getInstance(3, 1, 0, 0);\r
461         UNICODE_3_1_1 = getInstance(3, 1, 1, 0);\r
462         UNICODE_3_2   = getInstance(3, 2, 0, 0);\r
463         UNICODE_4_0   = getInstance(4, 0, 0, 0);\r
464         UNICODE_4_0_1 = getInstance(4, 0, 1, 0);\r
465         UNICODE_4_1   = getInstance(4, 1, 0, 0);\r
466         UNICODE_5_0   = getInstance(5, 0, 0, 0);\r
467         UNICODE_5_1   = getInstance(5, 1, 0, 0);\r
468         UNICODE_5_2   = getInstance(5, 2, 0, 0);\r
469         ICU_VERSION   = getInstance(4, 4, 1, 1);\r
470         ICU_DATA_VERSION = getInstance(4, 4, 0, 1);\r
471         UCOL_RUNTIME_VERSION = getInstance(6);\r
472         UCOL_BUILDER_VERSION = getInstance(7);\r
473         UCOL_TAILORINGS_VERSION = getInstance(1);\r
474     }\r
475 \r
476     // private constructor -----------------------------------------------\r
477 \r
478     /**\r
479      * Constructor with int\r
480      * @param compactversion a 32 bit int with each byte representing a number\r
481      */\r
482     private VersionInfo(int compactversion)\r
483     {\r
484         m_version_ = compactversion;\r
485     }\r
486 \r
487     /**\r
488      * Gets the int from the version numbers\r
489      * @param major non-negative version number\r
490      * @param minor non-negativeversion number\r
491      * @param milli non-negativeversion number\r
492      * @param micro non-negativeversion number\r
493      */\r
494     private static int getInt(int major, int minor, int milli, int micro)\r
495     {\r
496         return (major << 24) | (minor << 16) | (milli << 8) | micro;\r
497     }\r
498 \r
499 }\r