001: /*
002: * Portions Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: *******************************************************************************
027: * (C) Copyright IBM Corp. 1996-2005 - All Rights Reserved *
028: * *
029: * The original version of this source code and documentation is copyrighted *
030: * and owned by IBM, These materials are provided under terms of a License *
031: * Agreement between IBM and Sun. This technology is protected by multiple *
032: * US and International patents. This notice and attribution to IBM may not *
033: * to removed. *
034: *******************************************************************************
035: */
036:
037: package sun.text.normalizer;
038:
039: import java.util.HashMap;
040:
041: /**
042: * Class to store version numbers of the form major.minor.milli.micro.
043: * @author synwee
044: * @stable ICU 2.6
045: */
046: public final class VersionInfo {
047:
048: // public methods ------------------------------------------------------
049:
050: /**
051: * Returns an instance of VersionInfo with the argument version.
052: * @param version version String in the format of "major.minor.milli.micro"
053: * or "major.minor.milli" or "major.minor" or "major",
054: * where major, minor, milli, micro are non-negative numbers
055: * <= 255. If the trailing version numbers are
056: * not specified they are taken as 0s. E.g. Version "3.1" is
057: * equivalent to "3.1.0.0".
058: * @return an instance of VersionInfo with the argument version.
059: * @exception throws an IllegalArgumentException when the argument version
060: * is not in the right format
061: * @stable ICU 2.6
062: */
063: public static VersionInfo getInstance(String version) {
064: int length = version.length();
065: int array[] = { 0, 0, 0, 0 };
066: int count = 0;
067: int index = 0;
068:
069: while (count < 4 && index < length) {
070: char c = version.charAt(index);
071: if (c == '.') {
072: count++;
073: } else {
074: c -= '0';
075: if (c < 0 || c > 9) {
076: throw new IllegalArgumentException(
077: INVALID_VERSION_NUMBER_);
078: }
079: array[count] *= 10;
080: array[count] += c;
081: }
082: index++;
083: }
084: if (index != length) {
085: throw new IllegalArgumentException(
086: "Invalid version number: String '" + version
087: + "' exceeds version format");
088: }
089: for (int i = 0; i < 4; i++) {
090: if (array[i] < 0 || array[i] > 255) {
091: throw new IllegalArgumentException(
092: INVALID_VERSION_NUMBER_);
093: }
094: }
095:
096: return getInstance(array[0], array[1], array[2], array[3]);
097: }
098:
099: /**
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,
110: int milli, int micro) {
111: // checks if it is in the hashmap
112: // else
113: if (major < 0 || major > 255 || minor < 0 || minor > 255
114: || milli < 0 || milli > 255 || micro < 0 || micro > 255) {
115: throw new IllegalArgumentException(INVALID_VERSION_NUMBER_);
116: }
117: int version = getInt(major, minor, milli, micro);
118: Integer key = new Integer(version);
119: Object result = MAP_.get(key);
120: if (result == null) {
121: result = new VersionInfo(version);
122: MAP_.put(key, result);
123: }
124: return (VersionInfo) result;
125: }
126:
127: /**
128: * Compares other with this VersionInfo.
129: * @param other VersionInfo to be compared
130: * @return 0 if the argument is a VersionInfo object that has version
131: * information equals to this object.
132: * Less than 0 if the argument is a VersionInfo object that has
133: * version information greater than this object.
134: * Greater than 0 if the argument is a VersionInfo object that
135: * has version information less than this object.
136: * @stable ICU 2.6
137: */
138: public int compareTo(VersionInfo other) {
139: return m_version_ - other.m_version_;
140: }
141:
142: // private data members ----------------------------------------------
143:
144: /**
145: * Version number stored as a byte for each of the major, minor, milli and
146: * micro numbers in the 32 bit int.
147: * Most significant for the major and the least significant contains the
148: * micro numbers.
149: */
150: private int m_version_;
151: /**
152: * Map of singletons
153: */
154: private static final HashMap MAP_ = new HashMap();
155: /**
156: * Error statement string
157: */
158: private static final String INVALID_VERSION_NUMBER_ = "Invalid version number: Version number may be negative or greater than 255";
159:
160: // private constructor -----------------------------------------------
161:
162: /**
163: * Constructor with int
164: * @param compactversion a 32 bit int with each byte representing a number
165: */
166: private VersionInfo(int compactversion) {
167: m_version_ = compactversion;
168: }
169:
170: /**
171: * Gets the int from the version numbers
172: * @param major non-negative version number
173: * @param minor non-negativeversion number
174: * @param milli non-negativeversion number
175: * @param micro non-negativeversion number
176: */
177: private static int getInt(int major, int minor, int milli, int micro) {
178: return (major << 24) | (minor << 16) | (milli << 8) | micro;
179: }
180: }
|