• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 1996-2016, International Business Machines Corporation and
7  * others. All Rights Reserved.
8  *******************************************************************************
9  */
10 
11 package ohos.global.icu.util;
12 
13 import java.util.concurrent.ConcurrentHashMap;
14 
15 /**
16  * Class to store version numbers of the form major.minor.milli.micro.
17  * @author synwee
18  */
19 public final class VersionInfo implements Comparable<VersionInfo>
20 {
21     // public data members -------------------------------------------------
22 
23     /**
24      * Unicode 1.0 version
25      */
26     public static final VersionInfo UNICODE_1_0;
27     /**
28      * Unicode 1.0.1 version
29      */
30     public static final VersionInfo UNICODE_1_0_1;
31     /**
32      * Unicode 1.1.0 version
33      */
34     public static final VersionInfo UNICODE_1_1_0;
35     /**
36      * Unicode 1.1.5 version
37      */
38     public static final VersionInfo UNICODE_1_1_5;
39     /**
40      * Unicode 2.0 version
41      */
42     public static final VersionInfo UNICODE_2_0;
43     /**
44      * Unicode 2.1.2 version
45      */
46     public static final VersionInfo UNICODE_2_1_2;
47     /**
48      * Unicode 2.1.5 version
49      */
50     public static final VersionInfo UNICODE_2_1_5;
51     /**
52      * Unicode 2.1.8 version
53      */
54     public static final VersionInfo UNICODE_2_1_8;
55     /**
56      * Unicode 2.1.9 version
57      */
58     public static final VersionInfo UNICODE_2_1_9;
59     /**
60      * Unicode 3.0 version
61      */
62     public static final VersionInfo UNICODE_3_0;
63     /**
64      * Unicode 3.0.1 version
65      */
66     public static final VersionInfo UNICODE_3_0_1;
67     /**
68      * Unicode 3.1.0 version
69      */
70     public static final VersionInfo UNICODE_3_1_0;
71     /**
72      * Unicode 3.1.1 version
73      */
74     public static final VersionInfo UNICODE_3_1_1;
75     /**
76      * Unicode 3.2 version
77      */
78     public static final VersionInfo UNICODE_3_2;
79 
80     /**
81      * Unicode 4.0 version
82      */
83     public static final VersionInfo UNICODE_4_0;
84 
85     /**
86      * Unicode 4.0.1 version
87      */
88     public static final VersionInfo UNICODE_4_0_1;
89 
90     /**
91      * Unicode 4.1 version
92      */
93     public static final VersionInfo UNICODE_4_1;
94 
95     /**
96      * Unicode 5.0 version
97      */
98     public static final VersionInfo UNICODE_5_0;
99 
100     /**
101      * Unicode 5.1 version
102      */
103     public static final VersionInfo UNICODE_5_1;
104 
105     /**
106      * Unicode 5.2 version
107      */
108     public static final VersionInfo UNICODE_5_2;
109 
110     /**
111      * Unicode 6.0 version
112      */
113     public static final VersionInfo UNICODE_6_0;
114 
115     /**
116      * Unicode 6.1 version
117      */
118     public static final VersionInfo UNICODE_6_1;
119 
120     /**
121      * Unicode 6.2 version
122      */
123     public static final VersionInfo UNICODE_6_2;
124 
125     /**
126      * Unicode 6.3 version
127      */
128     public static final VersionInfo UNICODE_6_3;
129 
130     /**
131      * Unicode 7.0 version
132      */
133     public static final VersionInfo UNICODE_7_0;
134 
135     /**
136      * Unicode 8.0 version
137      */
138     public static final VersionInfo UNICODE_8_0;
139 
140     /**
141      * Unicode 9.0 version
142      */
143     public static final VersionInfo UNICODE_9_0;
144 
145     /**
146      * Unicode 10.0 version
147      */
148     public static final VersionInfo UNICODE_10_0;
149 
150     /**
151      * Unicode 11.0 version
152      */
153     public static final VersionInfo UNICODE_11_0;
154 
155     /**
156      * Unicode 12.0 version
157      */
158     public static final VersionInfo UNICODE_12_0;
159 
160     /**
161      * Unicode 12.1 version
162      */
163     public static final VersionInfo UNICODE_12_1;
164 
165     /**
166      * Unicode 13.0 version
167      */
168     public static final VersionInfo UNICODE_13_0;
169 
170     /**
171      * ICU4J current release version
172      */
173     public static final VersionInfo ICU_VERSION;
174 
175     /**
176      * Data version string for ICU's internal data.
177      * Used for appending to data path (e.g. icudt43b)
178      * @deprecated This API is ICU internal only.
179      * @hide deprecated on icu4j-org
180      * @hide draft / provisional / internal are hidden on OHOS
181      */
182     @Deprecated
183     public static final String ICU_DATA_VERSION_PATH = "67b";
184 
185     /**
186      * Data version in ICU4J.
187      * @deprecated This API is ICU internal only.
188      * @hide deprecated on icu4j-org
189      * @hide draft / provisional / internal are hidden on OHOS
190      */
191     @Deprecated
192     public static final VersionInfo ICU_DATA_VERSION;
193 
194     /**
195      * Collation runtime version (sort key generator, string comparisons).
196      * If the version is different, sort keys for the same string could be different.
197      * This value may change in subsequent releases of ICU.
198      */
199     public static final VersionInfo UCOL_RUNTIME_VERSION;
200 
201     /**
202      * Collation builder code version.
203      * When this is different, the same tailoring might result
204      * in assigning different collation elements to code points.
205      * This value may change in subsequent releases of ICU.
206      */
207     public static final VersionInfo UCOL_BUILDER_VERSION;
208 
209     /**
210      * Constant version 1.
211      * This was intended to be the version of collation tailorings,
212      * but instead the tailoring data carries a version number.
213      * @deprecated ICU 54
214      * @hide deprecated on icu4j-org
215      */
216     @Deprecated
217     public static final VersionInfo UCOL_TAILORINGS_VERSION;
218 
219 
220     // public methods ------------------------------------------------------
221 
222     /**
223      * Returns an instance of VersionInfo with the argument version.
224      * @param version version String in the format of "major.minor.milli.micro"
225      *                or "major.minor.milli" or "major.minor" or "major",
226      *                where major, minor, milli, micro are non-negative numbers
227      *                &lt;= 255. If the trailing version numbers are
228      *                not specified they are taken as 0s. E.g. Version "3.1" is
229      *                equivalent to "3.1.0.0".
230      * @return an instance of VersionInfo with the argument version.
231      * @exception IllegalArgumentException when the argument version
232      *                is not in the right format
233      */
getInstance(String version)234     public static VersionInfo getInstance(String version)
235     {
236         int length  = version.length();
237         int array[] = {0, 0, 0, 0};
238         int count   = 0;
239         int index   = 0;
240 
241         while (count < 4 && index < length) {
242             char c = version.charAt(index);
243             if (c == '.') {
244                 count ++;
245             }
246             else {
247                 c -= '0';
248                 if (c < 0 || c > 9) {
249                     throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
250                 }
251                 array[count] *= 10;
252                 array[count] += c;
253             }
254             index ++;
255         }
256         if (index != length) {
257             throw new IllegalArgumentException(
258                                                "Invalid version number: String '" + version + "' exceeds version format");
259         }
260         for (int i = 0; i < 4; i ++) {
261             if (array[i] < 0 || array[i] > 255) {
262                 throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
263             }
264         }
265 
266         return getInstance(array[0], array[1], array[2], array[3]);
267     }
268 
269     /**
270      * Returns an instance of VersionInfo with the argument version.
271      * @param major major version, non-negative number &lt;= 255.
272      * @param minor minor version, non-negative number &lt;= 255.
273      * @param milli milli version, non-negative number &lt;= 255.
274      * @param micro micro version, non-negative number &lt;= 255.
275      * @exception IllegalArgumentException when either arguments are negative or &gt; 255
276      */
getInstance(int major, int minor, int milli, int micro)277     public static VersionInfo getInstance(int major, int minor, int milli,
278                                           int micro)
279     {
280         // checks if it is in the hashmap
281         // else
282         if (major < 0 || major > 255 || minor < 0 || minor > 255 ||
283             milli < 0 || milli > 255 || micro < 0 || micro > 255) {
284             throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
285         }
286         int     version = getInt(major, minor, milli, micro);
287         Integer key     = Integer.valueOf(version);
288         VersionInfo  result  = MAP_.get(key);
289         if (result == null) {
290             result = new VersionInfo(version);
291             VersionInfo tmpvi = MAP_.putIfAbsent(key, result);
292             if (tmpvi != null) {
293                 result = tmpvi;
294             }
295         }
296         return result;
297     }
298 
299     /**
300      * Returns an instance of VersionInfo with the argument version.
301      * Equivalent to getInstance(major, minor, milli, 0).
302      * @param major major version, non-negative number &lt;= 255.
303      * @param minor minor version, non-negative number &lt;= 255.
304      * @param milli milli version, non-negative number &lt;= 255.
305      * @exception IllegalArgumentException when either arguments are
306      *                                     negative or &gt; 255
307      */
getInstance(int major, int minor, int milli)308     public static VersionInfo getInstance(int major, int minor, int milli)
309     {
310         return getInstance(major, minor, milli, 0);
311     }
312 
313     /**
314      * Returns an instance of VersionInfo with the argument version.
315      * Equivalent to getInstance(major, minor, 0, 0).
316      * @param major major version, non-negative number &lt;= 255.
317      * @param minor minor version, non-negative number &lt;= 255.
318      * @exception IllegalArgumentException when either arguments are
319      *                                     negative or &gt; 255
320      */
getInstance(int major, int minor)321     public static VersionInfo getInstance(int major, int minor)
322     {
323         return getInstance(major, minor, 0, 0);
324     }
325 
326     /**
327      * Returns an instance of VersionInfo with the argument version.
328      * Equivalent to getInstance(major, 0, 0, 0).
329      * @param major major version, non-negative number &lt;= 255.
330      * @exception IllegalArgumentException when either arguments are
331      *                                     negative or &gt; 255
332      */
getInstance(int major)333     public static VersionInfo getInstance(int major)
334     {
335         return getInstance(major, 0, 0, 0);
336     }
337 
338     private static volatile VersionInfo javaVersion;
339 
340     /**
341      * @deprecated This API is ICU internal only.
342      * @hide deprecated on icu4j-org
343      * @hide draft / provisional / internal are hidden on OHOS
344      */
345     @Deprecated
javaVersion()346     public static VersionInfo javaVersion() {
347         if (javaVersion == null) {
348             synchronized(VersionInfo.class) {
349                 if (javaVersion == null) {
350                     String s = System.getProperty("java.version");
351                     // clean string
352                     // preserve only digits, separated by single '.'
353                     // ignore over 4 digit sequences
354                     // does not test < 255, very odd...
355 
356                     char[] chars = s.toCharArray();
357                     int r = 0, w = 0, count = 0;
358                     boolean numeric = false; // ignore leading non-numerics
359                     while (r < chars.length) {
360                         char c = chars[r++];
361                         if (c < '0' || c > '9') {
362                             if (numeric) {
363                                 if (count == 3) {
364                                     // only four digit strings allowed
365                                     break;
366                                 }
367                                 numeric = false;
368                                 chars[w++] = '.';
369                                 ++count;
370                             }
371                         } else {
372                             numeric = true;
373                             chars[w++] = c;
374                         }
375                     }
376                     while (w > 0 && chars[w-1] == '.') {
377                         --w;
378                     }
379 
380                     String vs = new String(chars, 0, w);
381 
382                     javaVersion = VersionInfo.getInstance(vs);
383                 }
384             }
385         }
386         return javaVersion;
387     }
388 
389     /**
390      * Returns the String representative of VersionInfo in the format of
391      * "major.minor.milli.micro"
392      * @return String representative of VersionInfo
393      */
394     @Override
toString()395     public String toString()
396     {
397         StringBuilder result = new StringBuilder(7);
398         result.append(getMajor());
399         result.append('.');
400         result.append(getMinor());
401         result.append('.');
402         result.append(getMilli());
403         result.append('.');
404         result.append(getMicro());
405         return result.toString();
406     }
407 
408     /**
409      * Returns the major version number
410      * @return the major version number
411      */
getMajor()412     public int getMajor()
413     {
414         return (m_version_ >> 24) & LAST_BYTE_MASK_ ;
415     }
416 
417     /**
418      * Returns the minor version number
419      * @return the minor version number
420      */
getMinor()421     public int getMinor()
422     {
423         return (m_version_ >> 16) & LAST_BYTE_MASK_ ;
424     }
425 
426     /**
427      * Returns the milli version number
428      * @return the milli version number
429      */
getMilli()430     public int getMilli()
431     {
432         return (m_version_ >> 8) & LAST_BYTE_MASK_ ;
433     }
434 
435     /**
436      * Returns the micro version number
437      * @return the micro version number
438      */
getMicro()439     public int getMicro()
440     {
441         return m_version_ & LAST_BYTE_MASK_ ;
442     }
443 
444     /**
445      * Checks if this version information is equals to the argument version
446      * @param other object to be compared
447      * @return true if other is equals to this object's version information,
448      *         false otherwise
449      */
450     @Override
equals(Object other)451     public boolean equals(Object other)
452     {
453         return other == this;
454     }
455 
456     /**
457      * Returns the hash code value for this set.
458      *
459      * @return the hash code value for this set.
460      * @see java.lang.Object#hashCode()
461      */
462     @Override
hashCode()463     public int hashCode() {
464         return m_version_;
465     }
466 
467     /**
468      * Compares other with this VersionInfo.
469      * @param other VersionInfo to be compared
470      * @return 0 if the argument is a VersionInfo object that has version
471      *           information equals to this object.
472      *           Less than 0 if the argument is a VersionInfo object that has
473      *           version information greater than this object.
474      *           Greater than 0 if the argument is a VersionInfo object that
475      *           has version information less than this object.
476      */
477     @Override
compareTo(VersionInfo other)478     public int compareTo(VersionInfo other)
479     {
480         return m_version_ - other.m_version_;
481     }
482 
483     // private data members ----------------------------------------------
484 
485     /**
486      * Unicode data version used by the current release.
487      * Defined here privately for printing by the main() method in this class.
488      * Should be the same as {@link ohos.global.icu.lang.UCharacter#getUnicodeVersion()}
489      * which gets the version number from a data file.
490      * We do not want VersionInfo to have an import dependency on UCharacter.
491      */
492     private static final VersionInfo UNICODE_VERSION;
493 
494     /**
495      * Version number stored as a byte for each of the major, minor, milli and
496      * micro numbers in the 32 bit int.
497      * Most significant for the major and the least significant contains the
498      * micro numbers.
499      */
500     private int m_version_;
501     /**
502      * Map of singletons
503      */
504     private static final ConcurrentHashMap<Integer, VersionInfo> MAP_ = new ConcurrentHashMap<>();
505     /**
506      * Last byte mask
507      */
508     private static final int LAST_BYTE_MASK_ = 0xFF;
509     /**
510      * Error statement string
511      */
512     private static final String INVALID_VERSION_NUMBER_ =
513         "Invalid version number: Version number may be negative or greater than 255";
514 
515     // static declaration ------------------------------------------------
516 
517     /**
518      * Initialize versions only after MAP_ has been created
519      */
520     static {
521         UNICODE_1_0   = getInstance(1, 0, 0, 0);
522         UNICODE_1_0_1 = getInstance(1, 0, 1, 0);
523         UNICODE_1_1_0 = getInstance(1, 1, 0, 0);
524         UNICODE_1_1_5 = getInstance(1, 1, 5, 0);
525         UNICODE_2_0   = getInstance(2, 0, 0, 0);
526         UNICODE_2_1_2 = getInstance(2, 1, 2, 0);
527         UNICODE_2_1_5 = getInstance(2, 1, 5, 0);
528         UNICODE_2_1_8 = getInstance(2, 1, 8, 0);
529         UNICODE_2_1_9 = getInstance(2, 1, 9, 0);
530         UNICODE_3_0   = getInstance(3, 0, 0, 0);
531         UNICODE_3_0_1 = getInstance(3, 0, 1, 0);
532         UNICODE_3_1_0 = getInstance(3, 1, 0, 0);
533         UNICODE_3_1_1 = getInstance(3, 1, 1, 0);
534         UNICODE_3_2   = getInstance(3, 2, 0, 0);
535         UNICODE_4_0   = getInstance(4, 0, 0, 0);
536         UNICODE_4_0_1 = getInstance(4, 0, 1, 0);
537         UNICODE_4_1   = getInstance(4, 1, 0, 0);
538         UNICODE_5_0   = getInstance(5, 0, 0, 0);
539         UNICODE_5_1   = getInstance(5, 1, 0, 0);
540         UNICODE_5_2   = getInstance(5, 2, 0, 0);
541         UNICODE_6_0   = getInstance(6, 0, 0, 0);
542         UNICODE_6_1   = getInstance(6, 1, 0, 0);
543         UNICODE_6_2   = getInstance(6, 2, 0, 0);
544         UNICODE_6_3   = getInstance(6, 3, 0, 0);
545         UNICODE_7_0   = getInstance(7, 0, 0, 0);
546         UNICODE_8_0   = getInstance(8, 0, 0, 0);
547         UNICODE_9_0   = getInstance(9, 0, 0, 0);
548         UNICODE_10_0   = getInstance(10, 0, 0, 0);
549         UNICODE_11_0   = getInstance(11, 0, 0, 0);
550         UNICODE_12_0   = getInstance(12, 0, 0, 0);
551         UNICODE_12_1   = getInstance(12, 1, 0, 0);
552         UNICODE_13_0   = getInstance(13, 0, 0, 0);
553 
554         ICU_VERSION   = getInstance(67, 1, 0, 0);
555         ICU_DATA_VERSION = ICU_VERSION;
556         UNICODE_VERSION = UNICODE_13_0;
557 
558         UCOL_RUNTIME_VERSION = getInstance(9);
559         UCOL_BUILDER_VERSION = getInstance(9);
560         UCOL_TAILORINGS_VERSION = getInstance(1);
561     }
562 
563     // private constructor -----------------------------------------------
564 
565     /**
566      * Constructor with int
567      * @param compactversion a 32 bit int with each byte representing a number
568      */
VersionInfo(int compactversion)569     private VersionInfo(int compactversion)
570     {
571         m_version_ = compactversion;
572     }
573 
574     /**
575      * Gets the int from the version numbers
576      * @param major non-negative version number
577      * @param minor non-negative version number
578      * @param milli non-negative version number
579      * @param micro non-negative version number
580      */
getInt(int major, int minor, int milli, int micro)581     private static int getInt(int major, int minor, int milli, int micro)
582     {
583         return (major << 24) | (minor << 16) | (milli << 8) | micro;
584     }
585     ///CLOVER:OFF
586     /**
587      * Main method prints out ICU version information
588      * @param args arguments (currently not used)
589      * @hide unsupported on OHOS
590      */
main(String[] args)591     public static void main(String[] args) {
592         String icuApiVer;
593 
594         if (ICU_VERSION.getMajor() <= 4) {
595             if (ICU_VERSION.getMinor() % 2 != 0) {
596                 // Development mile stone
597                 int major = ICU_VERSION.getMajor();
598                 int minor = ICU_VERSION.getMinor() + 1;
599                 if (minor >= 10) {
600                     minor -= 10;
601                     major++;
602                 }
603                 icuApiVer = "" + major + "." + minor + "M" + ICU_VERSION.getMilli();
604             } else {
605                 icuApiVer = ICU_VERSION.getVersionString(2, 2);
606             }
607         } else {
608             if (ICU_VERSION.getMinor() == 0) {
609                 // Development mile stone
610                 icuApiVer = "" + ICU_VERSION.getMajor() + "M" + ICU_VERSION.getMilli();
611             } else {
612                 icuApiVer = ICU_VERSION.getVersionString(2, 2);
613             }
614         }
615 
616 
617         System.out.println("International Components for Unicode for Java " + icuApiVer);
618 
619         System.out.println("");
620         System.out.println("Implementation Version: " + ICU_VERSION.getVersionString(2, 4));
621         System.out.println("Unicode Data Version:   " + UNICODE_VERSION.getVersionString(2, 4));
622         System.out.println("CLDR Data Version:      " + LocaleData.getCLDRVersion().getVersionString(2, 4));
623         System.out.println("Time Zone Data Version: " + getTZDataVersion());
624     }
625 
626     /**
627      * Generate version string separated by dots with
628      * the specified digit width.  Version digit 0
629      * after <code>minDigits</code> will be trimmed off.
630      * @param minDigits Minimum number of version digits
631      * @param maxDigits Maximum number of version digits
632      * @return A tailored version string
633      * @deprecated This API is ICU internal only. (For use in CLDR, etc.)
634      * @hide deprecated on icu4j-org
635      * @hide draft / provisional / internal are hidden on OHOS
636      */
637     @Deprecated
getVersionString(int minDigits, int maxDigits)638     public String getVersionString(int minDigits, int maxDigits) {
639         if (minDigits < 1 || maxDigits < 1
640                 || minDigits > 4 || maxDigits > 4 || minDigits > maxDigits) {
641             throw new IllegalArgumentException("Invalid min/maxDigits range");
642         }
643 
644         int[] digits = new int[4];
645         digits[0] = getMajor();
646         digits[1] = getMinor();
647         digits[2] = getMilli();
648         digits[3] = getMicro();
649 
650         int numDigits = maxDigits;
651         while (numDigits > minDigits) {
652             if (digits[numDigits - 1] != 0) {
653                 break;
654             }
655             numDigits--;
656         }
657 
658         StringBuilder verStr = new StringBuilder(7);
659         verStr.append(digits[0]);
660         for (int i = 1; i < numDigits; i++) {
661             verStr.append(".");
662             verStr.append(digits[i]);
663         }
664 
665         return verStr.toString();
666     }
667     ///CLOVER:ON
668 
669 
670     // Moved from TimeZone class
671     private static volatile String TZDATA_VERSION = null;
672 
getTZDataVersion()673     static String getTZDataVersion() {
674         if (TZDATA_VERSION == null) {
675             synchronized (VersionInfo.class) {
676                 if (TZDATA_VERSION == null) {
677                     UResourceBundle tzbundle = UResourceBundle.getBundleInstance("ohos/global/icu/impl/data/icudt"
678                             + VersionInfo.ICU_DATA_VERSION_PATH, "zoneinfo64");
679                     TZDATA_VERSION = tzbundle.getString("TZVersion");
680                 }
681             }
682         }
683         return TZDATA_VERSION;
684     }
685 }
686