2 *******************************************************************************
\r
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
8 package com.ibm.icu.util;
\r
10 import java.util.HashMap;
\r
13 * Class to store version numbers of the form major.minor.milli.micro.
\r
17 public final class VersionInfo implements Comparable<VersionInfo>
\r
19 // public data members -------------------------------------------------
\r
22 * Unicode 1.0 version
\r
25 public static final VersionInfo UNICODE_1_0;
\r
27 * Unicode 1.0.1 version
\r
30 public static final VersionInfo UNICODE_1_0_1;
\r
32 * Unicode 1.1.0 version
\r
35 public static final VersionInfo UNICODE_1_1_0;
\r
37 * Unicode 1.1.5 version
\r
40 public static final VersionInfo UNICODE_1_1_5;
\r
42 * Unicode 2.0 version
\r
45 public static final VersionInfo UNICODE_2_0;
\r
47 * Unicode 2.1.2 version
\r
50 public static final VersionInfo UNICODE_2_1_2;
\r
52 * Unicode 2.1.5 version
\r
55 public static final VersionInfo UNICODE_2_1_5;
\r
57 * Unicode 2.1.8 version
\r
60 public static final VersionInfo UNICODE_2_1_8;
\r
62 * Unicode 2.1.9 version
\r
65 public static final VersionInfo UNICODE_2_1_9;
\r
67 * Unicode 3.0 version
\r
70 public static final VersionInfo UNICODE_3_0;
\r
72 * Unicode 3.0.1 version
\r
75 public static final VersionInfo UNICODE_3_0_1;
\r
77 * Unicode 3.1.0 version
\r
80 public static final VersionInfo UNICODE_3_1_0;
\r
82 * Unicode 3.1.1 version
\r
85 public static final VersionInfo UNICODE_3_1_1;
\r
87 * Unicode 3.2 version
\r
90 public static final VersionInfo UNICODE_3_2;
\r
93 * Unicode 4.0 version
\r
96 public static final VersionInfo UNICODE_4_0;
\r
99 * Unicode 4.0.1 version
\r
102 public static final VersionInfo UNICODE_4_0_1;
\r
105 * Unicode 4.1 version
\r
108 public static final VersionInfo UNICODE_4_1;
\r
111 * Unicode 5.0 version
\r
114 public static final VersionInfo UNICODE_5_0;
\r
117 * Unicode 5.1 version
\r
120 public static final VersionInfo UNICODE_5_1;
\r
123 * Unicode 5.2 version
\r
126 public static final VersionInfo UNICODE_5_2;
\r
129 * ICU4J current release version
\r
132 public static final VersionInfo ICU_VERSION;
\r
135 * Data version string for ICU's internal data.
\r
136 * Used for appending to data path (e.g. icudt43b)
\r
138 * @deprecated This API is ICU internal only.
\r
140 public static final String ICU_DATA_VERSION_PATH = "44b";
\r
143 * Data version in ICU4J.
\r
145 * @deprecated This API is ICU internal only.
\r
147 public static final VersionInfo ICU_DATA_VERSION;
\r
150 * ICU4J collator runtime version
\r
153 public static final VersionInfo UCOL_RUNTIME_VERSION;
\r
156 * ICU4J collator builder version
\r
159 public static final VersionInfo UCOL_BUILDER_VERSION;
\r
162 * ICU4J collator tailorings version
\r
165 public static final VersionInfo UCOL_TAILORINGS_VERSION;
\r
168 // public methods ------------------------------------------------------
\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
183 public static VersionInfo getInstance(String version)
\r
185 int length = version.length();
\r
186 int array[] = {0, 0, 0, 0};
\r
190 while (count < 4 && index < length) {
\r
191 char c = version.charAt(index);
\r
197 if (c < 0 || c > 9) {
\r
198 throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
\r
200 array[count] *= 10;
\r
205 if (index != length) {
\r
206 throw new IllegalArgumentException(
\r
207 "Invalid version number: String '" + version + "' exceeds version format");
\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
215 return getInstance(array[0], array[1], array[2], array[3]);
\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
227 public static VersionInfo getInstance(int major, int minor, int milli,
\r
230 // checks if it is in the hashmap
\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
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
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
256 public static VersionInfo getInstance(int major, int minor, int milli)
\r
258 return getInstance(major, minor, milli, 0);
\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
270 public static VersionInfo getInstance(int major, int minor)
\r
272 return getInstance(major, minor, 0, 0);
\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
283 public static VersionInfo getInstance(int major)
\r
285 return getInstance(major, 0, 0, 0);
\r
288 private static VersionInfo javaVersion;
\r
292 * @deprecated This API is ICU internal only.
\r
294 public static VersionInfo javaVersion() {
\r
295 if (javaVersion == null) {
\r
296 String s = System.getProperty("java.version");
\r
298 // preserve only digits, separated by single '.'
\r
299 // ignore over 4 digit sequences
\r
300 // does not test < 255, very odd...
\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
310 // only four digit strings allowed
\r
322 while (w > 0 && chars[w-1] == '.') {
\r
326 String vs = new String(chars, 0, w);
\r
328 javaVersion = VersionInfo.getInstance(vs);
\r
330 return javaVersion;
\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
339 public String toString()
\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
353 * Returns the major version number
\r
354 * @return the major version number
\r
357 public int getMajor()
\r
359 return (m_version_ >> 24) & LAST_BYTE_MASK_ ;
\r
363 * Returns the minor version number
\r
364 * @return the minor version number
\r
367 public int getMinor()
\r
369 return (m_version_ >> 16) & LAST_BYTE_MASK_ ;
\r
373 * Returns the milli version number
\r
374 * @return the milli version number
\r
377 public int getMilli()
\r
379 return (m_version_ >> 8) & LAST_BYTE_MASK_ ;
\r
383 * Returns the micro version number
\r
384 * @return the micro version number
\r
387 public int getMicro()
\r
389 return m_version_ & LAST_BYTE_MASK_ ;
\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
399 public boolean equals(Object other)
\r
401 return other == this;
\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
415 public int compareTo(VersionInfo other)
\r
417 return m_version_ - other.m_version_;
\r
420 // private data members ----------------------------------------------
\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
428 private int m_version_;
\r
430 * Map of singletons
\r
432 private static final HashMap<Integer, VersionInfo> MAP_ = new HashMap<Integer, VersionInfo>();
\r
436 private static final int LAST_BYTE_MASK_ = 0xFF;
\r
438 * Error statement string
\r
440 private static final String INVALID_VERSION_NUMBER_ =
\r
441 "Invalid version number: Version number may be negative or greater than 255";
\r
443 // static declaration ------------------------------------------------
\r
446 * Initialize versions only after MAP_ has been created
\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
476 // private constructor -----------------------------------------------
\r
479 * Constructor with int
\r
480 * @param compactversion a 32 bit int with each byte representing a number
\r
482 private VersionInfo(int compactversion)
\r
484 m_version_ = compactversion;
\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
494 private static int getInt(int major, int minor, int milli, int micro)
\r
496 return (major << 24) | (minor << 16) | (milli << 8) | micro;
\r