Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/text/normalizer/VersionInfo.java
38830 views
/*1* Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25*******************************************************************************26* (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved *27* *28* The original version of this source code and documentation is copyrighted *29* and owned by IBM, These materials are provided under terms of a License *30* Agreement between IBM and Sun. This technology is protected by multiple *31* US and International patents. This notice and attribution to IBM may not *32* to removed. *33*******************************************************************************34*/3536package sun.text.normalizer;3738import java.util.HashMap;3940/**41* Class to store version numbers of the form major.minor.milli.micro.42* @author synwee43* @stable ICU 2.644*/45public final class VersionInfo46{4748// public methods ------------------------------------------------------4950/**51* Returns an instance of VersionInfo with the argument version.52* @param version version String in the format of "major.minor.milli.micro"53* or "major.minor.milli" or "major.minor" or "major",54* where major, minor, milli, micro are non-negative numbers55* <= 255. If the trailing version numbers are56* not specified they are taken as 0s. E.g. Version "3.1" is57* equivalent to "3.1.0.0".58* @return an instance of VersionInfo with the argument version.59* @exception throws an IllegalArgumentException when the argument version60* is not in the right format61* @stable ICU 2.662*/63public static VersionInfo getInstance(String version)64{65int length = version.length();66int array[] = {0, 0, 0, 0};67int count = 0;68int index = 0;6970while (count < 4 && index < length) {71char c = version.charAt(index);72if (c == '.') {73count ++;74}75else {76c -= '0';77if (c < 0 || c > 9) {78throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);79}80array[count] *= 10;81array[count] += c;82}83index ++;84}85if (index != length) {86throw new IllegalArgumentException(87"Invalid version number: String '" + version + "' exceeds version format");88}89for (int i = 0; i < 4; i ++) {90if (array[i] < 0 || array[i] > 255) {91throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);92}93}9495return getInstance(array[0], array[1], array[2], array[3]);96}9798/**99* Returns an instance of VersionInfo with the argument version.100* @param major major version, non-negative number <= 255.101* @param minor minor version, non-negative number <= 255.102* @param milli milli version, non-negative number <= 255.103* @param micro micro version, non-negative number <= 255.104* @exception throws an IllegalArgumentException when either arguments are105* negative or > 255106* @stable ICU 2.6107*/108public static VersionInfo getInstance(int major, int minor, int milli,109int micro)110{111// checks if it is in the hashmap112// else113if (major < 0 || major > 255 || minor < 0 || minor > 255 ||114milli < 0 || milli > 255 || micro < 0 || micro > 255) {115throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);116}117int version = getInt(major, minor, milli, micro);118Integer key = Integer.valueOf(version);119Object result = MAP_.get(key);120if (result == null) {121result = new VersionInfo(version);122MAP_.put(key, result);123}124return (VersionInfo)result;125}126127/**128* Compares other with this VersionInfo.129* @param other VersionInfo to be compared130* @return 0 if the argument is a VersionInfo object that has version131* information equals to this object.132* Less than 0 if the argument is a VersionInfo object that has133* version information greater than this object.134* Greater than 0 if the argument is a VersionInfo object that135* has version information less than this object.136* @stable ICU 2.6137*/138public int compareTo(VersionInfo other)139{140return m_version_ - other.m_version_;141}142143// private data members ----------------------------------------------144145/**146* Version number stored as a byte for each of the major, minor, milli and147* micro numbers in the 32 bit int.148* Most significant for the major and the least significant contains the149* micro numbers.150*/151private int m_version_;152/**153* Map of singletons154*/155private static final HashMap<Integer, Object> MAP_ = new HashMap<>();156/**157* Error statement string158*/159private static final String INVALID_VERSION_NUMBER_ =160"Invalid version number: Version number may be negative or greater than 255";161162// private constructor -----------------------------------------------163164/**165* Constructor with int166* @param compactversion a 32 bit int with each byte representing a number167*/168private VersionInfo(int compactversion)169{170m_version_ = compactversion;171}172173/**174* Gets the int from the version numbers175* @param major non-negative version number176* @param minor non-negativeversion number177* @param milli non-negativeversion number178* @param micro non-negativeversion number179*/180private static int getInt(int major, int minor, int milli, int micro)181{182return (major << 24) | (minor << 16) | (milli << 8) | micro;183}184}185186187