Path: blob/master/src/java.base/share/classes/jdk/internal/icu/util/VersionInfo.java
41161 views
/*1* Copyright (c) 2005, 2020, 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 jdk.internal.icu.util;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{47// public data members -------------------------------------------------4849/**50* Data version string for ICU's internal data.51* Used for appending to data path (e.g. icudt43b)52* @internal53* @deprecated This API is ICU internal only.54*/55@Deprecated56public static final String ICU_DATA_VERSION_PATH = "67b";5758// public methods ------------------------------------------------------5960/**61* Returns an instance of VersionInfo with the argument version.62* @param version version String in the format of "major.minor.milli.micro"63* or "major.minor.milli" or "major.minor" or "major",64* where major, minor, milli, micro are non-negative numbers65* {@literal <=} 255. If the trailing version numbers are66* not specified they are taken as 0s. E.g. Version "3.1" is67* equivalent to "3.1.0.0".68* @return an instance of VersionInfo with the argument version.69* @exception throws an IllegalArgumentException when the argument version70* is not in the right format71* @stable ICU 2.672*/73public static VersionInfo getInstance(String version)74{75int length = version.length();76int array[] = {0, 0, 0, 0};77int count = 0;78int index = 0;7980while (count < 4 && index < length) {81char c = version.charAt(index);82if (c == '.') {83count ++;84}85else {86c -= '0';87if (c < 0 || c > 9) {88throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);89}90array[count] *= 10;91array[count] += c;92}93index ++;94}95if (index != length) {96throw new IllegalArgumentException(97"Invalid version number: String '" + version + "' exceeds version format");98}99for (int i = 0; i < 4; i ++) {100if (array[i] < 0 || array[i] > 255) {101throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);102}103}104105return getInstance(array[0], array[1], array[2], array[3]);106}107108/**109* Returns an instance of VersionInfo with the argument version.110* @param major major version, non-negative number {@literal <=} 255.111* @param minor minor version, non-negative number {@literal <=} 255.112* @param milli milli version, non-negative number {@literal <=} 255.113* @param micro micro version, non-negative number {@literal <=} 255.114* @exception throws an IllegalArgumentException when either arguments are115* negative or {@literal >} 255116* @stable ICU 2.6117*/118public static VersionInfo getInstance(int major, int minor, int milli,119int micro)120{121// checks if it is in the hashmap122// else123if (major < 0 || major > 255 || minor < 0 || minor > 255 ||124milli < 0 || milli > 255 || micro < 0 || micro > 255) {125throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);126}127int version = getInt(major, minor, milli, micro);128Integer key = Integer.valueOf(version);129Object result = MAP_.get(key);130if (result == null) {131result = new VersionInfo(version);132MAP_.put(key, result);133}134return (VersionInfo)result;135}136137/**138* Compares other with this VersionInfo.139* @param other VersionInfo to be compared140* @return 0 if the argument is a VersionInfo object that has version141* information equal to this object.142* Less than 0 if the argument is a VersionInfo object that has143* version information greater than this object.144* Greater than 0 if the argument is a VersionInfo object that145* has version information less than this object.146* @stable ICU 2.6147*/148public int compareTo(VersionInfo other)149{150return m_version_ - other.m_version_;151}152153// private data members ----------------------------------------------154155/**156* Version number stored as a byte for each of the major, minor, milli and157* micro numbers in the 32 bit int.158* Most significant for the major and the least significant contains the159* micro numbers.160*/161private int m_version_;162/**163* Map of singletons164*/165private static final HashMap<Integer, Object> MAP_ = new HashMap<>();166/**167* Error statement string168*/169private static final String INVALID_VERSION_NUMBER_ =170"Invalid version number: Version number may be negative or greater than 255";171172// private constructor -----------------------------------------------173174/**175* Constructor with int176* @param compactversion a 32 bit int with each byte representing a number177*/178private VersionInfo(int compactversion)179{180m_version_ = compactversion;181}182183/**184* Gets the int from the version numbers185* @param major non-negative version number186* @param minor non-negativeversion number187* @param milli non-negativeversion number188* @param micro non-negativeversion number189*/190private static int getInt(int major, int minor, int milli, int micro)191{192return (major << 24) | (minor << 16) | (milli << 8) | micro;193}194}195196197