< prev index next >

src/java.base/share/classes/sun/text/normalizer/VersionInfo.java

Print this page




  36 
  37 package sun.text.normalizer;
  38 
  39 import java.util.HashMap;
  40 
  41 /**
  42  * Class to store version numbers of the form major.minor.milli.micro.
  43  * @author synwee
  44  * @stable ICU 2.6
  45  */
  46 public final class VersionInfo
  47 {
  48 
  49     // public methods ------------------------------------------------------
  50 
  51     /**
  52      * Returns an instance of VersionInfo with the argument version.
  53      * @param version version String in the format of "major.minor.milli.micro"
  54      *                or "major.minor.milli" or "major.minor" or "major",
  55      *                where major, minor, milli, micro are non-negative numbers
  56      *                <= 255. If the trailing version numbers are
  57      *                not specified they are taken as 0s. E.g. Version "3.1" is
  58      *                equivalent to "3.1.0.0".
  59      * @return an instance of VersionInfo with the argument version.
  60      * @exception throws an IllegalArgumentException when the argument version
  61      *                is not in the right format
  62      * @stable ICU 2.6
  63      */
  64     public static VersionInfo getInstance(String version)
  65     {
  66         int length  = version.length();
  67         int array[] = {0, 0, 0, 0};
  68         int count   = 0;
  69         int index   = 0;
  70 
  71         while (count < 4 && index < length) {
  72             char c = version.charAt(index);
  73             if (c == '.') {
  74                 count ++;
  75             }
  76             else {


  81                 array[count] *= 10;
  82                 array[count] += c;
  83             }
  84             index ++;
  85         }
  86         if (index != length) {
  87             throw new IllegalArgumentException(
  88                                                "Invalid version number: String '" + version + "' exceeds version format");
  89         }
  90         for (int i = 0; i < 4; i ++) {
  91             if (array[i] < 0 || array[i] > 255) {
  92                 throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
  93             }
  94         }
  95 
  96         return getInstance(array[0], array[1], array[2], array[3]);
  97     }
  98 
  99     /**
 100      * Returns an instance of VersionInfo with the argument version.
 101      * @param major major version, non-negative number <= 255.
 102      * @param minor minor version, non-negative number <= 255.
 103      * @param milli milli version, non-negative number <= 255.
 104      * @param micro micro version, non-negative number <= 255.
 105      * @exception throws an IllegalArgumentException when either arguments are
 106      *                                     negative or > 255
 107      * @stable ICU 2.6
 108      */
 109     public static VersionInfo getInstance(int major, int minor, int milli,
 110                                           int micro)
 111     {
 112         // checks if it is in the hashmap
 113         // else
 114         if (major < 0 || major > 255 || minor < 0 || minor > 255 ||
 115             milli < 0 || milli > 255 || micro < 0 || micro > 255) {
 116             throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
 117         }
 118         int     version = getInt(major, minor, milli, micro);
 119         Integer key     = Integer.valueOf(version);
 120         Object  result  = MAP_.get(key);
 121         if (result == null) {
 122             result = new VersionInfo(version);
 123             MAP_.put(key, result);
 124         }
 125         return (VersionInfo)result;
 126     }




  36 
  37 package sun.text.normalizer;
  38 
  39 import java.util.HashMap;
  40 
  41 /**
  42  * Class to store version numbers of the form major.minor.milli.micro.
  43  * @author synwee
  44  * @stable ICU 2.6
  45  */
  46 public final class VersionInfo
  47 {
  48 
  49     // public methods ------------------------------------------------------
  50 
  51     /**
  52      * Returns an instance of VersionInfo with the argument version.
  53      * @param version version String in the format of "major.minor.milli.micro"
  54      *                or "major.minor.milli" or "major.minor" or "major",
  55      *                where major, minor, milli, micro are non-negative numbers
  56      *                {@literal <=} 255. If the trailing version numbers are
  57      *                not specified they are taken as 0s. E.g. Version "3.1" is
  58      *                equivalent to "3.1.0.0".
  59      * @return an instance of VersionInfo with the argument version.
  60      * @exception throws an IllegalArgumentException when the argument version
  61      *                is not in the right format
  62      * @stable ICU 2.6
  63      */
  64     public static VersionInfo getInstance(String version)
  65     {
  66         int length  = version.length();
  67         int array[] = {0, 0, 0, 0};
  68         int count   = 0;
  69         int index   = 0;
  70 
  71         while (count < 4 && index < length) {
  72             char c = version.charAt(index);
  73             if (c == '.') {
  74                 count ++;
  75             }
  76             else {


  81                 array[count] *= 10;
  82                 array[count] += c;
  83             }
  84             index ++;
  85         }
  86         if (index != length) {
  87             throw new IllegalArgumentException(
  88                                                "Invalid version number: String '" + version + "' exceeds version format");
  89         }
  90         for (int i = 0; i < 4; i ++) {
  91             if (array[i] < 0 || array[i] > 255) {
  92                 throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
  93             }
  94         }
  95 
  96         return getInstance(array[0], array[1], array[2], array[3]);
  97     }
  98 
  99     /**
 100      * Returns an instance of VersionInfo with the argument version.
 101      * @param major major version, non-negative number {@literal <=} 255.
 102      * @param minor minor version, non-negative number {@literal <=} 255.
 103      * @param milli milli version, non-negative number {@literal <=} 255.
 104      * @param micro micro version, non-negative number {@literal <=} 255.
 105      * @exception throws an IllegalArgumentException when either arguments are
 106      *                                     negative or {@literal >} 255
 107      * @stable ICU 2.6
 108      */
 109     public static VersionInfo getInstance(int major, int minor, int milli,
 110                                           int micro)
 111     {
 112         // checks if it is in the hashmap
 113         // else
 114         if (major < 0 || major > 255 || minor < 0 || minor > 255 ||
 115             milli < 0 || milli > 255 || micro < 0 || micro > 255) {
 116             throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
 117         }
 118         int     version = getInt(major, minor, milli, micro);
 119         Integer key     = Integer.valueOf(version);
 120         Object  result  = MAP_.get(key);
 121         if (result == null) {
 122             result = new VersionInfo(version);
 123             MAP_.put(key, result);
 124         }
 125         return (VersionInfo)result;
 126     }


< prev index next >