0N/A/*
2362N/A * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A *******************************************************************************
1091N/A * (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved *
0N/A * *
0N/A * The original version of this source code and documentation is copyrighted *
0N/A * and owned by IBM, These materials are provided under terms of a License *
0N/A * Agreement between IBM and Sun. This technology is protected by multiple *
0N/A * US and International patents. This notice and attribution to IBM may not *
0N/A * to removed. *
0N/A *******************************************************************************
0N/A */
0N/A
0N/Apackage sun.text.normalizer;
0N/A
0N/Aimport java.util.HashMap;
0N/A
0N/A/**
0N/A * Class to store version numbers of the form major.minor.milli.micro.
0N/A * @author synwee
0N/A * @stable ICU 2.6
0N/A */
0N/Apublic final class VersionInfo
0N/A{
0N/A
0N/A // public methods ------------------------------------------------------
0N/A
0N/A /**
0N/A * Returns an instance of VersionInfo with the argument version.
0N/A * @param version version String in the format of "major.minor.milli.micro"
0N/A * or "major.minor.milli" or "major.minor" or "major",
0N/A * where major, minor, milli, micro are non-negative numbers
0N/A * <= 255. If the trailing version numbers are
0N/A * not specified they are taken as 0s. E.g. Version "3.1" is
0N/A * equivalent to "3.1.0.0".
0N/A * @return an instance of VersionInfo with the argument version.
0N/A * @exception throws an IllegalArgumentException when the argument version
0N/A * is not in the right format
0N/A * @stable ICU 2.6
0N/A */
0N/A public static VersionInfo getInstance(String version)
0N/A {
0N/A int length = version.length();
0N/A int array[] = {0, 0, 0, 0};
0N/A int count = 0;
0N/A int index = 0;
0N/A
0N/A while (count < 4 && index < length) {
0N/A char c = version.charAt(index);
0N/A if (c == '.') {
0N/A count ++;
0N/A }
0N/A else {
0N/A c -= '0';
0N/A if (c < 0 || c > 9) {
0N/A throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
0N/A }
0N/A array[count] *= 10;
0N/A array[count] += c;
0N/A }
0N/A index ++;
0N/A }
0N/A if (index != length) {
0N/A throw new IllegalArgumentException(
0N/A "Invalid version number: String '" + version + "' exceeds version format");
0N/A }
0N/A for (int i = 0; i < 4; i ++) {
0N/A if (array[i] < 0 || array[i] > 255) {
0N/A throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
0N/A }
0N/A }
0N/A
0N/A return getInstance(array[0], array[1], array[2], array[3]);
0N/A }
0N/A
0N/A /**
0N/A * Returns an instance of VersionInfo with the argument version.
0N/A * @param major major version, non-negative number <= 255.
0N/A * @param minor minor version, non-negative number <= 255.
0N/A * @param milli milli version, non-negative number <= 255.
0N/A * @param micro micro version, non-negative number <= 255.
0N/A * @exception throws an IllegalArgumentException when either arguments are
0N/A * negative or > 255
0N/A * @stable ICU 2.6
0N/A */
0N/A public static VersionInfo getInstance(int major, int minor, int milli,
0N/A int micro)
0N/A {
0N/A // checks if it is in the hashmap
0N/A // else
0N/A if (major < 0 || major > 255 || minor < 0 || minor > 255 ||
0N/A milli < 0 || milli > 255 || micro < 0 || micro > 255) {
0N/A throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
0N/A }
0N/A int version = getInt(major, minor, milli, micro);
215N/A Integer key = Integer.valueOf(version);
0N/A Object result = MAP_.get(key);
0N/A if (result == null) {
0N/A result = new VersionInfo(version);
0N/A MAP_.put(key, result);
0N/A }
0N/A return (VersionInfo)result;
0N/A }
0N/A
0N/A /**
0N/A * Compares other with this VersionInfo.
0N/A * @param other VersionInfo to be compared
0N/A * @return 0 if the argument is a VersionInfo object that has version
0N/A * information equals to this object.
0N/A * Less than 0 if the argument is a VersionInfo object that has
0N/A * version information greater than this object.
0N/A * Greater than 0 if the argument is a VersionInfo object that
0N/A * has version information less than this object.
0N/A * @stable ICU 2.6
0N/A */
0N/A public int compareTo(VersionInfo other)
0N/A {
0N/A return m_version_ - other.m_version_;
0N/A }
0N/A
0N/A // private data members ----------------------------------------------
0N/A
0N/A /**
0N/A * Version number stored as a byte for each of the major, minor, milli and
0N/A * micro numbers in the 32 bit int.
0N/A * Most significant for the major and the least significant contains the
0N/A * micro numbers.
0N/A */
0N/A private int m_version_;
0N/A /**
0N/A * Map of singletons
0N/A */
0N/A private static final HashMap MAP_ = new HashMap();
0N/A /**
0N/A * Error statement string
0N/A */
0N/A private static final String INVALID_VERSION_NUMBER_ =
0N/A "Invalid version number: Version number may be negative or greater than 255";
0N/A
0N/A // private constructor -----------------------------------------------
0N/A
0N/A /**
0N/A * Constructor with int
0N/A * @param compactversion a 32 bit int with each byte representing a number
0N/A */
0N/A private VersionInfo(int compactversion)
0N/A {
0N/A m_version_ = compactversion;
0N/A }
0N/A
0N/A /**
0N/A * Gets the int from the version numbers
0N/A * @param major non-negative version number
0N/A * @param minor non-negativeversion number
0N/A * @param milli non-negativeversion number
0N/A * @param micro non-negativeversion number
0N/A */
0N/A private static int getInt(int major, int minor, int milli, int micro)
0N/A {
0N/A return (major << 24) | (minor << 16) | (milli << 8) | micro;
0N/A }
0N/A}